user2861089
user2861089

Reputation: 1235

In matlab fit polynomial to data, forcing y-intercept to zero.

In matlab I am trying to use polyfitZero function to fit polynomial to data, forcing y-intercept to zero. However, my code doesn't seem to achieve this but I'm unsure what I am doing wrong.

a = [2.0044 2.0056 2.0021 2.0021 2.0048 2.0026 2.0035 2.0013 2.0035 2.0026];
b = [0.1006 0.0848 0.0502 0.0502 0.0909 0.0385 0.0732 0.0732 0.0896 0.0772];
scatter(a, b, 6);
hold on
p = polyfitZero(a,b,1);
f = polyval(p,a);
plot(a,f,'Color',[0.7500    0.7500    0.7500],'linewidth',1.5)
box on; 
ylim([0 0.11]);

Upvotes: 0

Views: 1213

Answers (1)

antongrin
antongrin

Reputation: 156

Well, in fact your code does exactly what it is supposed to: the fitting polynomial crosses y-axis precisely at (0,0). You can check this just by adding xlim([0 2.1]); to the end of your script: That's how it looks.
I may suppose that the problem is that your data are shifted along x-axis by 2. If you want your polynomial to run through (2,0) then this solution may be appropriate:

a = [2.0044 2.0056 2.0021 2.0021 2.0048 2.0026 2.0035 2.0013 2.0035 2.0026];
b = [0.1006 0.0848 0.0502 0.0502 0.0909 0.0385 0.0732 0.0732 0.0896 0.0772];
scatter(a, b, 6);
hold on
a=a-2;
p = polyfitZero(a,b,1);
f = polyval(p,a);
a=a+2;
plot(c,f,'Color',[0.7500    0.7500    0.7500],'linewidth',1.5)
box on; 
ylim([0 0.11]);

That's how it works.

Nevertheless, you should explain what you expect your result to look like.

Upvotes: 4

Related Questions