MATLAB Answers

Linear Regression Through Origin

147 views (last 30 days)
KH
KH on 17 Apr 2019
Answered: Image Analyst on 18 Apr 2019
I have the following code, which is supposed to create a linear fit (red line) of the raw data (stress and strain) through the origin. However, I am noticing that the fit of the raw data isn't what I would expect. With the definition of least squares fitting, I would assume the fit to have the area below (blue etches) and above (green etches) the curve be equal (see attached image). Can someone perhaps provide me guidance on how I could implement this?
Thank you in advanced!
Code:
clear all; close all; clc
% Strain (x) and stress (y) data
x = [0.001285797 0.003961826 0.00794437 0.011775521 0.015872369 0.02104614 ...
0.025087779 0.028869823 0.03265608 0.035941842 0.039237685 0.041613848 ...
0.043244122 0.043796973 0.044113916];
y = [435.5682116 709.7425027 1009.698448 1681.209018 2695.849269 2643.749641 ...
3752.858776 5470.575841 7448.561195 8853.52726 9654.903814 10952.03131 ...
11970.73227 12523.02663 12909.90468];
% Computing fitted line
K = x(:)\y(:);
yfit = x(:)*K;
% Plotting
figure(1)
plot(x, y, 'ko')
hold on
plot(x, yfit, '-r')
xlabel('Strain')
ylabel('Stress, Pa')
legend({'Raw Data','Fitted through (0,0)'}, 'Location', 'Northwest')
hold off
grid
axis([0 max(x) 0 max(y)])

  1 Comment

Jeff Miller
Jeff Miller on 18 Apr 2019
Your stated assumption is only valid if the model includes an intercept. When you force the line to go through (0,0), the residuals no longer have to sum to zero. You might get a bit more information by comparing the following dlm00 and dlmfree:
dlm00 = fitlm(x,y,'Intercept',false);
yfit00 = x(:)*dlm00.Coefficients{1,1};
sum00 = sum(dlm00.Residuals.Raw);
dlmfree = fitlm(x,y);
yfitfree = x(:)*dlmfree.Coefficients{2,1} + dlmfree.Coefficients{1,1};
sumfree = sum(dlmfree.Residuals.Raw);

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 18 Apr 2019
See a solution: here by Star Strider

  0 Comments

Sign in to comment.

Sign in to answer this question.