PH36010 Numerical Methods Solving Differential Equations using MATHCAD

Preview:

Citation preview

PH36010 PH36010 Numerical MethodsNumerical Methods

Solving Differential Equations using Solving Differential Equations using MATHCADMATHCAD

Solving ODEs numericallySolving ODEs numerically

• Produce Produce numericnumeric solution to system solution to system of ODEs.of ODEs.

• Must have initial conditionsMust have initial conditions• Use one of several different solversUse one of several different solvers• Produces matrix of solutionsProduces matrix of solutions

Steps to solving ODEsSteps to solving ODEs

• Scale equations, parameters & Scale equations, parameters & initial conditions to remove unitsinitial conditions to remove units

• Manipulate equations to give Manipulate equations to give vector of derivativesvector of derivatives

• Give vector of initial conditionsGive vector of initial conditions• Call solverCall solver• Plot resultsPlot results

• Radioactive decay, Newton’s law of Radioactive decay, Newton’s law of cooling etccooling etc

• A is amount of material, A is amount of material, temperature difference, etctemperature difference, etc

• k is rate constantk is rate constant

Solution of First Order ODESolution of First Order ODE

tAd

dk A

Forming the derivative Forming the derivative vectorvector

• ““The derivative The derivative of A with of A with respect to t is –k respect to t is –k times A”times A”

tAd

dk A

k 0.1

D t A( ) k A

Initial ConditionsInitial Conditions

• First order ODEFirst order ODE– 1 member of D(t,A) vector1 member of D(t,A) vector– 1 member of ic vector1 member of ic vector

ic0 1Although onlyAlthough only 1 member, still 1 member, still needs to be a vector needs to be a vector

Solution parametersSolution parameters

• TStart and TFinish timesTStart and TFinish times• Number of points, NNumber of points, N

TStart 0 TFinish 5 N 1000

Create Solution MatrixCreate Solution Matrix

• Use rkfixed() functionUse rkfixed() function• Return matrix with 1 column per DE + 1 Return matrix with 1 column per DE + 1

for independent variablefor independent variable• Use column operator to strip off columnsUse column operator to strip off columns

Soln rkfixed ic TStart TFinish N D( )

Time Soln0

Quantity Soln1

Plot ResultsPlot Results

0 1 2 3 4 50

0.5

11

4.54105

Quantity

50 Time

What can go wrongWhat can go wrong

• Found number greater than Found number greater than 10^30710^307– Use more pointsUse more points– Reduce TFinishReduce TFinish

• Can’t have anything with Can’t have anything with dimensions heredimensions here– Strip units from system before Strip units from system before

solutionsolution

More complex first order More complex first order systemsystem

• Double decayDouble decay• A => B => CA => B => C

K1 K2K1 K2

tA t( )

d

dK1 A t( )

tB t( )

d

dK1 A t( ) K2 B t( )

Forming the derivative Forming the derivative vectorvector

tA t( )

d

dK1 A t( )

tB t( )

d

dK1 A t( ) K2 B t( )

tF0 t( )

d

dK1 F0

t( )tF t( )1

d

dK1 F0

t( ) K2 F1 t( )

D t F( )K1 F0

K1 F0 K2 F1

Initial ConditionsInitial Conditions

• System of 2 DEsSystem of 2 DEs– 2 members in D(t,F) vector2 members in D(t,F) vector– 2 members in ic vector2 members in ic vector

• FF00 refers to A => ic refers to A => ic00

• FF11 refers to B => ic refers to B => ic11

ic1

0Initially 100% of A, Initially 100% of A, 0% of B0% of B

Form Solution of Double Form Solution of Double Decay systemDecay system

• Solution parameters as beforeSolution parameters as before• Call rkfixed() as before to create Call rkfixed() as before to create

matrixmatrix

TStart 0 TFinish 2 N 1000

Soln rkfixed ic TStart TFinish N D( )

Plot results of double Plot results of double decaydecay

Time Soln0

A Soln1

B Soln2

0 0.5 1 1.5 20

0.2

0.4

0.6

0.8

1

A

B

Time

Second Order SystemSecond Order SystemDamped SHMDamped SHM

• Rewrite as system of first order Rewrite as system of first order equationsequations

• Solve as beforeSolve as before

Second Order systemSecond Order systemLCR CircuitLCR Circuit

L coilti

d

d i R res

q

C cap0

Kirchoff’s Kirchoff’s Voltage Law =>Voltage Law =>

Second order SystemSecond order SystemForming the equationsForming the equations

L coilti

d

d i R res

q

