Currently I have a set of 2D points as shown below.
I need to obtain fitted curve of the points to determine the shape of the object.
Matlab offers a spline fitting function . I have to applied it to the set of points. However, the results I am obtaining is shown below. .
xx = linspace(-10,10,20);
pp2 = splinefit(PV(:,1),PV(:,2),8,5);
y2 = ppval(pp2,xx);
plot(x,y,'.',xx,y2), grid on
axis([-10 10 -10 10]);
PV is the data points. I am unsure where the problem lies.
You are trying to fit PV(:,1) = f( PV(:,2) )
that is, fitting directly y=f(x)
. But as you can see from your points, there are multiple values of y
for certain values of x
(around x=0
and x=-15
). Thus, you cannot mathematically fit y=f(x)
for any function f(.)
here.
What you can do is fit a parametric curve using an auxiliary parameter t
:
t = linspace(0, 1, size(PV,1));
ppx = splinefit(PV(:,1),t,8,5); % x = f_x(t)
ppy = splinefit(PV(:,2),t,8,5); % y = f_y(t)
Now you can plot the curve (f_x(t), f_y(t))
:
tt = linspace(0, 1, 50);
x2 = ppval(ppx, tt);
y2 = ppval(ppy, tt);
plot( PV(:,1), PV(:,2), '.', x2, y2); grid on;
axis([-10 10 -10 10]);
Note :
This fitting scheme is based on the assumption that the points in PV
are ordered : That is, the curve should follow from PV(1,:)
to PV(2,:)
and then to PV(3,:)
and so on. If this is not the case (eg, PV(1,:)
has x value of ~-10, PV(2,:)
has x value of ~0 and PV(3,:)
jumps back to x ~-10) then you are in deep s&#t.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.