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

Comput<strong>in</strong>g Chebyshev polynomials<br />

The follow<strong>in</strong>g function implements the recursion Chebyshev polynomials satisfy:<br />

T n+1 (x) =2xT n (x) − T n−1 (x), for n =1, 2, ..., <strong>with</strong> T 0 (x) =1and T 1 (x) =x. Note that<br />

<strong>in</strong> the code the <strong>in</strong>dex is shifted from n +1to n.<br />

In [11]: function cheb(x,n)<br />

n==0 && return 1<br />

n==1 && return x<br />

2x*cheb(x,n-1)-cheb(x,n-2)<br />

end<br />

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

Here is a plot of the first five Chebyshev polynomials:<br />

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

chebzero=map(x->cheb(x,0),xaxis)<br />

chebone=map(x->cheb(x,1),xaxis)<br />

chebtwo=map(x->cheb(x,2),xaxis)<br />

chebthree=map(x->cheb(x,3),xaxis)<br />

chebfour=map(x->cheb(x,4),xaxis)<br />

plot(xaxis,chebzero,label=L"T_0(x)")<br />

plot(xaxis,chebone,label=L"T_1(x)")<br />

plot(xaxis,chebtwo,label=L"T_2(x)")<br />

plot(xaxis,chebthree,label=L"T_3(x)")<br />

plot(xaxis,chebfour,label=L"T_4(x)")<br />

legend(loc="lower right");

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

Saved successfully!

Ooh no, something went wrong!