Upload
pamela-stanley
View
224
Download
0
Embed Size (px)
Citation preview
PH24010
MathCADMore Curve Fitting
Previously on PH24010
• Linear Fitting• slope(), intercept()• line()
• Pre-process Y-data:– y = k/x– y = ekx
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 50
0.3
0.6
0.9
1.2
1.5
1.8
2.1
2.4
2.7
3
Y dataY Error
Fitted
What when can’t pre-process ?
• eg.
• y = a + bx + cx2
• Current through light bulb
• R changes with heat
0 0.05 0.1 0.15 0.2 0.250
2
4
6
8
10
Current
Voltage
linfit()
• Fitting routine for ‘linear’ combinations of functions.
• eg: y = A + B ln(x) + C ex + D x3
Find A,B,C & D to give best fit to data.
• Needs ‘vector of functions’
A vector of functions
• Define function of variable x
• Create vector with n rows & 1 column
• Fill placeholders with expressions involving x
vFunc x( )
1
ln x( )
ex
x3
vFunc 1( )
1
0
2.718
1
vFunc 2( )
1
0.693
7.389
8
Lightbulb Example
• Quadratic
• A + B x + C x2
• Call linfit() to get coeffients
Quadrat x( )
1
x
x2
Coeffs linfitVoltage Current Quadrat( )
Coeffs
0.118
3.31
131.11
Create model from linfit() results
• Explicitly put Coeffs into model
• better to use vector maths…(dot product)
BulbModel x( ) Coeffs0 x Coeffs1 x2
Coeffs2
BulbModel x( ) Quadrat x( ) Coeffs
Compare model with data
0 0.1 0.20
5
10
15
Current
BulbModelv( )
Voltage v
Peak fitting
• Applications in spectroscopy
• Gaussian peak
• need to find:– position– amplitude– background– width
Gaussian Peak Fit
10 5 0 5 105
0
5
10
GaussY
GaussX
genfit()
• Generalised fit of any function
• Need:– model function– partial derivatives of model wrt parameters– vector of initial guesses for each parameter
Gaussian Peak Function
• Where:– y0 is the background level– m is the x value of the peak centre– w is the width of the peak at half amplitude– A is the amplitude of the peak maximum
y x( ) y0 A e
x 2
2w2
Function parameters
• Re-write as P0, P1, P2 …
y x( ) P0 P1 e
x P2( )2
2P32
P0 is the background level (y0)P1 is the amplitude of the peak maximum (A) P2 is the x value of the peak centre (m)P3 is the width of the peak at half amplitude (w)
Form Partial Derivatives
• Use symbolic differentiation
P0P0 P1 e
x P2( )2
2P32
d
d1
P1P0 P1 e
x P2( )2
2P32
d
de
12
x P2( )2
P32
Create Function Vector for genfit()
• Function takes 2 parameters:– Independent variable, x– Parameter Vector, P
• Re-write P0,P1,P2 etc to use vector subscripts P0, P1, P2
Vector function for Gaussian fit
• Function to fit
• dF/dP0
• dF/dP1
• dF/dP2
• dF/dP3
GaussFit x P( )
P0 P1 e
x P2 2
2 P3 2
1
e
12
x P2 2
P3 2
P1x P2
P3 2e
12
x P2 2
P3 2
P1x P2 2
P3 3e
12
x P2 2
P3 2
Guess Values for Parameters
• By inspection of graphThe other thing that the fitting routine needs is a vector of inital guesses for the parameters,P0 -- P3.
10 5 0 5 105
0
5
10
GaussY
GaussX
P0 is the background level (y0) = 2 P1 is the amplitude of the peak maximum (A) = 6 P2 is the x value of the peak centre () = -3 P3 is the width of the peak at half amplitude (w) = 2
GuessP
2
6
3
2
Call genfit()
• Form model as before from coeffients & fit function
GaussCoeffs genfit GaussX GaussY GuessP GaussFit( )
GaussModel x( ) GaussFit x GaussCoeffs( )0
Use vector subscript to extract correct function from vector function
Plot model with data
10 5 0 5 105
0
5
10
DataGaussian Fit
y0
y0 A