MATLAB Answers


fitting 1D data to y=x polynomial (cftool)

Asked by AP
on 7 Jul 2011

In an experiment vector y is a measurement of vector x. I want to fit polynomial y=x to this data and find R^2. When I use polyfit(x,y,1), MATLAB gives me y=ax+b but I am interested in knowing the R^2 when the data is fitted to y=x. Could someone please help me on that?

I found cftool which can solve my problem. Could someone tell me how I can retrieve R^2 from the analysis of cftool when the data is fitted to a polynomial?


  1 Comment

Hello. I think you only have to evaluate the fitted polynomial at the same abscissas and compare with the original ordinates:
poly = polyfit(x, y, 1);
yfit = polyval(poly, x);
cc = corrcoef(y, yfit);
R2 = cc(1, 2).^2;


No products are associated with this question.

1 Answer

Answer by Sean de Wolski
on 7 Jul 2011


on 7 Jul 2011

I did. But it does not fit to y=x. It fits it to y=mx+b. I solved my problem by using fit command.

All you need to do is change the vandermonde matrix, in his example 'F'

For zeroth order fit; i.e. what you want, it will just be x.^0, aka ones(size(x)) aka the mean.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!