An Introduction to MathCAD

advertisement
An Introduction to
MathCAD
You can always find a
solution !!
When things go wrong
f( x)
5
x
4
7 9 x
f1 ( x)
d
f( x)
dx
f1 ( x)
5x
4
 3
2 18 0x
 3
3 16x
2
1 33 76 4x
 2
6 54 0x
 x 1 60 39 29 6
3 09 84 48
2 67 52 8x 3 09 84 48
56
34
roo ts
f( x) 0 so lv e x
78
12
9
t urni ng s f1 ( x) 0 so lv e x
“Result is too large to Display”
MathCAD #6  dpl 2001
2
Can we fix it ?
Yes we can !!
f( x)
5
1 .0 x
f1 ( x)
d
f( x)
dx
f1 ( x)
4
5 .0 x
4
7 9 x
 3
2 18 0x
 3
3 16x
2
1 33 76 4x
 2
6 54 0x
 x 1 60 39 29 6
3 09 84 48
2 67 52 8x 3 09 84 48
5 6.
3 4.
roo ts
f( x) 0 so lv e x
7 8.
1 2.
9.
2 7.0 33 16 34 3 82 32 02 82 9 2
t urni ng s f1 ( x) 0 so lv e x
1 0.4 81 80 37 7 90 12 76 21 3 7
3 1.6 78 24 97 7 42 97 67 92 7 6
6 9.0 36 71 74 4 29 47 11 11 5 3
Force solver to work with real numbers
MathCAD #6  dpl 2001
3
What if its still broken ?
f( x)
5
x
4
7 9 x
f1 ( x)
d
f( x)
dx
f1 ( x)
5x
4
 3
2 18 0x
 3
3 16x
2
1 33 76 4x
 2
6 54 0x
x
3 09 84 48
2 67 52 8x
1 60 39 29 6
3 09 84 48
56
34
roo ts
f( x) 0 so lv e x
78
12
9
2 7.0 33 16 3 43 84 46 2 61 37
t urni ng s
f1 ( x) 0 .00 01so lv e x
1 0.4 81 80 3 77 86 52 3 27 20
3 1.6 78 24 9 77 40 81 3 98 83 1
6 9.0 36 71 7 44 30 17 1 89 74 9
Risky !! – OK in this case
MathCAD #6  dpl 2001
4
Still can’t solve it ?
 Use
polyroots to find roots
 Use coeffs keyword on solver to
get coefficients of x
3 09 84 48
2 67 52 8
cfsx
f1 ( x) co effs  x
6 54 0
3 16
5
2 7.0 33
p ol yroo ts( cfsx ) 
1 0.4 82
3 1.6 78
6 9.0 37
MathCAD #6  dpl 2001
5
Still can’t solve it ?
 Graph
shows maxima &
minima
 => there must be solutions
 Try guessing & using
roots(f1(x),x)
MathCAD #6  dpl 2001
6
Solving ODEs
numerically
 Produce
numeric solution to
system of ODEs.
 Must have initial conditions
 Manipulate equations
 Use one of several different
solvers
 Produces matrix of solutions
MathCAD #6  dpl 2001
7
First order linear ODE
#1
 Radioactive
decay, Newton’s
law of cooling etc
d
A
dt
k A
 A is
amount of material
temperature difference
etc
 k is rate constant
MathCAD #6  dpl 2001
8
First order linear ODE
#2
 Define
initial conditions as a vector
 1st order so only 1 element in vector
 Can’t use units in ODE solver
 Call vector ‘ic’
 Element 0 = A at t=0
ic0
100
MathCAD #6  dpl 2001
9
First order linear ODE
#3
 Now
define ODE & manipulate
for mathCAD
d
A
dt
k
kA
0 .1
D( t  A )
kA
MathCAD #6  dpl 2001
10
First order linear ODE
#4
 Now
define range of solution
t0
0
t1
5
N
1 00 0
Start time
Finish Time
Number of Points
 And
solve using rkfixed
Soln
rkfixed( ic  t0  t1  N D)
 Creates
matrix ‘Soln’ containing
solution
MathCAD #6  dpl 2001
11
First order linear ODE
#5
The Solution Matrix
0
1
0
0
100
1
5·10 -3
99.501
2
0.01
99.005
3
0.015
98.511
4
0.02
98.02
5
0.025
97.531
6
0.03
97.045
Sol n  7
0.035
96.561
8
0.04
96.079
9
0.045
95.6
10
0.05
95.123
11
0.055
94.649
12
0.06
94.176
13
0.065
93.707
14
0.07
93.239
15
0.075
92.774
0.08
92.312
Column 0 holds t values
Column 1 holds A values
1 row of matrix per
timestep (0..N)
MathCAD #6  dpl 2001
12
First order linear ODE
#6
 Plot
