How to get a continuous line through two scatter plots

Hi all,
I am trying to get get a continuous line to pass through the two scatter points in my MatLab code. I have currently joined to the two points together but I need the line to continue so I can get a point of intersection with the line and the curve.
Lr=(0:0.02:1.4);
Kr=(1-0.14.*Lr.^2).*(0.3 + 0.7.*exp(-0.65*Lr.^6));
%SteelA
Lr1=0;
Lr2=0.7356;
Kr1=0.01048;
Kr2=0.7333;
plot(Lr,Kr,'LineWidth',3);
legend('Limit');
hold on;
title('Failure Assessment Diagram (FAD) for Steel A');
xlabel('Lr');
ylabel('Kr');
hold on;
scatter(Lr1,Kr1,'*','r')
hold on;
scatter(Lr2,Kr2,'*','g')
hold on;
plot([Lr1 Lr2],[Kr1 Kr2])
grid on;
legend ('R6 Kr curve','Lr(s)=0, Kr(s)(Steel A)', 'Lr(p), Kr(p+s)(Steel A)')

 Accepted Answer

Another approach:
Lr=(0:0.02:1.4);
Kr=(1-0.14.*Lr.^2).*(0.3 + 0.7.*exp(-0.65*Lr.^6));
%SteelA
Lr1=0;
Lr2=0.7356;
Kr1=0.01048;
Kr2=0.7333;
B = [Lr1 1;Lr2 1] \ [Kr1; Kr2]; % Linear Regression
LExt = [Lr(:) ones(size(Lr(:)))] * B; % Extend Line For All ‘Lr’
Lr_int = interp1(LExt - Kr(:), Lr, 0); % ‘Lr’ Value At Intersection
Krfcn = @(Lr) (1-0.14.*Lr.^2).*(0.3 + 0.7.*exp(-0.65*Lr.^6)); % ‘Kr’ Anonymous Function
Kr_int = Krfcn(Lr_int); % ‘Kr’ Value At At Intersection
figure
h(1) = plot(Lr,Kr,'LineWidth',3);
hold on;
title('Failure Assessment Diagram (FAD) for Steel A');
xlabel('Lr');
ylabel('Kr');
h(2) = scatter(Lr1,Kr1,'*','r');
h(3) = scatter(Lr2,Kr2,'*','g');
plot([Lr1 Lr2],[Kr1 Kr2])
h(4) = plot([Lr2 Lr_int], [Kr2 Kr_int], ':r');
h(5) = plot(Lr_int, Kr_int,'pm', 'MarkerFaceColor','m', 'MarkerSize',8);
grid on;
legend (h, 'R6 Kr curve','Lr(s)=0, Kr(s)(Steel A)', 'Lr(p), Kr(p+s)(Steel A)', 'Extended Line', 'Intersection', 'Location','S')
EDIT — (19 Apr 2020 at 18:21)
Added plot image:
.

More Answers (1)

Compute the slope between the two points using the slope equation,
m = (y(2)-y(1))/(x(2)-x(1));
Compute the y-intercept by filling in the x, y, and slope value in the following equation. Choose one of the points to get the x and y values.
b = -m*x(1) + y(1);
Use refline(m,b) to draw a reference line that extends to the limits of your plot. Set xlim() and ylim() prior to creating the reference line.

1 Comment

"I need the line to continue so I can get a point of intersection with the line and the curve"
Note that refline() will create a line that extends across the entire plot. If you'd like the line to end at the intersection with the curve, Star Strider's answer does that. Once you have the slope and the intercept, you have complete control over the start and end points of the line segment.

Sign in to comment.

Tags

Asked:

on 19 Apr 2020

Edited:

on 19 Apr 2020

Community Treasure Hunt

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

Start Hunting!