Presentation is loading. Please wait.

Presentation is loading. Please wait.

Empirical Model Building Ib: Objectives: By the end of this class you should be able to: Determine the coefficients for any of the basic two parameter.

Similar presentations


Presentation on theme: "Empirical Model Building Ib: Objectives: By the end of this class you should be able to: Determine the coefficients for any of the basic two parameter."— Presentation transcript:

1 Empirical Model Building Ib: Objectives: By the end of this class you should be able to: Determine the coefficients for any of the basic two parameter models Plot the data and resulting fits Calculate and describe residuals Palm, Section 5.5 Download file FnDiscovery.mat and load into MATLAB

2 1. Below are three graphs of the same dataset. What is the name and equation of the likely model that would match this data?

3 2. Here are the plots for another dataset. Name the model and write its equation for this case

4 Two Parameter Models Three simple models available Can “make” the latter two linear by taking logs of the equation The resulting equations suggest plots where the data should form a straight line if the model is true. What did you get for the three problems? Problem 1: linear y = mx + b Problem 2: power y = bx m Problem 3: exponential y = b10 mx Note: on problem 3, power is also nearly straight. However, if similar the exponential model is slightly preferred over the power model.

5 How would you define the Best Fit line?

6 Fitting data plot data (see points at left) looks linear equation:y = mx + b need to find the “best” m & b (the parameters) what makes the best line? usual approach – minimize the squared distance between the points and the line in the y direction the predicted value is often displayed as and pronounced “y-hat” Calculate the predicted values then minimize the sum of squares next we look at how to do this

7 Fitting a Linear equation via matrices e.g., Fitting the Spring data Model:y = mx + b Setup: 1. Design Matrix: >> X = [ones(length(Force),1), Force] 2. Response Vector >> Y= Length Fit: find the fitted parameters >> B = X \ Y B will be Predict: calculate predicted y for each x >> Lhat = X*B Plot: plot the result >> plot(Force, Length, ‘p’, Force, Lhat) (plus labels...)

8 A Linear Model & it’s Design Matrix y = b(1) + mx Linear Model: Design Matrix: >> X = [ ones(4, 1), L’ ] Matlab Syntax: (to convert a row vector of x values to a design matrix)

9 Fitting a Linear Equation in Matrix Form >> B = X\Y Matrix Equation: The Full Matrices MATAB Syntax for finding the parameter matrix

10 Linear Equation in Matrix Form fits: >> yhat = X*B residuals: >> res = Y - X*B

11 Fitting Transformed models Same as linear model except set up design matrix (X) and response vector (Y) using the transformed variables e.g., the capacitor discharge from last time straight line on a semilog plot  what model is implied? Exponential y = b10 mx or in this example V = b10 mt  what is its linearized (transformed) form log(V) = log(b) + mt

12 E.G., Fitting the capacitor discharge data Model: Last lecture we found the data was straight on a semilog plot implying an exponential model. For the base-ten model the equations are: V = b10 mt orlog(V) = log(b) + mt Setup: 1. Design Matrix: >> X = [ones(length(t),1), t] 2. Response Vector >> Y= log10(V) Fit: determine parameters >> B = X \ Y Predict: Predict: >> logVhat = X*B Untransform: >> Vhat = 10.^logVhat or Untransform >> b = 10^B(1), m = B(2) Predict >> Vhat = b*10.^(m.*t) Plot: either on linear or semilog plot

13 Plotting the capacitor discharge fit: semilog plot (left below) >> semilogy(t, V, ‘s’, t, Vhat) >> xlabel ‘time (s.)’, ylabel ‘Voltage’ >> legend(‘Experimental data’, … ‘Fit: V = (95.8)10^{-0.43*t}’) linear plot (right below) >> plot(t, V, ‘s’, t, Vhat) >> xlabel ‘time (s.)’, ylabel ‘Voltage’ >> legend(‘Experimental data’, … ‘Fit: V = (95.8)10^{-0.43*t}’) or

14 notes: linear plot preferred when y axis range is reasonable, semilog is used when detail at the low values is required I usually set variables m & b equal to the correct values to prevent mistakes full labeling required notice use of up carrot and curly brackets {} to superscript the powers in the legend as always: data is points, equation is a line

15 EquationFitParameters linear x vs. y b = B(1) m= B(2) power log(x) vs. log(y) b=10^B(1) m=B(2), exponential x vs. ln(y) b=e^B(1) m=B(2), x vs. log(y) b=10^B(1) m=B(2) Function Discovery (Review) 2. Fitting Parameters (m & b)

16 Fitting a 2-parameter models Model: Identify Functional Form Plot data is it linear ? is it monotonic? Log-Log (loglog(x,y)) semilog (semilogy(x,y)) look for straight graph Setup: Transform Data to Linearize Create X & Y matrices Fit linear model to transformed data Predict and Untransform Parameters to m & b Plot: Plot data and predicted equation. “Normal Data”“Transformed Data”

17 Steps in Function Discovery & Fitting Step 1: Model - Use graphs to determine if function is linear, exponential or power. Write down the equation to be fitted Step 2: Setup – create design matrix (X) & response vector (Y) for a model that will be linear (I.E., use transformed data for exponential and power models) Step 3: Fit – Determine parameters for a linear model (B = X\Y) Step 4: Predict – Untransform: if exponential or power untransform the b coefficient Apply model to predict y values Step 5: Plot - data and model – fix any problems encountered.

