Solve odeSolve ode, ,, , ddeddedde& && & pdepde using...

27
Solve ode Solve ode Solve ode Solve ode, , , , dde dde dde dde & & & & pde pde pde pde us us us us Farida Mosally Mathematics Department King Abdulaziz University 2014 sing sing sing sing matlab matlab matlab matlab

Transcript of Solve odeSolve ode, ,, , ddeddedde& && & pdepde using...

Page 1: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

Solve odeSolve odeSolve odeSolve ode, , , , ddeddeddedde & & & & pdepdepdepde using using using using

Farida Mosally

Mathematics Department

King Abdulaziz University

2014

using using using using matlabmatlabmatlabmatlab

Page 2: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

Outline

1. Ordinary Differential Equations (ode)

1.1 Analytic Solutions

1.2 Numerical Solutions

2. Delay Differential Equations (dde

3. Partial Differential Equations (pde

Ordinary Differential Equations (ode)

dde)

pde)

Page 3: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

Ordinary Differential Equations

Analytic Solutions

Ordinary Differential Equations

Analytic Solutions

Page 4: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

First Order Differential EquationFirst Order Differential Equation

Page 5: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

Suppose get a rough idea

To solve an initial value problem, say,

Initial Value Problem

Suppose we want to plot the solution to get a rough idea of its behavior.

To solve an initial value problem, say,

Initial Value Problem

Page 6: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

Second and Higher Order Equations

Suppose we want to solve and plot the solution to the second order equation

and Higher Order Equations

Suppose we want to solve and plot the solution to the second order

Page 7: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

Numerical Solutions

Ordinary Differential Equations

Numerical Solutions

MATLAB has a number of tools for numerically solving ordinary differential equations. In the following table we display

Numerical Solutions

Ordinary Differential Equations

Numerical Solutions

MATLAB has a number of tools for numerically solving ordinary . In the following table we display some of them.

Page 8: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

Solver Method used

ode45 Runge-Kutta (4,5) formula

ode23 Runge-Kutta (2, 3) formula

ode initial value problem

ode113 Adams-Bashforth-Moulton solver

ode15s Solver based on the numerical

differentiation formulas

ode23s Solver based on a modified Rosenbrock

formula of order 2

Order of

Accuracy

When to Use

Medium Most of the time. This should be the

first solver you try.

Low For problems with crude error

tolerances or for solving moderately

stiff problems.

initial value problem solvers

Low to high For problems with stringent error

tolerances or for solving

computationally intensive problems.

Low to

medium

If ode45 is slow because the

problem is stiff.

Solver based on a modified Rosenbrock Low If using crude error tolerances to

solve stiff systems and the mass

matrix is constant.

Page 9: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

Defining an ODE functionfunction

Page 10: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

