Comparing two equations with measured data

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

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.
@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')
mdl1 =
Linear regression model: y ~ 1 + x1 + x1^2 + x1^3 Estimated Coefficients: Estimate SE tStat pValue ________ ______ ________ _______ (Intercept) 1.8759 9.077 0.20666 0.87026 x1 -13.066 64.314 -0.20315 0.87241 x1^2 28.847 117.95 0.24456 0.8473 x1^3 -16.476 63.504 -0.25945 0.83839 Number of observations: 5, Error degrees of freedom: 1 Root Mean Squared Error: 1.35 R-squared: 0.35, Adjusted R-Squared: -1.6 F-statistic vs. constant model: 0.179, p-value = 0.901

Sign in to comment.

Answers (0)

Asked:

on 4 Dec 2021

Commented:

on 8 Dec 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!