Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Engr. I. I.

Cheema
Chemical Engineering Mathematics
(Ch. E 304)
9
th
Week Chemical Engineering Mathematics (Ch.E - 304) 1
Ordinary Differential Equations
Engr. I. I. Cheema
Ordinary Differential Equations
(Numerical Solution)
ODE Solver
9
th
Week Chemical Engineering Mathematics (Ch.E - 304) 2
Different ODE Solvers
Solver Problem Type Method Accuracy
Engr. I. I. Cheema
ode45 Non-Stiff Runge-Kutta Medium
ode23 Non-Stiff Runge-Kutta Low
ode113 Non-Stiff Adams Low High
ode15s Stiff NDFs (BDFs) Low Medium
ode23s Stiff Rosenbrock Low
ode23t Moderately Stiff Trapezoidal Rule Low
ode23tb Stiff TR-BDF2 Low
9
th
Week Chemical Engineering Mathematics (Ch.E - 304) 3
Advantages of Correct ODE Solver
Save time Accurate result
Engr. I. I. Cheema
9
th
Week Chemical Engineering Mathematics (Ch.E - 304) 4
ODE Solvers
ode23:
Low-order solver
Engr. I. I. Cheema
Low-order solver
Small intervals integrating
Accuracy is less important than speed
ode45:
High order (Runge-Kutta) solver
High Accuracy
Moderate speed Moderate speed
ode15s:
Stiff ODE solver (Gear's algorithm)
Time constants vary by order of magnitude
9
th
Week Chemical Engineering Mathematics (Ch.E - 304) 5
Procedure for Solving ODE
Step 1: Create function file
Step 2: Call function in m-file / command window
Engr. I. I. Cheema
Step 2: Call function in m-file / command window
Optional:
Step 3: Step 3: Customization (Fix Time Step, Error Tolerances)
Step 4: Plot the required graph
9
th
Week Chemical Engineering Mathematics (Ch.E - 304) 6
ODE Solver Syntax
Standard Syntax:
[independent, dependent] = ode45(file name, tspan, initial condition vector)
Engr. I. I. Cheema
[independent, dependent] = ode45(file name, tspan, initial condition vector)
Example:
[t,y] = ode45('myODE',[0,10],[1;0])
ODE Solver
Initial conditions
ODE Solver
23, 45, 15s
ODE
function
Time range
Initial conditions
9
th
Week Chemical Engineering Mathematics (Ch.E - 304) 7
ODE Solver Syntax
Input
ODE function name:
Output
t contains the time points
Engr. I. I. Cheema
ODE function name:
This function takes inputs
(t,y), and returns dy/dt
Time interval:
2-element vector specifying
initial and final time
Initial conditions:
t contains the time points
y contains the corresponding
values of the integrated
variables
Initial conditions:
Column vector with an
initial condition for each
ODE.
9
th
Week Chemical Engineering Mathematics (Ch.E - 304) 8
ODE Solvers: Custom Options
Fixed Time Step:
[t,y]=ode45(myODE',[0:0.001:0.5],[0 1]);
Engr. I. I. Cheema
Specify the time step by giving a vector of times
Function value returns at the specify points
Fixed time step is usually slower (function values are interpolated at desired time)
Error Tolerances:
options=odeset('RelTol',1e-6,'AbsTol',1e-10);
[t,y]=ode45('chem',[0 0.5],[0 1],options); [t,y]=ode45('chem',[0 0.5],[0 1],options);
Error is less than RelTol times the value, and less than AbsTol (at each step)
Decreasing error tolerance can considerably slow the solver
Note: For further customization see odeset
9
th
Week Chemical Engineering Mathematics (Ch.E - 304) 9
ode23 vs ode45
Example: Solve for y(t) for the time interval 0 t 2, with
initial condition y(0) = 2 and dy/dt = cos (t).
Engr. I. I. Cheema
initial condition y(0) = 2 and dy/dt = cos (t).
Step 1: Create function file:
9
th
Week Chemical Engineering Mathematics (Ch.E - 304) 10
ode23 vs ode45
Step 2: Call function in m-file Step 4: Plot the required graph
Engr. I. I. Cheema
9
th
Week Chemical Engineering Mathematics (Ch.E - 304) 11
Class Task
Problem:
A fluid of constant density starts to flow into an
Engr. I. I. Cheema
A fluid of constant density starts to flow into an
empty and infinitely large tank at 8 L/s. A valve
regulates the outlet flow to a constant 4 L/s.
Derive and solve the differential equation
describing this process over a 100 second describing this process over a 100 second
interval.
9
th
Week Chemical Engineering Mathematics (Ch.E - 304) 12

You might also like