sol = solver(odefun,[t

Defining an ODE function

Example 1

,[t0 tf],y0,options)

function

1.7

1.8

1.9

0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.51

1.1

1.2

1.3

1.4

1.5

1.6

1.7

Page 11: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

First Order Equations with M

function out = example

xspan = [0,.5];

Example 2

xspan = [0,.5];

y0 = 1;

[x,y]=ode23(@firstode,xspan,y

plot(x,y)

function yprime = firstode

yprime = x*y^2 + y;

First Order Equations with M-files

out = example2()

(@firstode,xspan,y0);

firstode(x,y);

Page 12: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

Solving systems of firstExample 3

function example3

tspan = [0 12];tspan = [0 12];

y0 = [0; 1; 1];

% Solve the problem using ode45

ode45(@f,tspan,y0);

% ------------------------------

function dydt = f(t,y)

dydt = [ y(2)*y(3)

-y(1)*y(3)

-0.51*y(1)*y(2) ];

Solving systems of first-order ODEs

0.6

0.8

1

0 2 4 6 8 10 12-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

Page 13: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

… Solving systems of first

To define each curve

function example3

tspan = [0 12];

y0 = [0; 1; 1];

% Solve the problem using ode45

[t,y] = ode45(@f,tspan,y0);

plot(t,y(:,1),':r',t,y(:,2),'-.',t,y(:,

legend('y_1','y_2','y_3',3)

% ------------------------------

function dydt = f(t,y)

dydt = [ y(2)*y(3)

-y(1)*y(3)

-0.51*y(1)*y(2) ];

systems of first-order ODEs

0.6

0.8

1

(:,3))

0 2 4 6 8 10 12-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

y1

y2

y3

Page 14: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

Boundary Value Problems

sol = bvp4c(odefun,bcfun,solinit

Value Problems

odefun,bcfun,solinit)

Page 15: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

Delay Differential Equations

Functions

dde23 Solve delay differential equations (DDEs) with constant delays

ddesd Solve delay differential equations (DDEs) with general delaysddesd Solve delay differential equations (DDEs) with general delays

ddensd Solve delay differential equations (DDEs) of neutral type

Equations

Solve delay differential equations (DDEs) with constant delays

Solve delay differential equations (DDEs) with general delaysSolve delay differential equations (DDEs) with general delays

Solve delay differential equations (DDEs) of neutral type

Page 16: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

dde23

Solve delay differential equations (DDEs) with constant delays

sol = dde23(ddefun,lags,history,tspan

Example 4

Solve delay differential equations (DDEs) with constant delays

ddefun,lags,history,tspan)

for t ≤ 0.

Page 17: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

function ddex1

sol = dde23(@ddex1de,[1, 0.2],@ddex1hist,[

figure;

plot(sol.x,sol.y)

title('An example of Wille'' and Baker.'

xlabel('time t');

ylabel('solution y');

function s = ddex1hist(t)

% Constant history function for DDEX1.

function dydt = ddex1de(t,y,Z)

% Differential equations function for DDEX

ylag1 = Z(:,1);

ylag2 = Z(:,2);

dydt = [ ylag1(1)

ylag1(1) + ylag2(2)

y(2) ];

s = ones(3,1);

hist,[0, 5]);

'' and Baker.');

% Differential equations function for DDEX1.

Page 18: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:
Page 19: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

Partial Differential Equations (

pdepeSolve initial-boundary value problems for parabolic-elliptic PDEs in

Syntax

sol = pdepe(m,pdefun,icfun,bcfun,xmesh,tspan)

sol = pdepe(m,pdefun,icfun,bcfun,xmesh,tspan,OPTIONS

[sol,tsol,sole,te,ie] = pdepe(m,pdefun,icfun,bcfun,xmesh,tspan,OPTIONS

Partial Differential Equations (pde)

elliptic PDEs in 1-D

m,pdefun,icfun,bcfun,xmesh,tspan,OPTIONS)

m,pdefun,icfun,bcfun,xmesh,tspan,OPTIONS)

Page 20: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

Arguments

m A parameter corresponding to the symmetry of the problem.

cylindrical =1, or spherical = 2.

pdefun A handle to a function that defines the components of the PDE.

icfun A handle to a function that defines the initial conditions.

sol = pdepe(m,pdefun,icfun,bcfun,xmesh,tspan

bcfun A handle to a function that defines the boundary conditions.

xmesh A vector [x0, x1, ..., xn], x0 < x1 < ... < xn.

tspan A vector [t0, t1, ..., tf] , t0 < t1 < ... < tf.

options Some OPTIONS of the underlying ODE solver are

A parameter corresponding to the symmetry of the problem. m can be slab = 0,

A handle to a function that defines the components of the PDE.

A handle to a function that defines the initial conditions.

m,pdefun,icfun,bcfun,xmesh,tspan)

A handle to a function that defines the boundary conditions.

.

of the underlying ODE solver are available, See odeset for details.

Page 21: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

Description

PDE

IC

BC

Page 22: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:
Page 23: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

Example 1.

This example illustrates the straightforward formulation, computation, and plotting of the solution of a single PDE.

Single PDE

example illustrates the straightforward formulation, computation, and plotting of the

Page 24: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

function pdex1

m = 0;

x = linspace(0,1,20);

t = linspace(0,2,5);

sol = pdepe(m,@pdecfs,@ic,@bc,x,t);

u = sol(:,:,1);

% A solution profile can also be illuminating.

figure;

plot(x,u(end,:),'o',x,exp(-t(end))*sin(pi*x));

title('Solutions at t = 2.');

legend('Numerical, 20 mesh points','Analytical',

xlabel('Distance x');

ylabel('u(x,2)');

function [c,f,s] = pdecfs(x,t,u,DuDx)

c = pi^2;

f = DuDx;

s = 0;

% --------------------------------

function u0 = ic(x)

u0 = sin(pi*x);

,0);

% --------------------------------

function [pl,ql,pr,qr] = bc(xl,ul,xr,ur,t

pl = ul;

ql = 0;

pr = pi * exp(-t);

qr = 1;

Page 25: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

0.06

0.08

0.1

0.12

0.14Solutions at t

(x,2

)

Numerical

Analytical

0 0.1 0.2 0.3 0.4-0.02

0

0.02

0.04

Distance x

u(x

Solutions at t = 2.

Numerical, 20 mesh points

Analytical

0.5 0.6 0.7 0.8 0.9 1

Distance x

Page 26: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

References

1. http://www.mathworks.com/help/matlab/ref/pdepe.html://www.mathworks.com/help/matlab/ref/pdepe.html

Page 27: Solve odeSolve ode, ,, , ddeddedde& && & pdepde using matlabfmusaly.kau.edu.sa/Files/0000823/Files/101561_Solve... · Microsoft PowerPoint - Solve ode,dde & pde using matlab1 Author:

امتىن ان تكوين إستفديت