How do I plot and return the values of multiple intersections between a function and zero?

Hello,
I need to find the intersection between the det_a function and zero, return the values, and plot the intersections. Here's what I did so far
x = linspace(0,18,1000);
det_a = sin(x).*cosh(x) - cos(x).*sinh(x);
zr = zeros(size(x));
figure
plot(x,det_a,x,zr,'--')
grid on
ylim([-10 10])
xlabel('\betal')
The plot can be seen here:
I haven't found any easy method of doing this, I've tried using the find and intersect functions but they only return one value. Any help would be appreciated, thanks in advance!

 採用された回答

Here's one possibility (though you get repeated results for the roots!):
det_a = @(x) sin(x).*cosh(x) - cos(x).*sinh(x);
x=0:18;
zr = zeros(1,numel(x));
for i = 1:numel(x)
zr(i) = fzero(det_a, x(i));
end
disp(zr)
0 -0.0000 0.0000 3.9266 3.9266 3.9266 7.0686 7.0686 7.0686 10.2102 10.2102 10.2102 13.3518 13.3518 13.3518 13.3518 16.4934 16.4934 16.4934
figure
xx = linspace(0,18,100);
plot(xx,det_a(xx),zr,0,'o')
grid on
ylim([-10 10])
xlabel('\betal')

6 件のコメント

Gerry Oscar
Gerry Oscar 2021 年 10 月 20 日
Works for me, thanks!
Gerry Oscar
Gerry Oscar 2021 年 10 月 20 日
If I may enquire again, how would I go by doing the same thing except trying to find the intersections at different values of y, for example all intersections at y = -2
Alan Stevens
Alan Stevens 2021 年 10 月 20 日
Simply add 2 to the end of the det_a function definition.
Gerry Oscar
Gerry Oscar 2021 年 10 月 20 日
Hmm.. not sure what you mean by that.
fzero finds the value of x that makes the function det_a equal zero. So if it makes det_a+2 equal zero, then det_a must be -2.
Something like this:
det_a = @(x) sin(x).*cosh(x) - cos(x).*sinh(x);
det_b = @(x) det_a(x) + 2;
x=0:18;
zr = zeros(1,numel(x));
for i = 1:numel(x)
zr(i) = fzero(det_b, x(i));
end
disp(zr)
-1.4526 -1.4526 3.9795 3.9795 3.9795 3.9795 7.0662 7.0662 7.0662 10.2103 10.2103 10.2103 13.3518 13.3518 13.3518 13.3518 16.4934 16.4934 16.4934
figure
xx = linspace(0,18,100);
plot(xx,det_a(xx),zr,-2,'o')
grid on
ylim([-10 10])
xlabel('\betal')
Gerry Oscar
Gerry Oscar 2021 年 10 月 21 日
Riight, thanks!

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

製品

リリース

R2019a

タグ

質問済み:

2021 年 10 月 20 日

コメント済み:

2021 年 10 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by