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 5. APPROXIMATION THEORY 214<br />

Next we plot y = e x together <strong>with</strong> polynomial approximations of degree two and<br />

three us<strong>in</strong>g Chebyshev basis polynomials.<br />

In [17]: xaxis=-1:1/100:1<br />

polyChebCoeff(x->e^x,2)<br />

deg2=map(x->polyCheb(x,2),xaxis)<br />

polyChebCoeff(x->e^x,3)<br />

deg3=map(x->polyCheb(x,3),xaxis)<br />

plot(xaxis,map(x->e^x,xaxis),label=L"e^x")<br />

plot(xaxis,deg2,label="Chebyshev least squares poly of degree 2")<br />

plot(xaxis,deg3,label="Chebyshev least squares poly of degree 3")<br />

legend(loc="upper left");<br />

The cubic Legendre and Chebyshev approximations are difficult to dist<strong>in</strong>guish from<br />

the function itself. Let’s compare the quadratic approximations obta<strong>in</strong>ed by Legendre<br />

and Chebyshev polynomials. Below, you can see visually that Chebyshev does a better<br />

approximation at the end po<strong>in</strong>ts of the <strong>in</strong>terval. Is this expected?<br />

In [18]: xaxis=-1:1/100:1<br />

polyChebCoeff(x->e^x,2)<br />

cheb2=map(x->polyCheb(x,2),xaxis)<br />

polyLegCoeff(x->e^x,2)<br />

leg2=map(x->polyLeg(x,2),xaxis)

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

Saved successfully!

Ooh no, something went wrong!