How to use a spline for double values of y?

11 次查看(过去 30 天)
How can I use a spline to fit a list of points that has double values for each x-coordinate?77 For example, at the x=0 coordinate, I have y=0.2 and y=0.4 (it is a sort of ellipsis).
  1 个评论
arich82
arich82 2015-10-7
编辑:arich82 2015-10-7
This data would represent the most degenerate sort of ellipse imaginable, with major axis a=0.2, and minor axis b=0, i.e. a vertical line:
If you have additional points, you might try converting your x-y (cartesian) coordinates to th-r (polar) coordinates, then performing a spline fit to the polar data (and finally converting back to cartesian after interpolating, assuming that is you goal). This might require shifting your x-y data to be centered around the origin to ensure that theta is unique and strictly increasing.
However, if all you have are two data points 180-degrees apart, you're never going to get anything but a straight line without some additional information about the properties of the ellipse.
In order to get more help, you're going to need to post some additional information, either a larger sample of your data, or additional parameters for the ellipse. If you truly have only two data points 180-degrees apart, then I'm afraid your desired solution might not exist...
(Lastly, if you know the data obeys an ellipse, why not just try to find the best-fit ellipse equation instead of using a spline fit?)

请先登录,再进行评论。

回答(1 个)

Alessandro Orchini
Alessandro Orchini 2017-3-31
Suppose you have the ellipsis defined by:
N = 20;
dt = 2*pi/N;
theta = 0:dt:2*pi;
x = 0.5*cos(theta);
y = 0.1*sin(theta);
Assuming your points are already ordered, you can define two functions, one for the x values and one for the y values, as functions of the points indexes. These are single-valued functions (it is an arclength representation) and can be fitted onto splines.
splX = spline(1:length(x),x,1:0.1:length(x));
splY = spline(1:length(y),y,1:0.1:length(y));
figure(1)
clf
plot(splX,splY,'k-')
hold on
plot(x,y,'r.')
hold off
If your points are not ordered, you will need to order them before fitting, by starting from a certain point, looking for the closest neighbour, and iterating.
  3 个评论
Stefi
Stefi 2024-5-29
编辑:Stefi 2024-5-29
i am doing a similar kind of thing . how will I use the above method in my code to get the answer?
i have x and y cooridnates of semicircle and slopes at each point of evaluation. I want to generate the slope at end point using spline but it is showing same error .
theta = linspace(pi/2, -pi/2, 20);
R = 0.5;
x = R*cos(theta) ;
y = R*sin(theta);
slope= slp(y); % slp is a function made for calculating slope
slope_at_end = spline(x,slp(y),1);
Torsten
Torsten 2024-5-29
编辑:Torsten 2024-5-29
Here is the spline structure. Now you need to tell is where you want the slopes to be evaluated:
theta = linspace(pi/2, -pi/2, 20);
R = 0.5;
x = R*cos(theta) ;
y = R*sin(theta);
pp = cscvn([x;y])
pp = struct with fields:
form: 'pp' breaks: [0 0.2874 0.5747 0.8621 1.1495 1.4368 1.7242 2.0116 2.2989 2.5863 2.8737 3.1610 3.4484 3.7358 4.0231 4.3105 4.5979 4.8852 5.1726 5.4600] coefs: [38x4 double] pieces: 19 order: 4 dim: 2
fnplt(pp)
axis equal
xlim([0 0.5])
ylim([-0.5 0.5])

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Splines 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by