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

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

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

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

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

In the follow<strong>in</strong>g, we compare second degree least squares polynomial approximations<br />

for f(x) =e x2 . Compare how good the Legendre and Chebyshev polynomial<br />

approximations are <strong>in</strong> the mid<strong>in</strong>terval and toward the endpo<strong>in</strong>ts.<br />

In [19]: f(x)=e^(x^2)<br />

xaxis=-1:1/100:1<br />

polyChebCoeff(x->f(x),2)<br />

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

polyLegCoeff(x->f(x),2)<br />

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

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

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

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

legend(loc="upper center");

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

Saved successfully!

Ooh no, something went wrong!