
How to use a spline for double values of y?
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
arich82
le 7 Oct 2015
Modifié(e) : arich82
le 7 Oct 2015
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?)
Réponses (1)
Alessandro Orchini
le 31 Mar 2017
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 commentaires
Stefi
le 29 Mai 2024
Modifié(e) : Stefi
le 29 Mai 2024
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);
Voir également
Catégories
En savoir plus sur Splines dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
