Non linear fit of y=f[x] where y can have multiple values for a single value of x
조회 수: 10 (최근 30일)
이전 댓글 표시
Save me from hollowness
2023년 4월 27일
댓글: Save me from hollowness
2023년 4월 27일
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
채택된 답변
John D'Errico
2023년 4월 27일
편집: John D'Errico
2023년 4월 27일
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.
추가 답변 (2개)
Steven Lord
2023년 4월 27일
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
chicken vector
2023년 4월 27일
편집: chicken vector
2023년 4월 27일
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)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!