Code covered by the BSD License  

Highlights from
Chebpack

image thumbnail

Chebpack

by

 

15 Jul 2011 (Updated )

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

Ex2pant.m
% Example 2
n=16;q=1/2;dom=[0,2*pi];kind=2;m=24;
x=pd(n,dom,kind);D=deriv(n,dom);X=full(mult(n,dom));P =dev(n,x,dom,q*x);
T=cpv(n,dom,dom);F=4*funm(-X/2,@exp)*funm(X/2,@sin);
A=D^2-F*P;f=zeros(n,1);A(n-1:n,:)=T;f(n-1)=1;f(n)=exp(-dom(2))*cos(dom(2));

% Calculate the solution and the exact error
c=A\f;val=t2x(c,kind);ex=exp(-x).*cos(x);
xnew=linspace(0,2*pi,101);valnew=barycheb(xnew,val,x,kind);
exnew=exp(-xnew).*cos(xnew);er=norm(val-ex,inf);
subplot(2,1,1);semilogy(xnew,abs(valnew-exnew),'-',x,abs(val-ex),'o');
grid;xlabel('t');ylabel('the error');title(['exact error = ',num2str(er)]);

% Estimate the residual
xx=pd(m,dom,kind);DD=deriv(m,dom);XX=full(mult(m,dom));PP =dev(m,xx,dom,q*xx);
TT=cpv(m,dom,dom);FF=4*funm(-XX/2,@exp)*funm(XX/2,@sin);
AA=DD^2-FF*PP;cc=zeros(m,1);cc(1:n)=c;R=AA*cc;
subplot(2,1,2);stem(1:m,R);grid;xlabel('k');ylabel('the residual');

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

Contact us