18 Class Exercise: For problems 3 (x2 vs. y2) from last class: What type of model will likely fit this data? (from last time) Determine the full model including parameter values. Plot the data and the fitted curve on one plot For problem 2 (x1 vs. y1), repeat the above.

19 Handout Problem 3: (students to try) uses: x2, y2 Fitting an exponential model: Model: y = b 10 mx or log(y) = log(b) + mx Setup: >> X = [ones(length(x2),1), x2’]; Y = log10(y2’); Fit: >> B = X \ Y B = 5.3143 -0.0067 Predict: >> y2hat =10.^(X*B) ( & Untransform) or >> m = B(2), b = 10^B(1) m = -0.0067 b = 2.0622e+005 >> y2hat = b*10.^(m.*x2) Plot: >> plot(x2, y2, 's', x2, y2hat) the resulting prediction curve is not smooth. It is a good idea to smooth this out by simply adding more x points

20 Commands used to create a graph with a smooth prediction curve >> x2p = linspace(min(x2), max(x2),50); >> y2hat = b*10.^(m.*x2p); >> plot(x2, y2, 's', x2p, y2hat) >> legend(‘Experimental Data’, ‘Fit: y2 = (2.06*10^5)*10^{-0.0067*x2}’) >> xlabel ‘x2’, ylabel ‘y2’

21 Handout Problem 2: uses variables x1, y1, Fitting a power law model Model: log-log plot is linear  power model y1 = b(x1) m orlog(y1) = log(b) + m log(x1) Setup: >> X = [x1’./x1’, log10(x1’)]; Y = log10(y1’); Fit: >> B = X\Y B = 3.5541 -0.9764 Predict: >> y1hat = 10.^(X*B); or (& Untransform) >> m = B(2), b = 10^B(1) m = -0.9764 b = 3.5821e+003 >> y1hat = b*x1.^m; Plot: >> plot(x1, y1, 's', x1, y1hat) >> legend(‘Experimental Data’, ‘Fit: y = 3582*x^{-0.976}’) >> xlabel ‘x1’, ylabel ‘y1’

22 Notice that this function is very nearly 1/x. It would be a good idea to fit this case and see if this simpler model works as well. Model: y1 = m(1./x1) Setup: Design Matrix: X = (1./x1)’ Response Vector Y = y1’ Fit: m = X \ Y Predict: Yhat = X*m Plot the results Extra, if time: How could you fit 1/x? 

23

24 xy 15 28 310 420 521 629 734 836 945 Please plot this data and determine: the likely model parameters (m&b) (data is available in FnDiscovery.mat) plot resulting data and model

25 A Reminder of Some Nomenclature: y  response (dependent variable) vector y i  an individual response x  predictor (independent variable) vector x i  an individual predictor value  the predicted value (the fits)  an individual predicted value (fit)

26 Create x & y vectors (notice transpose operator): >> x = (1:9)’; y =[5, 8, 10, 20, 21, 29, 34, 36, 45]’; Model: Check linear plot >> plot(x,y,'s') Graph is linear, fit a simple linear model and calculate predicted values >> X = [x./x, x]; Y = y;% Set up: design and response matrix >> B = X \ Y% Fit: parameters B = -1.9722 5.0167% Untransform: not needed for linear model >> yhat = X*B;% Predict: calculate fitted values (the fits) >> plot(x, y, ‘p', x, yhat) % Plot: graph with labels >> xlabel 'x', ylabel 'y' >> legend('Experimental Data', 'Fit: y = -1.97 + 5.02*x')

27

28 Residuals: What is left after subtracting model from data: residuals = y – yhat Represents what is not fit by the model Ideal model should capture all systematic information Residuals should contain only random error Plot residuals and look for patterns

29 Now prepare a residuals plot Calculate residuals >> res = y – yhat Plot residuals vs. fits >> plot(yhat, res, 'p', yhat, 0*res) >> ylim([-4 4]) >> xlabel 'fitted value' >> ylabel 'residuals' Generally you make the y axis equal in both positive and negative direction (hence the ylim command above) notice zero line was also plotted for reference.

30 What to look for in a residual plot: 1. Does the residual plot look correct? data should vary about zero sum of residuals must equal zero 2. Are there any patterns in the residuals?, e.g., curvature: high center, low ends or l ow center, high ends changes is variability: the spread of the data in the y direction should be constant 3.How big are the residuals? (what is the magnitude of the y axis)

31 Thermocouple Calibration Data  is it linear? Plot this data Does it look linear? Fit a linear model Determine the residuals Prepare a residuals plot Is it linear? (data is available in FnDiscovery.mat) mV (mV) T(  C) 00 0.391010.0000 0.790020.0000 1.196030.0000 1.612040.0000 2.036050.0000 2.468060.0000 2.909070.0000 3.358080.0000 3.814090.0000 4.2790100.0000

32 Model: Plot data to see if it is linear >> plot(mV, T, ’p’) >> ylabel ‘Temperature(C)’, xlabel ‘Voltage(mV)’ Plot is linear so model is y = b + m x Setup: >> X = [ones(length(mV),1), mV] Fit: >> B = X \T B = 0.4024 24.8134 Predict: >> That = X*B Untransform: not necessary for a linear model Plot: >> plot(mV, T, ’p’, mV, That) plus labels and legend as we have done before Residuals: >> Res = T – That >> plot (That, Res, ‘p’, That, Res*0) >> ylabel ‘Residuals’ >> xlabel ‘Predicted Temperature (C)’


Download ppt "Empirical Model Building Ib: Objectives: By the end of this class you should be able to: Determine the coefficients for any of the basic two parameter."

Similar presentations


Ads by Google