MATLAB Answers

0

Fit y=m*x into a non lineardata

Asked by Hadi Data on 22 Aug 2019
Latest activity Commented on by John D'Errico
on 22 Aug 2019
Hello,
I have a set of data x and y. The curve of the data looks linear passing in the origin, but its actully not linear. I want to fit the best curve into thse data with equation y=m*x only, because m is what i am interested in.
I have tried to use "polyfit" and "polyval" function implemented in matlab, but it gives me a line of y=mx +b, where m is approximatly 2.8e9 and b is 3.9. Is there a way where i can estimate m and have only a curve with y = mx?

  2 Comments

darova
on 22 Aug 2019
Can you please attach you script? Did you try fit()?
i attched the script.
I used the fit, but its giving me error because the data should be in matrix, as far as i understood.

Sign in to comment.

1 Answer

Answer by Stephan
on 22 Aug 2019
Edited by Stephan
on 22 Aug 2019
 Accepted Answer

This forces b to be equal to zero - the value you are interested in is m(1):
% Your data
x = 0:10;
y = [1.5 3 5 7 8 9 13 16 20 25 35];
% calculate m and new y-vals to plot them
x = [x' zeros(numel(x),1)];
m = x\y'
y_calc = m(1).*x(:,1);
% plot results
hold on
plot(x(:,1),y)
plot(x(:,1),y_calc)
hold off

  4 Comments

Show 1 older comment
Thanks for the help.
Stephan
on 22 Aug 2019
you can change x to:
x = [x' ones(numel(x),1)];
to get m & b with b=m(2) then. This usually is a better fit, but you wanted b to be zero. We do not know your data, but in general i would expect that forcing b to be zero will increase the error.
John D'Errico
on 22 Aug 2019
Lets see. If you compare two models, one where you estimate ONLY a slope term, and the second where you also estimate the constant term, then you can effectively prove the error can never be smaller with model 1 than with model 2. This is because you might have estimated a constant term of zero in model 2. So model 2 effectively includes model 1 as a subset. Since in the two term model you minimize the error over all possible sets of parameters, the two term error MUST be never larger than what you get with only a slope in the model, and it will geenrally be smaller.
That m changed significantly from one model to the other when you estimated it both ways suggests that really the model with no constant term in it is probably wrong for this data. Of course, since you know the curve to be nonlinear, I am not entirely sure why you want to force fit the wrong model.
I will postulate that what you really want to do is estimate the slope at x==0, for a curve that passes through the point (0,0). If that is the case, then I recommend you use a model like
y = a*x + b*x^2 + c*x^3
Be careful not to use too many terms, as polynomials can become ill-posed to fit. But see that the slope at x==0 is just a for that model.
Or even use a spline to model the curve. Differentiating a spline model is also easy.

Sign in to comment.