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_ex2(n,dom, kind,dt,K)
function [x,t,solnum]=pde_nonlin_ex2(n,dom, kind,dt,K)
% Example: u_t=0.01 u_xx +u-u^3, x in [-1,1],t>0, (Allen-Cohn equation)
%          u(x,0)=0.13+0.43sin(-3 pi/2 x), u(-1,t)=-1, u(1,t)=1
% From: N. L. Trefethen, Spectral Methods in MATLAB, SIAM 2000
% Remark: uses backward Euler / forward Euler method
% call: [x,t,solnum]=pde_nonlin_ex2(128,[-1,1],2,0.1,250);
%
x=pd(n,dom,kind);D=deriv(n,dom);T=cpv(n,dom,dom);
myDElin;A=speye(n)-dt*A;myINIT;uo=x2t(f,kind);solnum(:,1)=f;sol(:,1)=uo;
h = waitbar(0,'Please wait...');t=zeros(K+1,1);
for k=2:K+1
    waitbar((k-1)/K,h);
    myDEnonlin;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
    A=0.01*D^2+speye(n);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myDEnonlin
    b=uo-dt*x2t((t2x(uo,kind).^3),kind);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myINIT
    f=0.13+0.43*sin(-3*pi/2*x);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myBC
    A(end-1,:)=T(1,:);A(end,:)=T(2,:);
    b(n-1)=-1;b(n)=1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
    figure(1)
    p=plot(x,f,'EraseMode','none');grid;xlabel('x');ylabel('u(x,t)');
    axis([-1.2 1.2 -1.2 1.2]);title('t= 0');hold on; pause(1);
    for j=2:K+1
        set(p,'color','w');
        set(p,'Ydata',solnum(:,j),'color','b');
        title(['t= ',num2str(t(j))]);hold on;pause(0.1);
    end
    figure(2);mesh(x,t,solnum');xlabel x;ylabel t;zlabel u(x,t);
    title('The solution');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

Contact us