Non linear fit of y=f[x] where y can have multiple values for a single value of x
10 views (last 30 days)
Show older comments
Save me from hollowness
on 27 Apr 2023
Commented: Save me from hollowness
on 27 Apr 2023
Hello everyone :)
I was just wondering if there is a clever way to fit the set of data i've got here (if it's possible...) :
For example, I have a set of coordinates :
x=[1,2,3,2,4,6,5,7,5,1,2,3]
y=[2,3,4,3,7,2,9,5,4,9,3,2]
% ploting plot(x,y) obviously gives a curve which can not be fitted with "common" fitting methods, I'm exagerating but you see my point i guess.
I tried to polyfitn but it does not seem to be the correct way to do it or I did not understand the function correctly because it seems to allow you to fit multiple independant variables instead of 1. However that is not really the issue I'm having here.
the curve can have a totally chaotic behaviour, therefore I can't fit the curve to a circle for instance. My guess would be that polynomial fit should work but again, it works when each x has an unique y value associated to it.
Thank you :)
0 Comments
Accepted Answer
John D'Errico
on 27 Apr 2023
Edited: John D'Errico
on 27 Apr 2023
No. You cannot use polyfitn. It is designed to solve a SINGLE valued problem, but with 1 or more independent variables. You cannot make a code do what it is not designed to do. Nor can you use a polynomial fit of any sort, because a polynomial fit would still presume a single valued function.
In terms of "fitting" a curve, that makes little sense anyway, since your curve is, as you admit, totally chaotic. (What is noise in this context, what is real signal?) So at best you can use an interpolating spline, perhaps a tool like cscvn, from the curve fitting toolbox. There is no real fit involved, since the curve will pass drectly through each point.
More Answers (2)
Steven Lord
on 27 Apr 2023
Are you trying to fit x and y as functions of a third variable t, which in this case could be the indices of the elements in the vectors?
x = [1,2,3,2,4,6,5,7,5,1,2,3];
y = [2,3,4,3,7,2,9,5,4,9,3,2];
t = 1:numel(x);
plot(t, x, 'r-', t, y, 'k--')
Those don't look all that much like polynomials to me. [Your plot of x versus y certain isn't a polynomial.] Maybe you could try fitting quadratics to them, but those fits don't look that great. Higher orders got a little better but not much.
px = polyfit(t, x, 2);
py = polyfit(t, y, 2);
tt = 1:0.5:numel(x);
figure
plot(t, x, 'r-', tt, polyval(px, tt), 'k+')
title('x')
figure
plot(t, y, 'r-', tt, polyval(py, tt), 'k+')
title('y')
figure
plot(x, y, 'r-', polyval(px, tt), polyval(py, tt), 'k+')
title('x versus y')
What exactly are you hoping to do with these fitted curves?
2 Comments
chicken vector
on 27 Apr 2023
Edited: chicken vector
on 27 Apr 2023
You can't fit this because bijective mapping is required between x and y.
Maybe this can hekp with your task.
x = [1,2,3,2,4,6,5,7,5,1,2,3];
y = [2,3,4,3,7,2,9,5,4,9,3,2];
[sortedX, sortedIdx] = sort(x);
sortedY = y(sortedIdx);
figure;
grid on;
plot(sortedX, sortedY)
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!