posted 3 years ago

Hi there,

I was looking through the Java Math class to find out how one if they have a number of values find the function if they were on a graph. I would like to have for example a collection of values and then using some code find the function of those values. I have looked on many forums but haven't been able to find anything relating to this. Is this possible?

Thanks for any replies.

TS.

I was looking through the Java Math class to find out how one if they have a number of values find the function if they were on a graph. I would like to have for example a collection of values and then using some code find the function of those values. I have looked on many forums but haven't been able to find anything relating to this. Is this possible?

Thanks for any replies.

TS.

posted 3 years ago

Sounds to me like a form of regression analysis; but I'm afraid it's decades since I did any of that stuff.

The link may be a good place to start though.

Winston

Tom Storm wrote:I was looking through the Java Math class to find out how one if they have a number of values find the function if they were on a graph. I would like to have for example a collection of values and then using some code find the function of those values. I have looked on many forums but haven't been able to find anything relating to this. Is this possible?

Sounds to me like a form of regression analysis; but I'm afraid it's decades since I did any of that stuff.

The link may be a good place to start though.

Winston

Matthew Brown

Bartender

Posts: 4568

9

posted 3 years ago

Are you looking for finding a quadratic given points that lie exactly on it? Or the "best fit" quadratic given points that you expect to lie near one.

If it's the former, you're looking at solving y = ax^2 + bx + c for a, b and c, when you're given (x, y) coordinates. Since there are three unknowns you only need three points, which will give you three simultaneous equations. Any further points will lie on that quadratic anyway, or will mean there's no solution.

If it's the latter, then Winston's right - that's a regression problem.

If it's the former, you're looking at solving y = ax^2 + bx + c for a, b and c, when you're given (x, y) coordinates. Since there are three unknowns you only need three points, which will give you three simultaneous equations. Any further points will lie on that quadratic anyway, or will mean there's no solution.

If it's the latter, then Winston's right - that's a regression problem.

Campbell Ritchie

Marshal

Posts: 56536

172

Piet Souris

Master Rancher

Posts: 2044

75

posted 3 years ago

@Tom:

the topic title is about a quadratic function from two arrays, and in your latest

reply you mention 'I'm looking for a quadratic based on the exact points'.

That makes it still not clear to me. Can you reply to Matthews questions?

Especially: do you want to determine a, b and c from just three (exact?) points,

or do you wish to find the best (in some way) quadratic given a lot of points, maybe

split over two arrays?

Greetz,

Piet

the topic title is about a quadratic function from two arrays, and in your latest

reply you mention 'I'm looking for a quadratic based on the exact points'.

That makes it still not clear to me. Can you reply to Matthews questions?

Especially: do you want to determine a, b and c from just three (exact?) points,

or do you wish to find the best (in some way) quadratic given a lot of points, maybe

split over two arrays?

Greetz,

Piet

Matthew Brown

Bartender

Posts: 4568

9

posted 3 years ago

OK. As I said, that means you can only base it on three points. So you're trying to find a, b and c in the equation y = ax^2 + bx + c. That means you're trying to solve (my attempt to represent a matrix equation in ASCII):

where you know all the x's and y's.

