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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 3. INTERPOLATION 128<br />

end<br />

for i <strong>in</strong> 2:n<br />

u[i]=3*(a[i+1]-a[i])/h[i]-3*(a[i]-a[i-1])/h[i-1]<br />

end<br />

s=Array{Float64}(undef,m)<br />

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

t=Array{Float64}(undef,n)<br />

s[1]=1<br />

z[1]=0<br />

t[1]=0<br />

for i <strong>in</strong> 2:n<br />

s[i]=2*(x[i+1]-x[i-1])-h[i-1]*t[i-1]<br />

t[i]=h[i]/s[i]<br />

z[i]=(u[i]-h[i-1]*z[i-1])/s[i]<br />

end<br />

s[m]=1<br />

z[m]=0<br />

c[m]=0<br />

for i <strong>in</strong> reverse(1:n)<br />

c[i]=z[i]-t[i]*c[i+1]<br />

b[i]=(a[i+1]-a[i])/h[i]-h[i]*(c[i+1]+2*c[i])/3<br />

d[i]=(c[i+1]-c[i])/(3*h[i])<br />

end<br />

Out[2]: CubicNatural (generic function <strong>with</strong> 1 method)<br />

Once the matrix equation is solved, and the coefficients of the cubic polynomials are<br />

computed by CubicNatural, the next step is to evaluate the spl<strong>in</strong>e at a given value.<br />

This is done by the follow<strong>in</strong>g function CubicNaturalEval. The <strong>in</strong>puts are the value<br />

at which the spl<strong>in</strong>e is evaluated, w, and the x-coord<strong>in</strong>ates of the data. The function<br />

first f<strong>in</strong>ds the <strong>in</strong>terval [x i ,x i+1 ],i =1, ..., m − 1, wbelongs to, and then evaluates the<br />

spl<strong>in</strong>e at w us<strong>in</strong>g the correspond<strong>in</strong>g cubic polynomial. Note that <strong>in</strong> the previous section<br />

the data and the <strong>in</strong>tervals are counted start<strong>in</strong>g at 0, so the formulas used <strong>in</strong> the <strong>Julia</strong><br />

code do not exactly match the formulas given earlier.<br />

In [3]: function CubicNaturalEval(w,x::Array)

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

Saved successfully!

Ooh no, something went wrong!