フィルターのクリア

Finding multiple x values in plot for given y

7 ビュー (過去 30 日間)
Geroge Papas
Geroge Papas 2020 年 12 月 17 日
回答済み: Geroge Papas 2020 年 12 月 17 日
I have to find the values for x when y=0 but i cant seem to make it work. I have tried with interp1 but it seems that it gives me an answer of x=2.3490 which is clearly wrong,since there are 2 points in the plot where y=0. Here's the code:
x = [0:0.001:pi]
f = exp(x/4).*sin(x)-1;
plot(x,f)
xlabel('Axis x');
ylabel('Axis y');
newLim = get(gca,'XLim');
newx = linspace(newLim(1), newLim(2), 30);
set(gca,'XTick', newx);
% what I tried:
yi = 0 ;
xi = interp1(f,x,yi) ;
hold on
semilogy(xi,yi,'*r')
  1 件のコメント
Geroge Papas
Geroge Papas 2020 年 12 月 17 日
Here is the plot that the code gives:

サインインしてコメントする。

採用された回答

Geroge Papas
Geroge Papas 2020 年 12 月 17 日
Welp I solved it! I used zero-crossings estimation code and got the index vectors i needed to find the values of x when y aproximates 0. Here is the finished code and graph:
x = [0:0.001:pi]
f = exp(x/4).*sin(x)-1;
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0);
zx = zci(f);
figure(1)
plot(x, f, '-r')
hold on
plot(x(zx), f(zx), 'bp')
hold off
grid
legend('f(x)', 'Approximate Zero-Crossings')
%x1=0.9180
%x2=2.5900

その他の回答 (1 件)

Daniel Pollard
Daniel Pollard 2020 年 12 月 17 日
I think you want fsolve. Give it a function handle and an initial starting point, and it'll tell you (approximately) what x gives f(x)=0.

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by