06.09.2021 Views

First Semester in Numerical Analysis with Julia, 2020a

First Semester in Numerical Analysis with Julia, 2020a

First Semester in Numerical Analysis with Julia, 2020a

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 2. SOLUTIONS OF EQUATIONS: ROOT-FINDING 75<br />

3. Muller’s method converges for a variety of start<strong>in</strong>g values even though pathological<br />

examples that do not yield convergence can be found (for example, when the three<br />

start<strong>in</strong>g values fall on a l<strong>in</strong>e).<br />

<strong>Julia</strong> code for Muller’s method<br />

The follow<strong>in</strong>g <strong>Julia</strong> code takes <strong>in</strong>itial guesses p 0 ,p 1 ,p 2 (written as pzero, pone, ptwo <strong>in</strong> the<br />

code), computes the coefficients a, b, c from Equation (2.14), and sets the root p 3 to p. Itthen<br />

updates the three <strong>in</strong>itial guesses as the last three iterates, and cont<strong>in</strong>ues until the stopp<strong>in</strong>g<br />

criterion is satisfied.<br />

We need to compute the square root, and the absolute value, of possibly complex numbers<br />

<strong>in</strong> Equations (2.15) and(2.16). The <strong>Julia</strong> function for the square root of a possibly complex<br />

number z is Complex(z) 0.5 , and its absolute value is abs(z).<br />

In [1]: function muller(f::Function,pzero,pone,ptwo,eps,N)<br />

n=1<br />

p=0<br />

while n

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!