Discussion:
Curve fitting (parabola)
(too old to reply)
Chon-Ji
2006-03-09 05:55:58 UTC
Permalink
Hello, I need to fit a parabolic curve (concave up) to a set of
point...is this possible? How do I do this in matlab?
Roger Stafford
2006-03-09 06:32:43 UTC
Permalink
Post by Chon-Ji
Hello, I need to fit a parabolic curve (concave up) to a set of
point...is this possible? How do I do this in matlab?
-------------------------
Let x and y be n-element vectors for the points' Cartesian coordinates.
To get a least squares parabolic fit to them, do this:

s0 = n; s1 = sum(x); s2 = sum(x.^2); s3 = sum(x.^3); s4 = sum(x.^4);
A = [s4,s3,s2;s3,s2,s1;s2,s1,s0];
d = [sum(x.^2.*y);sum(x.*y);sum(y)];
a = A\d;

The best-fitting parabola would then be given by:

y = a(1)*x.^2+a(2)*x+a(3);

(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford
Chon-Ji
2006-03-09 07:40:52 UTC
Permalink
Post by Roger Stafford
Post by Chon-Ji
Hello, I need to fit a parabolic curve (concave up) to a set of
point...is this possible? How do I do this in matlab?
-------------------------
Let x and y be n-element vectors for the points' Cartesian
coordinates.
s0 = n; s1 = sum(x); s2 = sum(x.^2); s3 = sum(x.^3); s4 =
sum(x.^4);
A = [s4,s3,s2;s3,s2,s1;s2,s1,s0];
d = [sum(x.^2.*y);sum(x.*y);sum(y)];
a = A\d;
y = a(1)*x.^2+a(2)*x+a(3);
(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford
so the plot of the parabola would be in [x,y] right? What I currently
have are pixel coordinates from the image, would that be a problem?
Roger Stafford
2006-03-10 01:06:02 UTC
Permalink
Post by Chon-Ji
so the plot of the parabola would be in [x,y] right? What I currently
have are pixel coordinates from the image, would that be a problem?
-----------------
It doesn't make any difference if they are coordinates of pixels or any
other kind of objects, just so long as they are coordinates for points in
some Cartesian coordinate system. Do a plot to match your original points
(in yellow) against those on the parabola (in red):

% Generate a from x and y
y2 = a(1)*x.^2+a(2)*x+a(3);
plot(x,y,'y+',x,y2,'ro')

(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford
Chon-Ji
2006-03-11 06:50:21 UTC
Permalink
Post by Chon-Ji
Post by Chon-Ji
so the plot of the parabola would be in [x,y] right? What I
currently
Post by Chon-Ji
have are pixel coordinates from the image, would that be a
problem?
-----------------
It doesn't make any difference if they are coordinates of pixels or any
other kind of objects, just so long as they are coordinates for points in
some Cartesian coordinate system. Do a plot to match your original
points
% Generate a from x and y
y2 = a(1)*x.^2+a(2)*x+a(3);
plot(x,y,'y+',x,y2,'ro')
(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford
I tried using this code but it just turned out like a slanted line.

My x and y are the coordinates of an object trajectory. Basically, I
know that the object follows an upward parabolic path so I think
having a parabolic curve fitting function would help improve the
accruacy.

I assigned 6 to s0 since I have 6 points in the trajectory.

s0 = 6; s1 = sum(x); s2 = sum(x.^2); s3 = sum(x.^3); s4 = sum(x.^4);

A = [s4,s3,s2;s3,s2,s1;s2,s1,s0];
d = [sum(x.^2.*y);sum(x.*y);sum(y)];
a = A\d;

y2 = a(1)*x.^2+a(2)*x+a(3);
x2 = x;

thanks
Roger Stafford
2006-03-11 14:49:38 UTC
Permalink
Post by Chon-Ji
I tried using this code but it just turned out like a slanted line.
My x and y are the coordinates of an object trajectory. Basically, I
know that the object follows an upward parabolic path so I think
having a parabolic curve fitting function would help improve the
accruacy.
I assigned 6 to s0 since I have 6 points in the trajectory.
s0 = 6; s1 = sum(x); s2 = sum(x.^2); s3 = sum(x.^3); s4 = sum(x.^4);
A = [s4,s3,s2;s3,s2,s1;s2,s1,s0];
d = [sum(x.^2.*y);sum(x.*y);sum(y)];
a = A\d;
y2 = a(1)*x.^2+a(2)*x+a(3);
x2 = x;
thanks
-----------------------
Could you please give the x-y coordinates of the six points that gave
you a "slanted line" result? If they lie on the path of an object's
trajectory acting under gravity, the parabola should be markedly concave
downwards. That is, a(1) should be negative.

I assumed when I gave you that code that you wanted a parabola of the
form y = a*x^2+b*x+c and in which the mean square difference between the y
values of the given points and the corresponding parabola points with the
same x value was to be a minimum. Did I assume correctly?

There are other parabola solutions possible having axes of symmetry at
orientations other than parallel to the y-axis. Also you might have
wanted to have a least mean square orthogonal distance from the points.
These would be more difficult problems.

(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford
Chon-Ji
2006-03-11 17:10:36 UTC
Permalink
Post by Chon-Ji
Post by Chon-Ji
I tried using this code but it just turned out like a slanted
line.
Post by Chon-Ji
My x and y are the coordinates of an object trajectory.
Basically, I
Post by Chon-Ji
know that the object follows an upward parabolic path so I
think
Post by Chon-Ji
Post by Chon-Ji
having a parabolic curve fitting function would help improve
the
Post by Chon-Ji
Post by Chon-Ji
accruacy.
I assigned 6 to s0 since I have 6 points in the trajectory.
s0 = 6; s1 = sum(x); s2 = sum(x.^2); s3 = sum(x.^3); s4 =
sum(x.^4);
Post by Chon-Ji
A = [s4,s3,s2;s3,s2,s1;s2,s1,s0];
d = [sum(x.^2.*y);sum(x.*y);sum(y)];
a = A\d;
y2 = a(1)*x.^2+a(2)*x+a(3);
x2 = x;
thanks
-----------------------
Could you please give the x-y coordinates of the six points that gave
you a "slanted line" result? If they lie on the path of an
object's
trajectory acting under gravity, the parabola should be markedly concave
downwards. That is, a(1) should be negative.
I assumed when I gave you that code that you wanted a parabola of the
form y = a*x^2+b*x+c and in which the mean square difference
between the y
values of the given points and the corresponding parabola points with the
same x value was to be a minimum. Did I assume correctly?
There are other parabola solutions possible having axes of
symmetry at
orientations other than parallel to the y-axis. Also you might
have
wanted to have a least mean square orthogonal distance from the points.
These would be more difficult problems.
(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford
here are the points:
x =

183.2872
186.9183
186.9305
191.5843
196.2380

y =

110.9744
103.1771
103.1502
94.4743
85.7985

Yes, that's what I wanted. The curve fitting function does indeed
show something similar to a parabolic curve I just thought the curve
could be smoother. The object we're tracking is not falling under
gravity, it's plainly travelling on a road. We took it at a certain
angle so that the trajectory resembles an upward parabolic curve.
Loading...