An Introduction to MathCAD

Download Report

Transcript An Introduction to MathCAD

An Introduction to
MathCAD
Animation, Interpolation
and other pretty pictures
Animation
Example
 Example
from our LCR circuit
MathCAD #7  dpl 2001
2
Animation #2
Basics
 Use
FRAME variable
 FRAME automatically set by
mathCAD for each frame of
animation
 Select View|Animation… to
bring up animation dialog
 Give start & end values for
FRAME
 Select area to animate
 Select Animate to produce
 Save As… to create .avi file
MathCAD #7  dpl 2001
3
Animation #3
Hints
 “Error
initialising Video
Stream” => Nothing Selected
 May want to scale FRAME
 Animations can:
– Take a long time to create
– Produce big files
 Windows
video player allows
thumb bar to step forward &
back
MathCAD #7  dpl 2001
4
Animation #4
More Hints
 Fix
scale on graphs
 Don’t need to include
calculations in animation
 Echo FRAME= in shot
 If scaling, echo scaled variable
 For frequency plots, scale
FRAME thus:
P t sP erDecad e 5
FR AME

Pts PerDecade
10
MathCAD #7  dpl 2001
5
Vector Field Plots #1
 Plot
matrix of complex numbers
 Each point shown as vector
 Plot scaled so that largest vector
is gap between points
 Useful for:
–
–
–
–
Electric fields
Magnetic fields
Dynamic Flows
Plotting Gradient of functions
MathCAD #7  dpl 2001
6
Vector Field Plots #2
x
0  19
Mx y
y
0  19
i  x 0 .3
y e
M
MathCAD #7  dpl 2001
7
Data Analysis #1
Get data from file
 Read
data in from datafile
 Use either:
– Insert|Component|File Read or
Write & follow the wizard
– READPRN(“filename”)
 Reads
data from file into vector
or matrix
 File Read component offers
many more file types
MathCAD #7  dpl 2001
8
Data Analysis #2
Select the data we want
 Use
matrix functions to select
data we are interested in
 Rows(M) & Cols(M) to get size
of data array
 Submatrix(M,rs,rf,cs,cf) to
select rectangular chunk
 M<x> to select column
 Csort to sort array
 Augment & stack to build
arrays
MathCAD #7  dpl 2001
9
Data Analysis #3
Smoothing Data

medsmooth(vy,n) returns vy
smoothed with running medians
s
smoothed
s  50
medsmooth( data  s )
8 00
6 00
s
4 00
smo oth ed
i
2 00
0
6 00
6 10
6 20
6 30
6 40
6 50
i
MathCAD #7  dpl 2001
10
Data Analysis #4
Linear regression
 Slope
& intercept functions take
vectors of X & Y values
8 00
6 00
Y
4 00
2 00
0
0
20
40
60
80
1 00
X
intercept( X  Y)  4.914
slope ( X  Y)  6.955
MathCAD #7  dpl 2001
11
Data Analysis #5
Interpolation
2
stage process
– Fit polynomial through points
cspline – cubic spline
 Bspline – B-spline

– Interpolate using polynomial
obtained in stage 1

interp
MathCAD #7  dpl 2001
12
Cubic Spline fit &
Interpolated Fit
9 0.69 3 00 3
Y
fit( z)
0 .4 62 3 02
min( X )
1
X z
X-Y data
Cubic spline interpolation
MathCAD #7  dpl 2001
max( X )
1
13
Function Fitting
 Cspline
& Bspline fit smooth
curve through data points
 In physical modelling, we know
the shape of the function to fit,
just need parameters
 Example:
– Response of photon counting
detector
MathCAD #7  dpl 2001
14
Function Fitting
Example #1
5
1 1 0
4
1 1 0
3
1 1 0
1 
Data
100
10
1
0.1
7
1 1 0
Fit t o f unct ion
1 1 0
1 1 0
6
f( x) a  x e
 Determine
5
1 1 0
1 1 0
0 
Data
4
3
0.01
0.1
1
b x
parameters a & b
MathCAD #7  dpl 2001
15
Function Fitting
Example #2
 Rewrite
f(x) and derivatives
 Replace a,b by u0, u1…
 Find partial derivatives of f(x)
 Create vector of function & pds
f( x) a  x e
u 0  x e
F( x u )
b x
u x
1
d
x exp u 1  x
da
2
u 0  x  exp u 1  x
d
db
a  x e
b x
a  x e
b x
MathCAD #7  dpl 2001
x exp( b  x)
2
a  x  exp( b  x)
16
Function fitting
Example #3
 Create
vector of guess values
5 10
5
guess
7
 Call
genfit to solve
 Define function using params
p arams
f( x)
 0
 1
g enfit D ata
 D ata
 g uess  F
F( x p arams ) 0
MathCAD #7  dpl 2001
17
Function Fitting
Example #4
 Plot
f(x) against data
5
1 1 0
4
 0
2 .4 5 11
4
1 1 0
3
1 1 0
1 
Data
0 
f Data
100
10
1
0 .1
0 .0 6 6 0 .0 1
7
6
1 1 0
1 1 0
 0 7
1 .5 4 21
1 1 0
5
1 1 0
4
0 
Data
1 1 0
3
MathCAD #7  dpl 2001
0 .0 1
0 .1
1
0 .3 5
18