필터 지우기
필터 지우기

How to find x value for a known Y in array?

조회 수: 4 (최근 30일)
Zeyad Elreedy
Zeyad Elreedy 2022년 12월 11일
댓글: Zeyad Elreedy 2022년 12월 12일
I have x and y arrays for a sine wave. I am trying to find the x value for when y is equal to 0, since the y array does not contain a value of 0. How can I do so?

채택된 답변

Torsten
Torsten 2022년 12월 11일
편집: Torsten 2022년 12월 11일
x = 0.1:0.1:4*pi-0.1;
y = sin(x);
i = find(y(1:end-1).*y(2:end) <= 0)
i = 1×3
31 62 94
x_root = x(i)-y(i).*(x(i+1)-x(i))./(y(i+1)-y(i))
x_root = 1×3
3.1416 6.2832 9.4248
[pi,2*pi,3*pi]
ans = 1×3
3.1416 6.2832 9.4248
  댓글 수: 4
Torsten
Torsten 2022년 12월 11일
편집: Torsten 2022년 12월 11일
I determine x(i) and x(i+1) between which y changes sign. Then I approximate the zero between x(i) and x(i+1) by linear interpolating between the points (x(i),y(i)) and (x(i+1),y(i+1)) and determining the x_root value as the x-value where this line crosses the x-axis.
So interp1 is implicitly used in the line
x_root = x(i)-y(i).*(x(i+1)-x(i))./(y(i+1)-y(i))
Zeyad Elreedy
Zeyad Elreedy 2022년 12월 12일
I understand now. Thanks a lot!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by