PH36010 Numerical Methods: Solving Differential Equations Using MATHCAD

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 32

PH36010

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
kA
d
A
dt

• A is amount of material,
temperature difference, etc
• k is rate constant
Forming the derivative
vector
• “The derivative
d
A kA of A with
dt respect to t is
–k times A”
k 0.1

D( t  A ) kA
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

TStart 0 TFinish 5 N 1000


Create Solution Matrix
• Use rkfixed() function
• Return matrix with 1 column per DE +
1 for independent variable
• Use column operator to strip off
columns

Soln rkfixed( ic  TStart TFinish N  D)


 0  1
Time Soln Quantity Soln
Plot Results
1
1

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

TStart 0 TFinish 2 N 1000

Soln rkfixed( ic  TStart TFinish N  D)


Plot results of double
decay
 0  1  2
Time Soln A Soln B Soln

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

Use dq/dt=i and divide by Lcoil

2
d d  R res q
q q 0
d t L coil 
L coil C cap
d t2

To get homogeneous equation in q


Second Order System
Writing standard form
#1
2
d d  R res q
q q 0
d t L coil 
L coil C cap
d t2

Rewrite again, getting rid of d/dt


2
d d
q q2 q q1 q q0
d t2 dt

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

Use symbolic solver to get q2

R res q0 q1 R res C cap q0


q2 q1 0 s olve q2

L coil L coil C cap L coil C cap
Second Order System
Fill in D vector
• Have expressions for q1 & q2
• Now fill in D(t,q) vector
• Replace
– q0  q0 ,q1  q1 ,q2  q2

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

V0  C cap ic0 holds charge at t=0


ic
0 ic1 holds current at t=0
Second Order System
Create Solution
• Examine over 0.1s
• Use 1000 points

TStart 0 TFinish 0.1 N 1000

Soln rkfixed( ic  TStart TFinish N  D)


Second Order System
Plot Solution
 0  1  2 Charge
Time Soln Charge Soln Current Soln Voltagecap
C cap

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 q1R resC cap q0 f( t ) L coilC cap
q2 q1 f( t ) 0 s olve q2
L coil L coilC cap L coilC cap

• Compare with undriven case…

R res q0 q1 R res C cap q0


q2 q1 0 s olve q2
L coil L coil C cap L coil C cap
Forcing function
• Drive with sinusoidal waveform
A0 1  300 f( t) A 0 sin( t)

• Substitute to give…
q1R resC cap q0 A 0 s in(  t ) L coilC cap
q2
L coilC cap
Derivative Vector for
Forced SHM
q1

D( t  q ) q1R resC cap q0 A 0 s in(  t) L coilC cap


L coilC cap

• No initial charge or current


0
ic
0
Form solution
• Use rkfixed as before…

TStart 0 TFinish 0.1 N 1000

Soln rkfixed( ic  TStart TFinish N  D)


Extract values & plot
0  1  2  Charge
Time Soln Charge Soln Current Soln Voltagecap
C cap

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.54710

4 10
4

2 10
4

Current 0

2 10
4

4 10
4

4
4.92810 6 10 4
0.15 0.1 0.05 0 0.05 0.1 0.15
0.135 Voltage cap 0.118

You might also like