Linear best fit through specified y-axis intercept
이전 댓글 표시
Essentially I have a few data points grouped closely together and they require a linear best fit line. However, the best fit line needs extrapolating to a specific y-axis intercept. I'm not too sure how to approach this. Using a polyfit I get this result:

The y intercept has to be 1.68. So I presume the gradient has to be modified as well as the intercept to get the correct linear best fit line. How would I go about this?
답변 (2개)
David Hill
2019년 11월 13일
f=fit(x,y,'poly1');
disp(f);%will display the linear model including the y-intercept and slope of the fitted line
Star Strider
2019년 11월 13일
Try this:
x = linspace(0.7, 1.7, 5); % Create Data (Use Your Own ‘x’ & ‘y’)
y = randn(1,5); % Create Data (Use Your Own ‘x’ & ‘y’)
xp = [0 2]; % X-Vector For Plot
B = x(:) \ (y(:)-1.68);
yp = xp(:)*B+1.68;
Bnew = [xp(:) ones(size(xp(:)))] \ (yp(:)); % Calculate New Regression Parameters
figure
plot(x, y, '*')
hold on
plot(xp, yp, '-r')
plot(0, 1.68, '+g')
hold off
grid
xlim([0 2])
text(1, 1.5, sprintf('y = %.2f%+.3f\\cdotx', Bnew(2),Bnew(1)))
The ‘Bnew’ calculation is not absolutely necessary, since it is simply [B; 1.68]. I added it essentially to demonstrate that the new regression on ‘xp’ and ‘yp’ does actually result in the desired parameters.
카테고리
도움말 센터 및 File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!