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.

[x,t,solnum]=pde_nonlin_ex1(n,dom, kind,dt,K)
function [x,t,solnum]=pde_nonlin_ex1(n,dom, kind,dt,K)
% Example: u_t=niu u_xx +u^3, x in [0,1],t>0, niu=0.01/pi
%          u(x,0)=1+0.25exp(-500(x-0.25)^2)+exp(-500(x-0.75)^2), 
%          u(0,t)=u(1,t)=0
% From: J. M. Keiser, Wavelet based approach to numerical 
% solution of nonlinear partial differential equations,Thesis, Univ. Colorado, 1995
% Remark: uses Crank-Nicolson / forward Euler method
% call: [x,t,solnum]=pde_nonlin_ex1(128,[0,1],2,0.001,170);
%
x=pd(n,dom,kind);D=deriv(n,dom);T=cpv(n,dom,dom);L=zeros(n);niu=0.01/pi;
myDElin;A=speye(n)-dt/2*L;myINIT;uo=x2t(f,kind);solnum(:,1)=f;sol(:,1)=uo;
h = waitbar(0,'Please wait...');t=zeros(K+1);
for k=2:K+1
    waitbar((k-1)/K,h);
    myDEnonlin;b=(speye(n)+dt/2*L)*uo+dt*nl;
    myBC;un=A\b;t(k)=(k-1)*dt;
    uo=un;solnum(:,k)=t2x(uo,kind);sol(:,k)=uo;
end
close(h);myOUT;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myDElin
    L=niu*(D^2);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myDEnonlin
    nl=x2t((t2x(uo,kind).^3),kind);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myBC   
    A(end-1,:)=T(1,:);A(end,:)=T(2,:);
    b(n-1)=0;b(n)=0; 
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myINIT
    f=1+0.25*exp(-500*(x-0.25).^2)+exp(-500*(x-0.75).^2);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
    p=plot(x,f,'EraseMode','none');grid;xlabel('x');ylabel('u(x,t)');
    axis([-0.2 1.2 -0.2 30]);title('t= 0');hold on; pause(1);
    for j=1:K
        set(p,'color','w');
        set(p,'Ydata',solnum(:,j+1),'color','b');
        title(['t= ',num2str(t(j+1))]);hold on;pause(0.1);
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

Contact us