How to solve for the first five values only?

10 Ansichten (letzte 30 Tage)
Justin Bruh
Justin Bruh am 9 Feb. 2017
Kommentiert: Star Strider am 9 Feb. 2017
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.

Antworten (1)

Star Strider
Star Strider am 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 Kommentare
Justin Bruh
Justin Bruh am 9 Feb. 2017
That gives me exactly what I want. Can you give some explanation to what each line is doing? Thanks.
Star Strider
Star Strider am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Spline Postprocessing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by