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,solnum,A,b]=pde_lin_ex1(n,dom,kind,dt,K)
function [x,solnum,A,b]=pde_lin_ex1(n,dom,kind,dt,K)
% Example: E_t=B_x, B_t=E_x, x in [-2,2], t>0 
%          E(-2,t)=E(2,t)=0, E(x,0)=exp(-16x^2),B(x,0)=-exp(-16x^2)
% From: T. Driscoll, MAXWELL'S EQUATIONS, Chebfun examples
% Remark: uses Crank-Nicolson method for time step
% call: [x,solnum]=pde_lin_ex1(128,[-2,2],2,0.025,200);
%
x=pd(n,dom,kind);D=deriv(n,dom);T=cpv(n,dom,dom);I=speye(2*n);
myDE;A=(I-dt*L/2);myINIT;h = waitbar(0,'Please wait...');
uo=reshape(x2t(f,kind),2*n,1);solnum(:,1)=reshape(f,2*n,1);t(1)=0;
for k=1:K
    waitbar((k-1)/K,h);myBC;un=A\((I+dt*L/2)*uo);t(k+1)=k*dt;
    uo=un;solnum(:,k+1)=reshape(t2x(reshape(uo,n,2),kind),2*n,1);
end
close(h);myOUT;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myDE
    % describes the linear spatial part of the pde
    L=[zeros(n),D;D,zeros(n)];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myBC
    % describes the boundary conditions
    A(n,:)=zeros(1,2*n);A(n,1:n)=T(1,:);uo(n)=0;
    A(2*n,:)=zeros(1,2*n);A(2*n,1:n)=T(2,:);uo(2*n)=0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myINIT
    % describes the initial condition in physical form
    f=[exp(-16*x.^2),-exp(-16*x.^2)];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
    % describes the output of the program
    p=plot(x,solnum(1:n,1),'EraseMode','none');axis([-2.1 2.1 -1.1 1.1]);grid;
    title(['t= ',num2str(0)]);xlabel('x');ylabel('E(x,t)');hold on;pause(1);
    for j=2:K+1
        set(p,'color','w');set(p,'Ydata',solnum(1:n,j),'color','b');
        title(['t= ',num2str(t(j))]);hold on;pause(0.1);
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

Contact us