How to solve for the first five values only?
6 次查看(过去 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.
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Spline Postprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!