MATLAB Answers

Finding points with same y-value on a graph.

조회 수: 2(최근 30일)
Arthur Schrock
Arthur Schrock 23 Feb 2020
편집: Star Strider 23 Feb 2020
I have a set of numbers for x and y. The graph increses and decreses with a max point. I have a new value for y, which crosses the graph twice and want to know the corresponding x values.
x=2,4,7,9,10,14
y=5,10,15,14,12,3
new y value = 12.5

  댓글 수: 0

로그인 to comment.

답변(1개)

Star Strider
Star Strider 23 Feb 2020
편집: Star Strider 23 Feb 2020
Try this:
x = [2,4,7,9,10,14];
y = [5,10,15,14,12,3];
new_y_value = 12.5
[~,idx] = max(y);
newx(1) = interp1(y(1:idx), x(1:idx), new_y_value);
newx(2) = interp1(y(idx:end), x(idx:end), new_y_value);
figure
plot(x,y,'-r')
hold on
plot(newx, [1 1]*new_y_value, 'pg', 'MarkerSize',10, 'MArkerFaceColor','g')
hold off
grid
It is necessary to do this for each value of ‘newx’, splitting the vectors at ‘idx’ correspoinding to the maximum ‘y’-value, since interp1 can only work with monotonically-changing vectors for the independent variable (here: ‘y’).
EDIT —
Added plot image —

  댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.

태그


Translated by