Solve Ode Pde Using Matlab
Solve Ode Pde Using Matlab
Farida Mosally
Mathematics Department
2014
Outline
1. Ordinary Differential Equations (ode)
1.1 Analytic Solutions
1.2 Numerical Solutions
Numerical Solutions
MATLAB has a number of tools for numerically solving ordinary
differential equations.. In the following table we display some of them.
ode initial value problem solvers
Solver Method used Order of When to Use
Accuracy
ode45 Runge-Kutta (4,5) formula Medium Most of the time. This should be th
first solver you try.
ode23 Runge-Kutta (2, 3) formula Low For problems with crude error
tolerances or for solving moderatel
stiff problems.
ode113 Adams-Bashforth-Moulton solver Low to high For problems with stringent error
tolerances or for solving
computationally intensive problem
ode15s Solver based on the numerical Low to If ode45 is slow because the
differentiation formulas medium problem is stiff.
ode23s Solver based on a modified Rosenbrock Low If using crude error tolerances to
formula of order 2 solve stiff systems and the mass
matrix is constant.
Defining an ODE function
Defining an ODE function
sol = solver(odefun,[t
,[t0 tf],y0,options)
Example 1
1.9
1.8
1.7
1.6
1.5
1.4
1.3
1.2
1.1
1
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5
First Order Equations with M-files
M
Example 2
0.4
y0 = [0; 1; 1];
0.2
-0.2
ode45(@f,tspan,y0);
-0.4
-0.6
% ------------------------------
-0.8
function dydt = f(t,y)
-1
dydt = [ y(2)*y(3) 0 2 4 6 8 10 12
-y(1)*y(3)
-0.51*y(1)*y(2) ];
… Solving systems of first-order
first ODEs
To define each curve
function example3
tspan = [0 12];
1
y0 = [0; 1; 1]; 0.8
0.6
% Solve the problem using ode45 0.4
[t,y] = ode45(@f,tspan,y0); 0.2
plot(t,y(:,1),':r',t,y(:,2),'-.',t,y(:,
(:,3)) -0.2
legend('y_1','y_2','y_3',3) -0.4
% ------------------------------ -0.6 y1
y2
function dydt = f(t,y) -0.8
y3
dydt = [ y(2)*y(3) -1
0 2 4 6 8 10 12
-y(1)*y(3)
-0.51*y(1)*y(2) ];
Boundary Value Problems
sol = bvp4c(odefun,bcfun,solinit
odefun,bcfun,solinit)
Delay Differential Equations
Functions
sol = dde23(ddefun,lags,history,tspan
ddefun,lags,history,tspan)
Example 4
for t ≤ 0.
function ddex1
sol = dde23(@ddex1de,[1, 0.2],@ddex1hist,[
hist,[0, 5]);
figure;
plot(sol.x,sol.y)
title('An example of Wille''
'' and Baker.');
Baker.'
xlabel('time t');
ylabel('solution y');
function s = ddex1hist(t)
% Constant history function for DDEX1.
s = ones(3,1);
pdepe
Solve initial-boundary value problems for parabolic-elliptic
elliptic PDEs in 1-D
Syntax
sol = pdepe(m,pdefun,icfun,bcfun,xmesh,tspan)
sol = pdepe(m,pdefun,icfun,bcfun,xmesh,tspan,OPTIONS
m,pdefun,icfun,bcfun,xmesh,tspan,OPTIONS)
[sol,tsol,sole,te,ie] = pdepe(m,pdefun,icfun,bcfun,xmesh,tspan,OPTIONS
m,pdefun,icfun,bcfun,xmesh,tspan,OPTIONS)
Arguments
sol = pdepe(m,pdefun,icfun,bcfun,xmesh,tspan
m,pdefun,icfun,bcfun,xmesh,tspan)
m A parameter corresponding to the symmetry of the problem. m can be slab = 0,
cylindrical =1, or spherical = 2.
pdefun A handle to a function that defines the components of the PDE.
cfun A handle to a function that defines the initial conditions.
bcfun A handle to a function that defines the boundary conditions.
xmesh A vector [x0, x1, ..., xn], x0 < x1 < ... < xn..
span A vector [t0, t1, ..., tf] , t0 < t1 < ... < tf.
options Some OPTIONS of the underlying ODE solver are available, See odeset for details.
Description
PDE
IC
BC
Single PDE
xample 1.
his example illustrates the straightforward formulation, computation, and plotting of the
olution of a single PDE.
nction pdex1
= 0;
function [c,f,s] = pdecfs(x,t,u,DuDx)
= linspace(0,1,20); c = pi^2;
= linspace(0,2,5); f = DuDx;
s = 0;
l = pdepe(m,@pdecfs,@ic,@bc,x,t);
% --------------------------------
= sol(:,:,1);
function u0 = ic(x)
u0 = sin(pi*x);
0.12
0.1
0.06
u(x
0.04
0.02
-0.02
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Distance x
References
1. http://www.mathworks.com/help/matlab/ref/pdepe.html
://www.mathworks.com/help/matlab/ref/pdepe.html
ﺍﲤﲎ ﺍﻥ ﺗﻜﻮﱐ ﺇﺳﺘﻔﺪﰐ