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 63<br />

In [3]: x=range(-2,2,length=1000)<br />

y=map(x->x^5+2*x^3-5*x-2,x)<br />

ax = gca()<br />

ax.sp<strong>in</strong>es["bottom"].set_position("center")<br />

ax.sp<strong>in</strong>es["left"].set_position("center")<br />

ax.sp<strong>in</strong>es["top"].set_position("center")<br />

ax.sp<strong>in</strong>es["right"].set_position("center")<br />

ylim([-40,40])<br />

plot(x,y);<br />

The derivative is f ′ =5x 4 +6x 2 − 5, we set p<strong>in</strong> =1,eps= ɛ =10 −4 ,andN =20,<br />

<strong>in</strong> the code.<br />

In [4]: newton(x -> x^5+2x^3-5x-2,x->5x^4+6x^2-5,1,10^(-4),20)<br />

p is 1.3196411672093726 and the iteration number is 6<br />

Recall that the bisection method required 16 iterations to approximate the root <strong>in</strong><br />

[0, 2] as p =1.31967. (However, the stopp<strong>in</strong>g criterion used <strong>in</strong> bisection and Newton’s<br />

methods are slightly different.) 1.3196 is the rightmost root <strong>in</strong> the plot. But there are<br />

other roots of the function. Let’s run the code <strong>with</strong> p<strong>in</strong> = 0.

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

Saved successfully!

Ooh no, something went wrong!