PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line()...

20
PH24010 MathCAD More Curve Fitting

Transcript of PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line()...

Page 1: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

PH24010

MathCADMore Curve Fitting

Page 2: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.
Page 3: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

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

Page 4: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

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

Page 5: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

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’

Page 6: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

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

Page 7: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

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

Page 8: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

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

Page 9: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

Compare model with data

0 0.1 0.20

5

10

15

Current

BulbModelv( )

Voltage v

Page 10: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

Peak fitting

• Applications in spectroscopy

• Gaussian peak

• need to find:– position– amplitude– background– width

Page 11: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

Gaussian Peak Fit

10 5 0 5 105

0

5

10

GaussY

GaussX

Page 12: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

genfit()

• Generalised fit of any function

• Need:– model function– partial derivatives of model wrt parameters– vector of initial guesses for each parameter

Page 13: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

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

Page 14: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

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)

Page 15: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

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

Page 16: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

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

Page 17: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

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

Page 18: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

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

Page 19: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

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

Page 20: PH24010 MathCAD More Curve Fitting. Previously on PH24010 Linear Fitting slope(), intercept() line() Pre-process Y-data: –y = k/x –y = e kx.

Plot model with data

10 5 0 5 105

0

5

10

DataGaussian Fit

y0

y0 A