Curve fitting for non-linear data

4 vues (au cours des 30 derniers jours)
Julian
Julian le 11 Oct 2018
Commenté : Alex Sha le 29 Jan 2022
I am trying to fit some data using lsqcurvefit in MATLAB but I am fairly new to this area.
xdata1 = [0 60 660 1250];
ydata1 = [0 18 23 31];
In the image below, the red line is the fit I want to achieve. Sadly, Polyfit does not provide suitable results.
How can I achieve this fit? Thank you in advance!

Réponse acceptée

Matt J
Matt J le 11 Oct 2018
I believe piece-wise linear fitting was in the scope of Bruno's free-knot spline fitting package,
  1 commentaire
Julian
Julian le 12 Oct 2018
Thank you Matt I will look into this!

Connectez-vous pour commenter.

Plus de réponses (2)

Chaoyu Zhang
Chaoyu Zhang le 11 Oct 2018
Modifié(e) : Chaoyu Zhang le 15 Oct 2018
You can use the method described below,
The target equation (3rd order or maybe higher) is
y = a*x.^3 + b*x.^2 + c*x + d;
A * p = y;
p is the parameters of the equation,
p = [a;b;c;d]
A is the matrix made of x.^3,x.^2,x,1,
A = [x(1).^3 x(1).^2 x(1) 1; ... ; x(n).^3 x(n).^2 x(n) 1]
y is the vector made of y,
y = [y(1); ... ;y(n)];
p = (A.'*A)^(-1)*A.'*y;
Now you get the parameters you need.
  5 commentaires
Matt J
Matt J le 15 Oct 2018
Modifié(e) : Matt J le 15 Oct 2018
Well, the poorer accuracy comes from the inversion of (A.'*A), since cond(A.'*A) is the square of cond(A).
>> A=rand(1000,100);
>> cond(A.'*A)
ans =
632.4462
>> cond(A)
ans =
25.1485
When solving with mldivide(), the QR decomposition is used, which avoids this inversion. With A=Q*R,
(A.'*A)^(-1)*A.'*y
=(R.'*R)^(-1)*R.'*Q.'*y
=R^(-1)*Q.'*y
So, the inversion involves only R^(-1) and cond( R )=cond(A).
Matt J
Matt J le 15 Oct 2018
Modifié(e) : Matt J le 15 Oct 2018
Here is a test showing the increased error sensitivity of inv(A.'*A)*(A.'*y).
N=1000;
M=15;
A=vander(linspace(1,3.3,M)) + eye(M);
xt=rand(M,1);
yt=A*xt;
y=yt+randn(M,N)*1e-6;
x1=inv(A.'*A)*(A.'*y);
x2=A\y;
Error1=mean( sqrt(sum((x1-xt).^2)) )
Error2=mean( sqrt(sum((x2-xt).^2)) )
should give something like
Error1 =
5.0094
Error2 =
2.8313e-04

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 11 Oct 2018
You cannot get that unless you put in a model curve for that shape. Otherwise functions are not going to know that it's a piecewise linear fit or some sharply kinked log function or whatever. And having more data points would help too. Then you can use fitnlm.
I'm attaching several examples for piecewise linear fit and non-linear fits.
  4 commentaires
Julian
Julian le 11 Oct 2018
Yes that would greatly improve the fit. Can you describe me how it is possible to combine two linear fit into one function in Matlab ?
Alex Sha
Alex Sha le 29 Jan 2022
For three order of polyfit: y = p1+p2*x+p3*x^2+p4*x^3
Root of Mean Square Error (RMSE): 5.67944047963309E-15
Sum of Squared Residual: 1.2902417664678E-28
Correlation Coef. (R): 1
R-Square: 1
Parameter Best Estimate
---------- -------------
p1 3.92787554473843E-15
p2 0.340654276995852
p3 -0.000698994200659206
p4 3.57048623250019E-7
while, if taking function as: y = p1*x/(p2+x)^2-p3*x
Root of Mean Square Error (RMSE): 0
Sum of Squared Residual: 0
Correlation Coef. (R): 1
R-Square: 1
Parameter Best Estimate
---------- -------------
p1 9035.3274708592
p2 119.630504666011
p3 -0.0199834390844168
Obviously, the second function should be more reasonable than 3rd polyfit.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Least Squares 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!

Translated by