Comparing two equations with measured data
Show older comments
Given two polynomial models Y= 3x^3 +4x^2 + 0.1x + 0.04 and Y2 = 2.5x^3 + 3.2x^2 + 0.5x - 0.1. Use the given measured data X={0.2, 0.6, 0.8, 0.9, 1}, Y={0.3, 0.65, 2.3, 0.5, 1.5}. Use MATLAB to fit the data with the given model. Provide residual plots for each and perform F-statistical analysis to determine which model provides better fits the measured data. I tried but was unsuccessfull.
plot(X,Y,'o')
model1= 3*x.^3 +4*x.^2 + 0.1*x + 0.04;
guess1=[1 1];
model2= 2.5*x.^3 + 3.2*x.^2 + 0.5*x - 0.1;
guess2=[1 1 ];
[alpha,R1,J1,CovB1,MSE1] = nlinfit(X,Y,model1, guess1);
[beta,R2,J2,CovB2,MSE2] = nlinfit(X,Y,model2, guess2);
x=linspace(2,18,1000);
ssmodel1=sum(R1.^2);
ssmodel2=sum(R2.^2);
sstotal=sum((X-mean(X)).^2);
r2model1=1-ssmodel1./sstotal;
r2model2=1-ssmodel2./sstotal;
F=ssmodel1./ssmodel2;
df=length(T)-2;
P=1-fcdf(F,df,df);
3 Comments
Star Strider
on 4 Dec 2021
Check the nlinfit documentation to understant how to write the ‘model1’ and ‘model2’ functions correctly as anonymous functions so that nlinfit can work with them. They need to be defined in terms of the independent variable vector and the parameter vector as arguments.
the cyclist
on 4 Dec 2021
@Kabit Kishore, in addition to the wise words of @Star Strider, be aware the you do not need to fit a non-linear function here (i.e. do not need nlinfit). When a model is described as "non-linear", it is referring to the coefficients, not the powers of the terms. So, for example
y = a*x^2 + b*x + c
is a linear model because it is linear in the coeffients a, b, and c; but
y = exp(d*x)
is non-linear, because it is not linear in the coefficient d.
I would recommend using the fitlm function. And read the documentation to see example of how to specify the fitting functions.
As @the cyclist pointed out, your model is in fact linear (linear combination of predictors). But your question is not clear to me, because you've already two equations (fitted probably on another dataset), so you're not looking for coefficients, you already have them!
x = [0.2, 0.6, 0.8, 0.9, 1]; y = [0.3, 0.65, 2.3, 0.5, 1.5];
mdl1 = fitlm(x, y, 'y ~ x1^3 + x1^2 + x1')
Answers (0)
Categories
Find more on Generalized Linear Regression 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!