200 likes | 320 Vues
This guide explores advanced curve fitting techniques in MathCAD, focusing on linear fitting and Gaussian peak fitting. It includes methods for processing Y-data, utilizing fitting routines for linear combinations of functions, and creating models using vector mathematics. You'll learn how to define functions, generate fitting models, and extract coefficients using the linfit and genfit functions. Applications in spectroscopy and practical examples, such as light bulb current fitting and Gaussian peak analysis, illustrate the effectiveness of these techniques.
E N D
PH24010 MathCAD More Curve Fitting
Previously on PH24010 • Linear Fitting • slope(), intercept() • line() • Pre-process Y-data: • y = k/x • y = ekx
What when can’t pre-process ? • eg. • y = a + bx + cx2 • Current through light bulb • R changes with heat
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
Lightbulb Example • Quadratic • A + B x + C x2 • Call linfit() to get coeffients
Create model from linfit() results • Explicitly put Coeffs into model • better to use vector maths…(dot product)
Peak fitting • Applications in spectroscopy • Gaussian peak • need to find: • position • amplitude • background • width
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
Function parameters • Re-write as P0, P1, P2 … 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
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
Guess Values for Parameters • By inspection of graph 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 (m) = -3 P3 is the width of the peak at half amplitude (w) = 2
Call genfit() • Form model as before from coeffients & fit function Use vector subscript to extract correct function from vector function