Create a fit for data with a polynom first degree

Hey, I have two data sets that I have already plotted and now I want to create a new plot with a polynom first degree to have a linear model for my data and calculate the sum of squares from it. The problem is that in the new plot my data points are already linear even though they aren't and it shows me, that the sum of squares is 1, so I guess I made a mistake implenting the correct data.
x = [0.1201 0.1348 0.1708 0.4551 0.7091 1.5297 2.4558 4.4908 7.7372]; %Dataset 1
y = [0.5291 0.6967 1.1081 4.3538 7.2549 16.6232 27.1949 50.4295 87.4983]; %Dataset 2
plot(x,y,'o') %Plot dataset 1 and 2
p=polyfit(x,y,1); %Polynom first degree
py=polyval(p,x);
hold on
plot(x,py) %Insert the model in the plot
hold off
Rsq=1-sum((y-py).^2)/sum((y-mean(y)).^2);
Rsq=1.0000

 Accepted Answer

I would expect to be 1 (or close to it) if a lilnear model is a perfect firt to the data.
Using fitlm
x = [0.1201 0.1348 0.1708 0.4551 0.7091 1.5297 2.4558 4.4908 7.7372]; %Dataset 1
y = [0.5291 0.6967 1.1081 4.3538 7.2549 16.6232 27.1949 50.4295 87.4983]; %Dataset 2
mdl = fitlm(x(:), y(:))
mdl =
Linear regression model: y ~ 1 + x1 Estimated Coefficients: Estimate SE tStat pValue ________ __________ _______ __________ (Intercept) -0.84238 0.00050089 -1681.7 6.9415e-21 x1 11.417 0.00015913 71750 2.6979e-32 Number of observations: 9, Error degrees of freedom: 7 Root Mean Squared Error: 0.00117 R-squared: 1, Adjusted R-Squared: 1 F-statistic vs. constant model: 5.15e+09, p-value = 2.7e-32
figure
plot(mdl)
.

2 Comments

Thanks for your answer.
I think something went wrong with the data, because it shouldn't be that linear and R^2 shouldn't be one.
As always, my pleasure!
I do not know what your data are. It could be that you need to collect more data, and that the data you have currently show only the linear trend. This would be the situation if they could be modeled by or a similar function that has an approximate linear trend for low values of ‘x’ and becomes nonlinear (and asymptotic) as ‘x’ increases. That could be modeled with fitnlm or a number of other nonlinear parameter estimation methods.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots 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!