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 3. INTERPOLATION 117<br />

z f(z) 1st diff 2nd diff 3rd diff 4th diff 5th diff<br />

z 0 = −1.5 0.071<br />

f ′ (z 0 )=1<br />

−0.032−1<br />

z 1 = −1.5 0.071<br />

1.6+1.5 = −0.33<br />

f[z 1 ,z 2 ]=−0.032 0.0065<br />

−1+0.032<br />

z 2 =1.6 -0.029<br />

1.6+1.5<br />

= −0.31 0.015<br />

f ′ (z 2 )=−1 0.10 -0.005<br />

0.0055+1<br />

z 3 =1.6 -0.029<br />

4.7−1.6<br />

=0.32 -0.016<br />

f[z 3 ,z 4 ]=0.0055 0<br />

1−0.0055<br />

z 4 =4.7 -0.012<br />

4.7−1.6 =0.32<br />

f ′ (z 4 )=1<br />

z 5 =4.7 -0.012<br />

Therefore, the Hermite polynomial is:<br />

H 5 (x) =0.071 + 1(x +1.5)−0.33(x +1.5) 2 + 0.0065(x +1.5) 2 (x − 1.6)+<br />

+ 0.015(x +1.5) 2 (x − 1.6) 2 −0.005(x +1.5) 2 (x − 1.6) 2 (x − 4.7).<br />

<strong>Julia</strong> code for comput<strong>in</strong>g Hermite <strong>in</strong>terpolat<strong>in</strong>g polynomial<br />

In [1]: us<strong>in</strong>g PyPlot<br />

The follow<strong>in</strong>g function hdiff computes the divided differences needed for Hermite<br />

<strong>in</strong>terpolation. It is based on the function diff for comput<strong>in</strong>g divided differences for<br />

Newton <strong>in</strong>terpolation. The <strong>in</strong>puts to hdiff are the x-coord<strong>in</strong>ates, the y-coord<strong>in</strong>ates,<br />

and the derivatives yprime.<br />

In [2]: function hdiff(x::Array,y::Array,yprime::Array)<br />

m=length(x) # here m is the number of data po<strong>in</strong>ts. Note n=m-1<br />

#and 2n+1=2m-1<br />

l=2m<br />

z=Array{Float64}(undef,l)<br />

a=Array{Float64}(undef,l)<br />

for i <strong>in</strong> 1:m<br />

z[2i-1]=x[i]<br />

z[2i]=x[i]

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

Saved successfully!

Ooh no, something went wrong!