Question

I'm using Matlab to analyse a couple of data, for I that I need the curve fitting, I've wrote this code from the documentation :

% I is  14 points vector that change its value in a loop 

y =0:13;
[p,S] = polyfit(I,y,1);
[fx, delta] = polyval(p,I,S);
plot(y,I,'+',fx,I,'-');

here is what I get : plot

my question is , how can evaluate this 'fitting', I mean how good it is , and how can I get the slope of this line?

UPDATE

after Rafaeli's answer , I had some trouble understand the results, since fx is the fitting curve fitting for y considering 'I' , meaning that I get for `fx':

-1.0454    3.0800   4.3897    6.5324   4.0947  3.8975   4.3476   9.0088  5.8307  6.7166 9.8243  11.4009  11.9223

instead the I values are :

 0.0021  0.0018   0.0017  0.0016  0.0018 0.0018 0.0017   0.0014  0.0016 0.0016  0.0014 0.0012 0.0012 0.0013

and the plot has exactly the value of `I' : enter image description here

so the result I hope to get should be near to those values ! Itried to switch the

[p,S] = polyfit(y,I,1);

but is didn't the wasn't any better fx= 0.0020,so my question is how can I do that ?

2nd UPDATE got it, here is the code :

y = 0:13 p = polyfit(y,I,1) fx = polyval(p,y); plot(y,I,'+',y,fx,'o')

here is the result :

enter image description here

thanks for any help !

Was it helpful?

Solution

The line is defined by y = ax + b, where a = p(1) and b = p(2), so the slope is p(1).

A simple way to know how good is the fit is to take the root mean square of the error: rms(fx - I). The lesser the value, better the fit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top