Code covered by the BSD License

# Chebpack

### Damian Trif (view profile)

15 Jul 2011 (Updated )

The MATLAB package Chebpack solves specific problems for differential or integral equations.

Ex1pant.m
```% Example 1
n=8;mm=8;q=1/2;m=20;
dom=[0,1];kind=2;t=pd(n,dom,kind);D=deriv(n,dom);
Sdev =dev(n,t,dom,q*t);T=cpv(n,0,dom);
s=sin(t);ss=x2t(s,kind);S=fact(ss,mm);
cq=cos(q*t);cqcq=x2t(cq,kind);Cq=fact(cqcq,mm);
sq=sin(q*t);sqsq=x2t(sq,kind);Sq=fact(sqsq,mm);
f=x2t(cos(t)-sin(t).^2,kind);
A=D-S-Cq*Sdev+Sq*Sdev*D;B=A;B(n,:)=T;g=f;g(n)=0;

% Calculate the solution and the exact error
coef=B\g;val=t2x(coef,kind);
tnew=linspace(0,1,101);valnew=barycheb(tnew,val,t,kind);
ex=sin(tnew);er=norm(val-sin(t));
subplot(2,1,1);semilogy(tnew,abs(valnew-ex),'-',t,abs(val-sin(t)),'o');
grid;xlabel('t');ylabel('the error');title(['exact error = ',num2str(er)]);

% Estimate the residual
tt=pd(m,dom,kind);DD=deriv(m,dom);XX=full(mult(m,dom));
SSdev =dev(m,tt,dom,q*tt);TT=cpv(m,0,dom);
SS=funm(XX,@sin);
CqCq=funm(q*XX,@cos);
SqSq=funm(q*XX,@sin);
ff=x2t(cos(tt)-sin(tt).^2,kind);
AA=DD-SS-CqCq*SSdev+SqSq*SSdev*DD;rez=AA*[coef;zeros(m-n,1)]-ff;
subplot(2,1,2);stem(1:m,rez);grid;xlabel('k');ylabel('the residual');

% Estimate the tau error for the solution
AA(m,:)=TT;rez(m)=0;ersol=-AA\rez;
est=norm(t2x(ersol),kind);er;est;
title(['estimated error = ',num2str(est)]);```