Plotting graph with only one point (x,y) and slope
Show older comments
Here is the equation of the curve:
(y2-y1)=m(x2-x1)+c
I have the value of x1, y1, y2 and the slope, and I am trying to get the value of x2 from the graph which correspond to y2, in order to solve for c. How should I write the code to plot the graph on matlab?
Answers (1)
Star Strider
on 15 Oct 2020
A bit of algebra (courtesy of the Symbolic Math Toolbox to prevent stupid errors, that I will let you re-derive if you wish) makes the calculation straightforward:
x1 = rand;
y1 = rand;
y2 = rand;
m = rand;
B = [(y2 - y1 + m*x1)/m; y1 - m*x1];
figure
plot([x1 B(1)], [y1, y2], '-b')
hold on
plot(B(1), y2, 'p')
plot(0, B(2), 'p')
hold off
grid
axis([0 max(xlim)*1.1 0 max(ylim)*1.1])
text(0.3, 0.1, sprintf('$Calculated Slope\\ \\ \\frac{y2-y1}{x2-x1} = %.3f$', (y2-y1)/(B(1)-x1)), 'Interpreter','latex')
text(B(1), y2, '\leftarrow x_2', 'HorizontalAlignment','left', 'VerticalAlignment','middle')
text(0, B(2), '\leftarrow c', 'HorizontalAlignment','left', 'VerticalAlignment','middle')
Obviously use your own values for the variables.
Categories
Find more on Line 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!