How to solve for the first five values only?
10 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Star Strider
on 9 Feb 2017
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 Comments
Star Strider
on 9 Feb 2017
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.
See Also
Categories
Find more on Spline Postprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!