How to find x value for a known Y in array?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Zeyad Elreedy
am 11 Dez. 2022
Kommentiert: Zeyad Elreedy
am 12 Dez. 2022
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?
0 Kommentare
Akzeptierte Antwort
Torsten
am 11 Dez. 2022
Bearbeitet: Torsten
am 11 Dez. 2022
x = 0.1:0.1:4*pi-0.1;
y = sin(x);
i = find(y(1:end-1).*y(2:end) <= 0)
x_root = x(i)-y(i).*(x(i+1)-x(i))./(y(i+1)-y(i))
[pi,2*pi,3*pi]
4 Kommentare
Torsten
am 11 Dez. 2022
Bearbeitet: Torsten
am 11 Dez. 2022
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))
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Interpolation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!