How to find the intersection of linear regression model and yline

조회 수: 3 (최근 30일)
Joy
Joy 2024년 6월 12일
답변: Star Strider 2024년 6월 12일
Say I have a table with the following values:
T(:,1) = [0 1 2 3 4 5]
T(:,2) = [0 1 2 3 4 5]
I want to be able to predict values so I fit a simple linear regression model to it. I also want to extract the X value for when the lin reg model intersects a value Y = 4.3. Is there a way to find the coordinates of this intersection?

채택된 답변

Star Strider
Star Strider 2024년 6월 12일
Use the interp1 function for this —
T(:,1) = [0 1 2 3 4 5].';
T(:,2) = [0 1 2 3 4 5].';
x = T(:,1);
y = T(:,2);
p = polyfit(x, y, 1)
p = 1x2
1.0000 0.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
f = polyval(p, x);
ylv = 4.3;
xv = interp1(f, x, ylv)
xv = 4.3000
figure
plot(x, y, '.', 'DisplayName','Data')
hold on
plot(x, f, '-r', 'DisplayName','Regression')
plot(xv, ylv, 'ms', 'DisplayName','Intersection Of ‘yline’')
hold off
yline(ylv, '--k', 'DisplayName','yline')
xlabel('x')
ylabel('y')
legend('Location','best')
.

추가 답변 (1개)

John D'Errico
John D'Errico 2024년 6월 12일
Pretty boring data.
T(:,1) = [0 1 2 3 4 5];
T(:,2) = [0 1 2 3 4 5];
P = polyfit(T(:,1),T(:,2),1)
P = 1x2
1.0000 0.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
a = P(1)
a = 1.0000
b = P(2)
b = 3.1322e-16
They are the coefficients of the linear polynomial model. The model is of the form:
Y = a*X + b
So if you want to solve for x, given y, just use algebra.
Y0 = 4.3
Y0 = 4.3000
X0 = (Y0 - b)/a
X0 = 4.3000
Since your data was so simple, the line is just the 45 degree line, with X == Y here.

카테고리

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

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by