0% found this document useful (0 votes)
248 views27 pages

Solve Ode Pde Using Matlab

This document provides an outline and overview of solving ordinary differential equations (ODEs), delay differential equations (DDEs), and partial differential equations (PDEs) using MATLAB. It introduces the main numerical solvers for ODEs (ode45, ode23, etc.) and discusses defining ODE functions. It also covers DDE solvers (dde23, ddesd) and provides an example of solving a DDE. Finally, it introduces the pdepe solver for parabolic PDEs and includes a single PDE example.

Uploaded by

Saddy Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
248 views27 pages

Solve Ode Pde Using Matlab

This document provides an outline and overview of solving ordinary differential equations (ODEs), delay differential equations (DDEs), and partial differential equations (PDEs) using MATLAB. It introduces the main numerical solvers for ODEs (ode45, ode23, etc.) and discusses defining ODE functions. It also covers DDE solvers (dde23, ddesd) and provides an example of solving a DDE. Finally, it introduces the pdepe solver for parabolic PDEs and includes a single PDE example.

Uploaded by

Saddy Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Solve ode,

ode, dde & pde using matlab

Farida Mosally

Mathematics Department

King Abdulaziz University

2014
Outline
1. Ordinary Differential Equations (ode)
1.1 Analytic Solutions
1.2 Numerical Solutions

2. Delay Differential Equations (dde


dde)

3. Partial Differential Equations (pde


pde)
Ordinary Differential
Equations
Analytic Solutions
First Order Differential Equation
Initial Value Problem
To solve an initial value problem, say,

Suppose we want to plot the solution to


get a rough idea of its behavior.
Second and Higher Order Equations
Suppose we want to solve and plot the solution to the second order
equation
Ordinary Differential Equations

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

function out = example2()


example
xspan = [0,.5];
y0 = 1;
[x,y]=ode23(@firstode,xspan,y
(@firstode,xspan,y0);
plot(x,y)

function yprime = firstode(x,y);


firstode
yprime = x*y^2 + y;
Solving systems of first-order
first ODEs
Example 3

function example3 0.8

tspan = [0 12]; 0.6

0.4
y0 = [0; 1; 1];
0.2

% Solve the problem using ode45 0

-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

dde23 Solve delay differential equations (DDEs) with constant delays

ddesd Solve delay differential equations (DDEs) with general delays

ddensd Solve delay differential equations (DDEs) of neutral type


dde23
Solve delay differential equations (DDEs) with constant delays

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);

function dydt = ddex1de(t,y,Z)


% Differential equations function for DDEX1.
DDEX
ylag1 = Z(:,1);
ylag2 = Z(:,2);
dydt = [ ylag1(1)
ylag1(1) + ylag2(2)
y(2) ];
Partial Differential Equations (pde)
(

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);

solution profile can also be illuminating. % --------------------------------


ure; function [pl,ql,pr,qr] = bc(xl,ul,xr,ur
t(x,u(end,:),'o',x,exp(-t(end))*sin(pi*x)); pl = ul;
le('Solutions at t = 2.'); ql = 0;
end('Numerical, 20 mesh points','Analytical',
,0); pr = pi * exp(-t);
bel('Distance x'); qr = 1;
bel('u(x,2)');
Solutions at t = 2.
0.14

0.12

0.1

0.08 Numerical, 20 mesh points


Numerical
Analytical
(x,2)

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
‫ﺍﲤﲎ ﺍﻥ ﺗﻜﻮﱐ ﺇﺳﺘﻔﺪﰐ‬

You might also like