the results
 Use M<> to extract columns
 Use subscripting to extract rows
i
0  N
1 00
 1
Soln
i
50
0
0
2
4
6
 0
Soln
i
MathCAD #6  dpl 2001
13
Second Order ODE
 Same
steps as for first order
 Slightly less obvious
manipulation
 Replace ODE by system of 1st
orders
 Can use symbolic solver to
formulate equations
MathCAD #6  dpl 2001
14
Second Order ODE
Example #1
 Damped
SHM
– LCR circuit
– Damped Pendulum
d

L i
dt
i R
q
C
0
Substitute for i & divide through by L
d
q i
dt
Gives:
d
2
q
d t2
Rd
L dt
q
MathCAD #6  dpl 2001
1 
q 0
LC
15
Second Order ODE
Example #2
d
2
q
d t2
Rd
L dt
1 
q 0
LC
q
rewrite equation (by hand) substituting
2
d
q q2
d t2
d
q q1
dt
q q0
Gives manipulated
equation:
q2
R
L
q1
MathCAD #6  dpl 2001
1 
q0 0

LC
16
Second Order ODE
Example #3
R
q2
L
 Use
q2
R
L
q1
1 
q0 0
L C
solver to solve for q2
q1
1 
q0 0 solve  q2
L C
( R q1  C q0 )
( L C )
 Now
ready to create D(t,q)
function
MathCAD #6  dpl 2001
17
Second Order ODE
Example #4
 Equations
d
q q1
dt
to create D(t,q)
d
2
q q2
d t2
( R q1  C q0 )
( L C )
2
row vector to hold D(t,q)
 Change subscripts for suffixes
q1
D( t  q )
R q1 C
d
q
dt
q0
( L C )
MathCAD #6  dpl 2001
d
2
q
d t2
18
Second Order ODE
Example #5
 Specify
initial conditions
 For second order need 2
elements in ic vector
ic
1
Charge (q) at t=0
0
Current (dq/dt) at t=0
 Also
need to specify constants
in D(t,q) equations
 Watch for lack of units !!
L
10
2
C
10
3
R
MathCAD #6  dpl 2001
1
19
Second Order ODE
Example #6
 Define
times and number of
points as before
 Call rkfixed to solve
t1
0.1
t0
0
N
10 00
So ln rkfixed( ic 0  t1 N  D)
 Matrix
Soln filled with solution
points
MathCAD #6  dpl 2001
20
Second Order ODE
Example #7
• The solution matrix
0
0
1
2
3
4
5
6
Soln  7
8
9
10
11
12
13
14
15
1
0
1·10
2·10
3·10
4·10
5·10
6·10
7·10
8·10
9·10
1·10
1.1·10
1.2·10
1.3·10
1.4·10
1.5·10
1.6·10
-4
-4
-4
-4
-4
-4
-4
-4
-4
-3
-3
-3
-3
-3
-3
-3
2
1
1
0.998
0.996
0.992
0.988
0.982
0.976
0.969
0.961
0.952
0.942
0.932
0.92
0.908
0.895
0.881
0
-9.949
-19.788
-29.51
-39.106
-48.568
-57.887
-67.055
-76.066
-84.912
-93.585
-102.078
-110.386
-118.501
-126.417
-134.129
-141.631
Column 0 holds t
values
Column 1 holds q
values
Column 2 holds
dq/dt values
MathCAD #6  dpl 2001
21
Second Order ODE
Example #8
 Graphing
Time
the solution
 0
Soln
Charge
 1
Soln
1
0 .5
Ch arg e
0
0 .5
1
0
0 .0 5
0 .1
Time
MathCAD #6  dpl 2001
22
Surely nothing can go
wrong ?
 Solution
relies on numeric
integration which divides
timestep up into smaller chunks
for integration
 If system is changing much
faster than timestep, solution
will fail
 Clues to look for:
– “found a number >10^307”
– Singularity at tfinish
MathCAD #6  dpl 2001
23
Can we fix it ?
 Check
time constants
 Try more point in solution (N)
 Try smaller interval
 Use a different solver
– ‘Stiff’ systems give problems
– Special solvers for stiff systems
– See Quicksheet & help system
MathCAD #6  dpl 2001
24
Download