C cap0

2tq

tq

R res

L coil

d

d

q

L coil C cap

0d

d

2

Use dq/dt=i and divide by LUse dq/dt=i and divide by Lcoilcoil

To get homogeneous equation in qTo get homogeneous equation in q

Second Order SystemSecond Order SystemWriting standard form #1Writing standard form #1

2tq

tq

R res

L coil

d

d

q

L coil C cap

0d

d

2

q2 q1R res

L coil

q0

L coil C cap

0

Rewrite again, getting rid of d/dtRewrite again, getting rid of d/dt

2tq q2

d

d

2

tq

d

dq1 q q0

Second Order SystemSecond Order SystemWriting standard form #2Writing standard form #2

q2 q1R res

L coil

q0

L coil C cap

0

q2 q1R res

L coil

q0

L coil C cap

0 solve q2q1 R res

C cap q0

L coil C cap

Use symbolic solver to get q2Use symbolic solver to get q2

Second Order SystemSecond Order SystemFill in D vectorFill in D vector

• Have expressions for q1 & q2Have expressions for q1 & q2• Now fill in D(t,q) vectorNow fill in D(t,q) vector• Replace Replace

– q0 q0 q q00 ,q1 ,q1 q q1 1 ,q2 ,q2 q q22

D t q( )

q1

q1 R res C cap

q0

L coil C cap

tq

d

dq1

2tq q2

d

d

2 q1 R res C cap

q0

L coil C cap

Second Order SystemSecond Order SystemDefine Initial ConditionsDefine Initial Conditions

• Start with 1V across capacitor & no Start with 1V across capacitor & no current flowingcurrent flowing

V0 1

ic0 holds charge at t=0ic

V0 C cap

0 ic1 holds current at t=0

Second Order SystemSecond Order SystemCreate SolutionCreate Solution

• Examine over 0.1sExamine over 0.1s• Use 1000 pointsUse 1000 points

TStart 0 TFinish 0.1 N 1000

Soln rkfixed ic TStart TFinish N D( )

Second Order SystemSecond Order SystemPlot Solution Plot Solution

Time Soln0

Charge Soln1

Current Soln2

Voltage capCharge

C cap

0 0.02 0.04 0.06 0.08 0.10.01

0.005

0

0.005

0.01

Current

Time

Driven SystemsDriven Systems

• So far all systems have been So far all systems have been ‘Relaxation to steady state’‘Relaxation to steady state’

• Can also model systems driven by Can also model systems driven by ‘Forcing Function’‘Forcing Function’

Forcing Function for LCR Forcing Function for LCR circuitcircuit

• Enables us to see resonanceEnables us to see resonance• Put voltage source in loopPut voltage source in loop

L coilti

d

d i R res

q

C capf t( ) 0

Solution for Forced Solution for Forced OscillationOscillation

• Use symbolic solver to solve for q2 as beforeUse symbolic solver to solve for q2 as before

• Compare with undriven case…Compare with undriven case…

q2 q1R res

L coil

q0

L coil C cap

f t( ) 0 solve q2q1 R res

C cap q0 f t( ) L coil

C cap

L coil C cap

q2 q1R res

L coil

q0

L coil C cap

0 solve q2q1 R res

C cap q0

L coil C cap

Forcing functionForcing function

• Drive with sinusoidal waveformDrive with sinusoidal waveform

• Substitute to give…Substitute to give…

q2q1 R res

C cap q0 A 0 sin t( ) L coil

C cap

L coil C cap

A 0 1 300 f t( ) A 0 sin t( )

Derivative Vector for Derivative Vector for Forced SHMForced SHM

• No initial charge or currentNo initial charge or current

D t q( )

q1

q1 R res C cap

q0 A 0 sin t( ) L coil C cap

L coil C cap

ic0

0

Form solutionForm solution

• Use rkfixed as before…Use rkfixed as before…

TStart 0 TFinish 0.1 N 1000

Soln rkfixed ic TStart TFinish N D( )

Extract values & plotExtract values & plot

Time Soln0

Charge Soln1

Current Soln2

Voltage capCharge

C cap

0 0.02 0.04 0.06 0.08 0.15 10 4

0

5 10 4

Current

Time

Phase PlotPhase Plot

• Plot Current (q1) vs Charge (q0)Plot Current (q1) vs Charge (q0)

0.15 0.1 0.05 0 0.05 0.1 0.156 10 4

4 10 4

2 10 4

0

2 10 4

4 10 4

6 10 4

4.547104

4.928104

Current

0.1180.135 Voltage cap

Recommended