PH36010 Numerical Methods: Solving Differential Equations Using MATHCAD
PH36010 Numerical Methods: Solving Differential Equations Using MATHCAD
PH36010 Numerical Methods: Solving Differential Equations Using MATHCAD
Numerical Methods
Solving Differential Equations
using MATHCAD
Solving ODEs
numerically
• Produce numeric solution to
system of ODEs.
• Must have initial conditions
• Use one of several different
solvers
• Produces matrix of solutions
Steps to solving ODEs
• Scale equations, parameters &
initial conditions to remove units
• Manipulate equations to give
vector of derivatives
• Give vector of initial conditions
• Call solver
• Plot results
Solution of First Order
ODE
• Radioactive decay, Newton’s
law of cooling etc
kA
d
A
dt
• A is amount of material,
temperature difference, etc
• k is rate constant
Forming the derivative
vector
• “The derivative
d
A kA of A with
dt respect to t is
–k times A”
k 0.1
D( t A ) kA
Initial Conditions
• First order ODE
– 1 member of D(t,A) vector
– 1 member of ic vector
Although only
ic0 1 1 member, still
needs to be a vector
Solution parameters
• TStart and TFinish times
• Number of points, N
Quantity 0.5
5
4.54 10 0
0 1 2 3 4 5
0 Time 5
What can go wrong
• Found number greater than
10^307
– Use more points
– Reduce TFinish
• Can’t have anything with
dimensions here
– Strip units from system before
solution
More complex first order
system
• Double decay
• A => B => C
K1 K2
K1 A( t ) B( t ) K1 A( t ) K2 B( t )
d d
A( t )
dt dt
Forming the derivative
vector
K1 A( t ) B( t ) K1 A( t ) K2 B( t )
d d
A( t )
dt dt
K1 F0 ( t ) F ( t ) 1 K1 F0 ( t ) K2 F1 ( t )
d d
F0 ( t )
dt dt
K1 F0
D( t F )
K1 F0 K2 F1
Initial Conditions
• System of 2 DEs
– 2 members in D(t,F) vector
– 2 members in ic vector
• F0 refers to A => ic0
• F1 refers to B => ic1
1 Initially 100% of A,
ic
0 0% of B
Form Solution of Double
Decay system
• Solution parameters as before
• Call rkfixed() as before to create
matrix
0.8
0.6
A
B
0.4
0.2
0
0 0.5 1 1.5 2
Time
Second Order System
Damped SHM
• Rewrite as system of first order
equations
• Solve as before
Second Order system
LCR Circuit
Kirchoff’s
Voltage Law =>
q
L coil i i R res
d
0
dt C cap
Second order System
Forming the equations
q
L coil i i R res
d
0
dt C cap
2
d d R res q
q q 0
d t L coil
L coil C cap
d t2
R res q0
q2 q1 0
L coil L coil C cap
Second Order System
Writing standard form
#2
R res q0
q2 q1 0
L coil L coil C cap
d
q1 q q1
dt
D( t q ) q1 R res C cap q0
q1 R res C cap q0
L coil C cap
2
d
q q2
d t2 L coil C cap
Second Order System
Define Initial Conditions
• Start with 1V across capacitor
& no current flowing
V0 1
0.01
0.005
Current 0
0.005
0.01
0 0.02 0.04 0.06 0.08 0.1
Time
Driven Systems
• So far all systems have been
‘Relaxation to steady state’
• Can also model systems driven
by ‘Forcing Function’
Forcing Function for LCR
circuit
• Enables us to see resonance
• Put voltage source in loop
q
L coil i i R res
d
f( t) 0
dt C cap
Solution for Forced
Oscillation
• Use symbolic solver to solve for q2
as before
R res q0 q1R resC cap q0 f( t ) L coilC cap
q2 q1 f( t ) 0 s olve q2
L coil L coilC cap L coilC cap
• Substitute to give…
q1R resC cap q0 A 0 s in( t ) L coilC cap
q2
L coilC cap
Derivative Vector for
Forced SHM
q1
5 10
4
Current 0
5 10
4
0 0.02 0.04 0.06 0.08 0.1
Time
Phase Plot
• Plot Current (q1) vs Charge (q0)
6 10
4
4
4.54710
4 10
4
2 10
4
Current 0
2 10
4
4 10
4
4
4.92810 6 10 4
0.15 0.1 0.05 0 0.05 0.1 0.15
0.135 Voltage cap 0.118