Monday, June 21, 2010

Partial differential equations in MATLAB

function Solve_PDE
%******************************************
% Author: Jiansen Lu, Date:
% Purpose: A example to solve PDE in MATLAB
%reference: Partial Differential Equations in MATLAB
% Slove dv/dt=(0.1+0.2cost) d/dx(dv/dx) for x between 0 and 1
%v(x, 0) = 2*x-2*x*x, v(0,t)=0 and v(1,t)=0
%**************************************************
%PDE1: MATLAB script M-file that solves and plots
%solutions to the PDE stored in eqn1.m
%solutions to the PDE stored in eqn1.m
m = 0;
%NOTE: m=0 specifies no symmetry in the problem. Taking
%m=1 specifies cylindrical symmetry, while m=2 specifies
%spherical symmetry.
%%Define the solution mesh

x = linspace(0,1,20);
t = linspace(0,1,10);
%Solve the PDE
u = pdepe(m,@eqn1,@initial1,@bc1,x,t)
%first is t, second is x, check middle point
u(10,10)
% check if answer reasonalbe
check1=u(1,:)-initial1(x)
check2=u(:,1)
check3=u(:,20)


function [c,b,s] = eqn1(x,t,u,DuDx)
%EQN1: MATLAB function M-file that specifies
%a PDE in time and one space dimension.
c = 1/(0.1+0.2*cos(t));
b = DuDx;
s = 0;


function [pl,ql,pr,qr] = bc1(xl,ul,xr,ur,t)
%BC1: MATLAB function M-file that specifies boundary conditions
%for a PDE in time and one space dimension.
pl = ul;
ql = 0;
pr = ur;
qr = 0;

function value = initial1(x)
%INITIAL1: MATLAB function M-file that specifies the initial condition
%for a PDE in time and one space dimension.
value = 2.*x-2.*x.*x;

No comments:

Post a Comment