PH24010 MathCAD More Curve Fitting Previously on PH24010 • Linear Fitting • slope(), intercept() • line() 3 2.7 2.4 2.1 1.8 1.5 1.2 • Pre-process Y-data: – y = k/x – y = ekx 0.9 0.6 0.3 0 0 0.5 1 Y data Y Error Fit ted 1.5 2 2.5 3 3.5 4 4.5 5 What when can’t pre-process ? • eg. • y = a + bx + cx2 10 8 6 • Current through light bulb Current 4 2 0 • R changes with heat 0 0.05 0.1 0.15 Voltage 0.2 0.25 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 1 ln ( x ) vFunc ( x) x e 3 x 1 0 vFun c1 ( ) 2.7 18 1 1 0.6 93 vFun c2 ( ) 7.3 89 8 Lightbulb Example • Quadratic • A + B x + C x2 1 Quadrat( x) x 2 x Coeffs linfit( Voltage Current Quadrat) • Call linfit() to get coeffients 0.118 Coeffs 3.31 131.11 Create model from linfit() results • Explicitly put Coeffs into model • better to use vector maths… (dot product) 2 BulbModel( x) Coeffs0 xCoeffs1 x Coeffs2 BulbModel ( x) Quadrat( x) Coeffs Compare model with data 15 10 Current BulbModel( v) 5 0 0 0.1 0.2 Voltage v Peak fitting • Applications in spectroscopy • Gaussian peak • need to find: – position – amplitude – background – width Gaussian Peak Fit 10 5 GaussY 0 5 10 5 0 GaussX 5 10 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 2 x y( x) y0 Ae 2 2w • 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 Function parameters • Re-write as P0, P1, P2 … ( xP2) y( x) P 0 P 1e 2 2 2P3 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 d dP0 d dP1 2 ( xP2) 2 2P3 P0 P1 e 1 2 2 ( xP2) 1 ( xP2) 2 2 2 2P3 P3 P0 P1 e e 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 2 xP 2 2 2 P 3 P P e 0 1 1 2 1 xP 2 2 2 P 3 e GaussFit ( x P) 2 1 xP 2 x P2 2 P 3 2 P1 e 2 P3 2 1 xP 2 2 2 2 x P P 2 3 P1 e 3 P 3 • Function to fit • dF/dP0 • dF/dP1 • dF/dP2 • dF/dP3 Guess Values for Parameters • By inspection of graph The other thing that the f itting routine needs is a v ector of inital gues ses f or the param eters , P0 -- P3. 10 5 GaussY 0 5 10 5 0 GaussX 5 10 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 2 6 Guess P 3 2 Call genfit() GaussCoeffs genfit( GaussX GaussY GuessP GaussFit) • Form model as before from coeffients & fit function GaussModel(x) GaussFit(x GaussCoeffs)0 Use vector subscript to extract correct function from vector function Plot model with data 10 y0A 5 y0 0 5 10 5 Data Gaussian Fit 0 5 10