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

Then for each po<strong>in</strong>t she eyeballs the x and y-coord<strong>in</strong>ates <strong>with</strong> the help of a graph<br />

paper. The results are displayed <strong>in</strong> the table below.<br />

t 1 2 3 4 5 6 7 8<br />

x 0 0 -0.05 0.1 0.4 0.65 0.7 0.76<br />

y 0 1.25 2.5 1 0.3 0.9 1.5 0<br />

The next step is to fit a cubic spl<strong>in</strong>e to the data (t 1 ,x 1 ), ..., (t 8 ,x 8 ), and another<br />

cubic spl<strong>in</strong>e to the data (t 1 ,y 1 ), ..., (t 8 ,y 8 ). Let’s call these spl<strong>in</strong>es xspl<strong>in</strong>e(t), yspl<strong>in</strong>e(t),<br />

respectively, s<strong>in</strong>ce they represent the x and y-coord<strong>in</strong>ates as functions of the parameter t.<br />

Plott<strong>in</strong>g xspl<strong>in</strong>e(t), yspl<strong>in</strong>e(t) will produce the letter NUH, as we can see <strong>in</strong> the follow<strong>in</strong>g<br />

<strong>Julia</strong> codes.<br />

<strong>First</strong>, load the PyPlot package, and copy and evaluate the functions CubicNatural<br />

and CubicNaturalEval that we discussed earlier. Here is the letter NUH, obta<strong>in</strong>ed by<br />

spl<strong>in</strong>e <strong>in</strong>terpolation:<br />

In [4]: t=[1,2,3,4,5,6,7,8]<br />

x=[0,0,-0.05,0.1,0.4,0.65,0.7,0.76]<br />

y=[0,1.25,2.5,1,0.3,0.9,1.5,0]<br />

taxis=1:1/100:8<br />

CubicNatural(t,x)<br />

xspl<strong>in</strong>e=map(z->CubicNaturalEval(z,t),taxis)<br />

CubicNatural(t,y)<br />

yspl<strong>in</strong>e=map(z->CubicNaturalEval(z,t),taxis)<br />

plot(xspl<strong>in</strong>e,yspl<strong>in</strong>e,l<strong>in</strong>ewidth=5);

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

Saved successfully!

Ooh no, something went wrong!