Fit y=m*x into a non lineardata

4 ビュー (過去 30 日間)
Hadi Data
Hadi Data 2019 年 8 月 22 日
コメント済み: John D'Errico 2019 年 8 月 22 日
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 件のコメント
darova
darova 2019 年 8 月 22 日
Can you please attach you script? Did you try fit()?
Hadi Data
Hadi Data 2019 年 8 月 22 日
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.

サインインしてコメントする。

採用された回答

Stephan
Stephan 2019 年 8 月 22 日
編集済み: Stephan 2019 年 8 月 22 日
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 件のコメント
Stephan
Stephan 2019 年 8 月 22 日
編集済み: Stephan 2019 年 8 月 22 日
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
John D'Errico 2019 年 8 月 22 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by