How to plot point x on a line

22 次查看(过去 30 天)
i am new in matlab i want to highlight point intersection point on a line in graph i can plot a point but not able to plot the point on line my code is here:
function[ x y]= test (x1,y1,x2,y2)
y=((-x1)+y1*(y2-y1))/(x2-x1);
x=((-y1*(x1-x2))/(y1-y2))+x1;
plot(x,y,'ro')
end

采纳的回答

Nobel Mondal
Nobel Mondal 2015-5-2
编辑:Nobel Mondal 2015-5-2
I agree that the question is a bit unclear.
If you're wondering how to find and highlight the intersection point(s) between two curves this below code snippet might be helpful:
x1 = [1 2 3 4]; y1 = [0 2 4 6];
x2 = [1 2 3 4]; y2 = [6 4 2 0];
% find intersection
[x_intsect, y_intsect] = polyxpoly(x1, y1, x2, y2)
% plot everything on a single figure
figure;
hold on;
plot(x1, y1, 'k-', x2, y2, 'r-');
plot(x_intsect, y_intsect, 'bo');
hold off;

更多回答(1 个)

pfb
pfb 2015-5-2
编辑:pfb 2015-5-2
Not sure what you are asking, nor what your function is expected to do.
I guess x1,x2,y1,y2 are scalars (otherwise you're likely to get an error).
Your function calculates x and y based on those inputs, and plots it in a graph (by the way, since x and y are outputs, you can plot them outside your function).
Unless you get some sort of error, that should work.
Perhaps you are complaining that, after plotting a line, your function seems to delete it and plot only the point at (x,y).
If this is the case, you just need to hold the plots.
Matlab replaces the old plot with the new one, if you do not hold. After plotting the first line, write
hold;
or
hold on;
Take a look at the documentation of hold by typing "doc hold" in the command window.
A concrete example
% plot first line through points (0,1), (1,0)
plot([0 1],[1 0],'r');
% hold it
hold;
% plot second line through points (0,0.2) (1,1.2)
plot([0 1],[0.2 1.2],'r');
% plot a point at their intersection
plot(0.4,0.6,'.');

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by