Non linear fit of y=f[x] where y can have multiple values for a single value of x

10 views (last 30 days)
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 :)

Accepted Answer

John D'Errico
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.
  1 Comment
Save me from hollowness
Save me from hollowness on 27 Apr 2023
Indeed, the fit does not make sense.
This curve represents the evolution of coordinates with time. The process that makes the coordinates move is random but there is no noise. However, since the process is random. My guess is that i can't even interpolate since nothing says that at half distance between x(1) and x(2), x=1.5. Therefore, In a sense I'm kinda f**** ahah. But thanks anyway for the response at least it confirmed my doubts :)
Have a nice day.

Sign in to comment.

More Answers (2)

Steven Lord
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
Save me from hollowness
Save me from hollowness on 27 Apr 2023
Edited: Save me from hollowness on 27 Apr 2023
Thank you for the answer :) , indeed in this example a polynomial fit is not suited, I was speaking about my actual code that I can't really share in details here.
Yes, these coordinates are a function of time so your answer seems the most appropriate if I absolutly want to fit the data. However, John cleverly pointed it out, the chaotic behaviour of the curve makes any fit useless in a sense.

Sign in to comment.


chicken vector
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)

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!