How to plot point x on a line

조회 수: 76 (최근 30일)
Anonymous Learner
Anonymous Learner 2015년 5월 2일
댓글: Anonymous Learner 2015년 5월 2일
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
Anonymous Learner
Anonymous Learner 2015년 5월 2일
thanks a lot for your help that's what i was looking for

댓글을 달려면 로그인하십시오.

추가 답변 (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에서 Graphics Performance에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by