linear fit to data with intercept at origin

조회 수: 27 (최근 30일)
Lindsay Anderson
Lindsay Anderson 2016년 3월 22일
댓글: Star Strider 2018년 3월 30일
Hi, I am new to matlab and am working on a problem trying to fit a straight line through a series of data points, but I need to force the line to cross the origin.
I am using the linfit.m code and would like to modify it to calculate a(2) and the uncertainty in a(2), sa(2) (since a(1) will be 0). The code for linfit.m is attached.
Can anyone provide a suggestion on how to modify the code to force the line through the origin and to estimate the uncertainty in the slope (without using cramer's rule)? I know how to do this by hand and in excel (only 12 data points in the problem), but can't seem to sort it out in the linfit.m code, since it uses cramer's rule which from what I understand will produce a degenerate solution in estimating the uncertainty in the slope a(2) when I force a(1) to be zero.
Any help is greatly appreciated.
Thanks!
  댓글 수: 1
John D'Errico
John D'Errico 2016년 3월 22일
Ye gawds! That is terribly poor code. Why not use something better like regress from the stats toolbox? Or you can download my own polyfitn from the FEX.
Is your goal to learn to solve the problem yourself? It is rarely a good idea to write code to solve a problem when there is high quality code written by professionals out there. The result is often much like what I saw in linfit. Bad code, using poor algorithms. It may look impressive to the person who knows no better, but linfit is simply poor code.

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2016년 3월 22일
You can do that wit the mldivide,\ function (or operator), and by not including a vector of ones in the ‘x’ matrix (that would create an intercept term):
x = 0:10;
y = randi(99, 11, 1);
B = x(:)\y(:); % Estimate Parameter: x*B = y
y_est = x*B;
figure(1)
plot(x, y, 'bp')
hold on
plot(x, y_est, '-r')
hold off
grid
axis([0 10 ylim])
  댓글 수: 2
Kennan Bieniawski
Kennan Bieniawski 2018년 3월 29일
this doesnt work though...
Star Strider
Star Strider 2018년 3월 30일
‘Hi, I am new to matlab and am working on a problem trying to fit a straight line through a series of data points, but I need to force the line to cross the origin.’
Yes it does!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by