If you want a library to help you, look for one that does linear/matrix algebra. If they can do Gaussian Elimination or matrix inverses they can solve this easily enough. This will generalise to higher-order polynomials as (the matrix just gets bigger: for an order n polynomial you need n points, and you'll get an n x n matrix), though if you're only interested in quadratics it wouldn't be impossible to solve it yourself.

Tom Storm wrote:I'm looking for a quadratic based on the exact points. Would I have to write that or do you know of any class that would simplify the matter?

OK. As I said, that means you can only base it on three points. So you're trying to find a, b and c in the equation y = ax^2 + bx + c. That means you're trying to solve (my attempt to represent a matrix equation in ASCII):

where you know all the x's and y's.

If you want a library to help you, look for one that does linear/matrix algebra. If they can do Gaussian Elimination or matrix inverses they can solve this easily enough. This will generalise to higher-order polynomials as (the matrix just gets bigger: for an order n polynomial you need n points, and you'll get an n x n matrix), though if you're only interested in quadratics it wouldn't be impossible to solve it yourself.

posted 3 years ago

My apologies to all if I have caused confusion. To answer your question Piet I will have two arrays of doubles and I would like to find a quadratic based on those points. Due to your explanation the latter is true.

"wish to find the best (in some way) quadratic given a lot of points, maybe split over two arrays".

I misunderstood Matthews question, I need the quadratic of best fit as I will have a number of values. Does that make this a regression analysis question?

Just to fully explain what I'm doing, I will have two arrays of variables and I would like to find the function of them should they be displayed on a graph.

Thank you for your answers.

TS

"wish to find the best (in some way) quadratic given a lot of points, maybe split over two arrays".

I misunderstood Matthews question, I need the quadratic of best fit as I will have a number of values. Does that make this a regression analysis question?

Just to fully explain what I'm doing, I will have two arrays of variables and I would like to find the function of them should they be displayed on a graph.

Thank you for your answers.

TS

Matthew Brown

Bartender

Posts: 4568

9

posted 3 years ago

Posts crossed: yes, then you've got a regression problem. What it really comes down to is trying to minimise:

SUM (y_i - (a x_i^2 + b x_i + c))^2

It's still a linear algebra problem, but it takes a bit more to solve it from scratch. But if you can find a library that does linear regression it should do the heavy lifting for you.

SUM (y_i - (a x_i^2 + b x_i + c))^2

It's still a linear algebra problem, but it takes a bit more to solve it from scratch. But if you can find a library that does linear regression it should do the heavy lifting for you.

Matthew Brown

Bartender

Posts: 4568

9

Piet Souris

Master Rancher

Posts: 2044

75

posted 3 years ago

hi Tom,

yes that clears any doubt.

A well known method is the method of 'least squares'.

Look for this method in Wiki. You see that you need what Mattew just wrote.

(edit: what he wrote a couple of replies ago! ;)

Say, you have a series of points (x1, y1), (x2, y2), ..., (xn, yn).

Now, you want to determine a, b and c such that the error you get

by saying y = a.x^2 + b.x + c is "as small as possible".

Now, if you put all the equations you get with your collection of points,

you get the following series of equations:

x1 ^2 * a + x1 * b + 1 * c = y1

x2 ^2 * a + x2 * b + 1 * c = y2

...

xn ^2 * a + xn * b + 1 * c = yn

Now, if n = 3, then you have Matthews situation and solution.

If n > 3, then in all likelyhood, these equation have no solutons for a, b and c.

Write the equations as a matrix equation:

X * v = Y where X is an n x 3 matrix with first column (x1^2, x2 ^2, et cetera), third column = (1, 1, ..., 1),

v is the vector (a, b, c), and Y = the column vector (y1, y2, y3,... yn), then

what we have to do is determine a, b and c such that the vector (X * v - Y) has minimal length.

The solution is then:

v = ((X transposed) * X)^-1 * (X transposed) * Y.

So, in the end you still need to be able to invert matrices. So the thing to Google for is matrix inversion in Java,

to see what libraries are available.

There are many packages available that can solve all these and many more with the click of a button.

Sure you need Jave for this? There is a course at Coursera about the package 'R', that starts december 1st,

is totally free and which I will be following myself.

Greetz,

Piet

yes that clears any doubt.

A well known method is the method of 'least squares'.

Look for this method in Wiki. You see that you need what Mattew just wrote.

(edit: what he wrote a couple of replies ago! ;)

Say, you have a series of points (x1, y1), (x2, y2), ..., (xn, yn).

Now, you want to determine a, b and c such that the error you get

by saying y = a.x^2 + b.x + c is "as small as possible".

Now, if you put all the equations you get with your collection of points,

you get the following series of equations:

x1 ^2 * a + x1 * b + 1 * c = y1

x2 ^2 * a + x2 * b + 1 * c = y2

...

xn ^2 * a + xn * b + 1 * c = yn

Now, if n = 3, then you have Matthews situation and solution.

If n > 3, then in all likelyhood, these equation have no solutons for a, b and c.

Write the equations as a matrix equation:

X * v = Y where X is an n x 3 matrix with first column (x1^2, x2 ^2, et cetera), third column = (1, 1, ..., 1),

v is the vector (a, b, c), and Y = the column vector (y1, y2, y3,... yn), then

what we have to do is determine a, b and c such that the vector (X * v - Y) has minimal length.

The solution is then:

v = ((X transposed) * X)^-1 * (X transposed) * Y.

So, in the end you still need to be able to invert matrices. So the thing to Google for is matrix inversion in Java,

to see what libraries are available.

There are many packages available that can solve all these and many more with the click of a button.

Sure you need Jave for this? There is a course at Coursera about the package 'R', that starts december 1st,

is totally free and which I will be following myself.

Greetz,

Piet