How to get a continuous line through two scatter plots
Show older comments
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
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
Adam Danz
on 19 Apr 2020
"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.
Categories
Find more on Scatter Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!