How to solve for the first five values only?

1 次查看(过去 30 天)
I have an equation: cos(x)cosh(x)+1=0
There are infinitely many solutions to x but I only want the first five(only positive x's). I've messed around with fnzeros but I can't seem to make it work.

回答(1 个)

Star Strider
Star Strider 2017-2-9
See if this does what you want:
f = @(x) cos(x) .* cosh(x) + 1;
for k1 = 1:500
s(k1) = fzero(f, k1/10);
end
su = unique(round(abs(s), 5));
Output = su(1:5)
Output =
1.8751 4.6941 7.8548 10.996 14.137
  2 个评论
Justin Bruh
Justin Bruh 2017-2-9
That gives me exactly what I want. Can you give some explanation to what each line is doing? Thanks.
Star Strider
Star Strider 2017-2-9
My pleasure.
First, ‘f’ is an anonymous function version of your expression. See the relevant sections of the documentation on Function Basics (link) for details. It is necessary in order to use the fzero function.
Second, the for loop is just a brute-force method of finding the various zero-crossings. Many are duplicates (or nearly so, considering the default tolerances in fzero), so it is necessary to use the round function (here to 5 decimal places) to truncate them to provide actual unique values. (The uniquetol function is an alternate option, and would not require round in the input.)
Third, the ‘Output’ assignment simply selects the first 5 positive values.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Spline Postprocessing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by