Copyright © 2005. The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers Chapter 5B Model.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 ~ Curve Fitting ~ Least Squares Regression Chapter.
Advertisements

1 Functions and Applications
Probabilistic & Statistical Techniques Eng. Tamer Eshtawi First Semester Eng. Tamer Eshtawi First Semester
EGR 105 Foundations of Engineering I
Scatter Diagrams and Linear Correlation
ENGR-25_Plot_Model-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Registered Electrical.
Excel Part III Curve-Fitting, Regression Section 8 Fall 2013 EGR 105 Foundations of Engineering I.
© The McGraw-Hill Companies, Inc., 2000 CorrelationandRegression Further Mathematics - CORE.
CITS2401 Computer Analysis & Visualisation
Regression Regression: Mathematical method for determining the best equation that reproduces a data set Linear Regression: Regression method applied with.
Lecture 3 Cameron Kaplan
Engineering Fundamentals and Problem Solving, 6e Chapter 5 Representation of Technical Information.
Least Square Regression
Curve-Fitting Regression
Least Square Regression
EGR 105 Foundations of Engineering I Fall 2007 – week 7 Excel part 3 - regression.
The Islamic University of Gaza Faculty of Engineering Civil Engineering Department Numerical Analysis ECIV 3306 Chapter 17 Least Square Regression.
Part 4 Chapter 13 Linear Regression
EGR 105 Foundations of Engineering I Fall 2007 – week 7 Excel part 3 - regression.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. by Lale Yurttas, Texas A&M University Chapter 171 CURVE.
Math 227 Elementary Statistics Math 227 Elementary Statistics Sullivan, 4 th ed.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. by Lale Yurttas, Texas A&M University Chapter 171 Least.
Chapter 5 Plotting Data Curve Fitting, Good Graphing Practices Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Correlation & Regression Math 137 Fresno State Burger.
Correlation and Linear Regression
Descriptive Methods in Regression and Correlation
Physics 114: Lecture 17 Least Squares Fit to Polynomial
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 ~ Curve Fitting ~ Least Squares Regression Chapter.
Copyright © 2010, 2007, 2004 Pearson Education, Inc. All Rights Reserved Section 10-3 Regression.
Introduction to MATLAB for Engineers, Third Edition Chapter 6 Model Building and Regression PowerPoint to accompany Copyright © The McGraw-Hill Companies,
1 Chapter 3: Examining Relationships 3.1Scatterplots 3.2Correlation 3.3Least-Squares Regression.
3/2003 Rev 1 I – slide 1 of 33 Session I Part I Review of Fundamentals Module 2Basic Physics and Mathematics Used in Radiation Protection.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Part 4 Curve Fitting.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
© The McGraw-Hill Companies, Inc., Chapter 11 Correlation and Regression.
MECN 3500 Inter - Bayamon Lecture 9 Numerical Methods for Engineering MECN 3500 Professor: Dr. Omar E. Meza Castillo
Curve Fitting and Regression EEE 244. Descriptive Statistics in MATLAB MATLAB has several built-in commands to compute and display descriptive statistics.
Chapter 8 Curve Fitting.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers Chapter 5B Model.
The Hong Kong Polytechnic University Industrial Centre 1 MatLAB Lesson 4 : Polynomial Edward Cheung Room W311g 2008.
ES 240: Scientific and Engineering Computation. Chapter 13: Linear Regression 13. 1: Statistical Review Uchechukwu Ofoegbu Temple University.
Curve-Fitting Regression
CHAPTER 3 Model Fitting. Introduction Possible tasks when analyzing a collection of data points: Fitting a selected model type or types to the data Choosing.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers Chapter 5 Advanced.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® Chapter 5 Advanced.
11/20/2015ENGR 111A - Fall MatLab – Palm Chapter 5 Curve Fitting Class 14.1 Palm Chapter:
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers Chapter 5 Advanced.
Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering.
1 Data Analysis Linear Regression Data Analysis Linear Regression Ernesto A. Diaz Department of Mathematics Redwood High School.
^ y = a + bx Stats Chapter 5 - Least Squares Regression
MODEL FITTING jiangyushan. Introduction The goal of model fitting is to choose values for the parameters in a function to best describe a set of data.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Part 4 Chapter 15 General Least Squares and Non- Linear.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 ~ Curve Fitting ~ Least Squares Regression.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers Chapter 5 Advanced.
Copyright © Cengage Learning. All rights reserved. 8 9 Correlation and Regression.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Applied Numerical Methods With MATLAB ® for Engineers.
Copyright © Cengage Learning. All rights reserved.
Chapter 8 Linear Regression.
1 Functions and Applications
Part 5 - Chapter
Part 5 - Chapter 17.
Statistics 101 Chapter 3 Section 3.
Introduction to MATLAB 7
Part 5 - Chapter 17.
MatLab – Palm Chapter 5 Curve Fitting
M248: Analyzing data Block D UNIT D2 Regression.
Least Square Regression
Ch 4.1 & 4.2 Two dimensions concept
Presentation transcript:

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers Chapter 5B Model Building PowerPoint to accompany

Agenda Model Building Function Discovery Curve Fitting Linear Least Squares Regression 3D Plotting

Model Building Collect a batch of experimental data data contains noise perhaps irregular sampling To be useful, data must be fit by math model reduces noise provides estimates between samples How to choose model to fit? linear? polynomial? exponential?

3 Common Fitting Fns The linear function y  mx  b Its slope is m and its intercept is b. The power function y  bx m x raised to some power m m does not need to be an integer or positive no other terms – simpler than polynomial The exponential function y  b(10) mx also written as y  be mx base (10 or e) raised to some power mx In all 3, fitting involves estimating m and b !

Function Discovery – Which is it??? involves plotting data on linear, semilogy and loglog scales and looking for straight line behavior The power function y  2x  0  5 and the exponential function y  10 1  x  Figure 5.3–8 5-55

Plots tell which model to use Each function gives a straight line when plotted using a specific set of axes: 1. The linear function y  mx  b gives a straight line when plotted on rectilinear axes. Its slope is m and its intercept is b. 2. The power function y  bx m gives a straight line when plotted on log-log axes. 3. The exponential function y  b(10) mx and its equivalent form y  be mx give a straight line when plotted on a semilog plot whose y-axis is logarithmic More? See pages

Steps for Function Discovery 1) Examine the data near the origin. The exponential function can never pass through the origin (unless of course b  0, which is a trivial case). (See Figure 5.5–1 for examples with b  1.) The linear function can pass through the origin only if b  0. The power function can pass through the origin but only if m  0. (See Figure 5.5–2 for examples with b  1.)‏ 5-57

Examples of exponential functions. Figure 5.5–1 5-58

Examples of power functions. Figure 5.5–2 5-59

2. Plot the data using rectilinear scales. If it forms a straight line, then it can be represented by the linear function and you are finished. Otherwise, if you have data at x  0, then a. If y(0)  0, try the power function. b. If y(0)  0, try the exponential function. If data is not given for x  0, proceed to step Steps for Function Discovery (continued)‏ (continued…)‏

3. If you suspect a power function, plot the data using log- log scales. Only a power function will form a straight line on a log-log plot. If you suspect an exponential function, plot the data using the semilog scales. Only an exponential function will form a straight line on a semilog plot Steps for Function Discovery (continued) ‏ (continued…)‏

4. In function discovery applications, we use the log-log and semilog plots only to identify the function type, but not to find the coefficients b and m. The reason is that it is difficult to interpolate on log scales Steps for Function Discovery (continued) ‏

Plot and determine which curve fits best Read on to learn how to fit...

Command p = polyfit(x,y,n)‏ Description Fits a polynomial of degree n to data described by the vectors x and y, where x is the independent variable. Returns a row vector p of length n + 1 that contains the polynomial coefficients in order of descending powers. Fitting the Data – novel use of polyfit The polyfit command fits polynomials more complicated than the fits we want We can use polyfit to solve for linear, exponential, and power fits 5-63

Using Polyfit for Linear Curve Fit Syntax: p = polyfit(x,y,1)‏ where x and y contain the data, 1 means the polynomial to be fitted is linear, and p is the vector of coefficients, [p1 p2]. The linear fit results: given the model y = mx + b, the fit results indicate a best linear fit is made with ymod = p1 * x + p2, where ymod is the model's calculation of y. In other words, the first element p 1 of the vector p will be m, and the second element p 2 will be b. 5-64

Fit for Problem 5-30a) best described by a linear function x=[ ]; y=[ ]; pfit=polyfit(x,y,1); % derive fit xmodel=25:45; % construct fit curve ymodel=polyval(pfit,xmodel); % y values for model plot(x,y,'*'); hold on; plot(xmodel, ymodel), xlabel('x'), ylabel('y') text(35,200,'pfit= ')

Plot for Ex5-30a)

Using Polyfit for Power Curve Fit The power function: y  bx m. In this case log 10 y  m log 10 x  log 10 b(5.5–5)‏ which has the form w  p 1 z  p 2 where the polynomial variables w and z are related to the original data variables x and y by w  log 10 y and z  log 10 x. Thus we can find the power function that fits the data by typing p = polyfit(log10(x),log10(y),1)‏ The first element p 1 of the vector p will be m, and the second element p 2 will be log 10 b. We can find b from b  10 p

Using Polyfit for Exponential Curve Fit The exponential function: y  b(10) mx. In this case log 10 y  mx  log 10 b (5.5–6)‏ which has the form w  p 1 z  p 2 where the polynomial variables w and z are related to the original data variables x and y by w  log 10 y and z  x. We can find the exponential function that fits the data by typing p = polyfit(x, log10(y),1)‏ The first element p 1 of the vector p will be m, and the second element p 2 will be log 10 b. We can find b from b  10 p More? See pages

Now fit the curves to the data

and another...

Residuals (optional) It can be helpful to plot the model error samples directly—the residuals For Ex5-30a) this would involve: make another vector of model data that aligns with original data and subtract: ymod2=polyfit(pfit,x) ; % original x vector resid=y – ymod2; % vector of model errors plot(x, resid); Residual plots can identify "outliers" that might have an undue influence on model

Plot of ex5-30 with residuals

The Least Squares Criterion: used to fit a function f (x). It minimizes the sum of the squares of the residuals, J. J is defined as We can use this criterion to compare the quality of the curve fit for two or more functions used to describe the same data. The function that gives the smallest J value gives the best fit. m  i1i1 J  [ f (x i ) – y i ] 2 (5.6–1)‏ 5-72 Least Squares Fitting

Illustration of the least squares criterion. Figure 5.6–1 5-73

The least squares fit for the example data. Figure 5.6– See pages

p = polyfit(x,y,n)‏ Fits a polynomial of degree n to data described by the vectors x and y, where x is the independent variable. Returns a row vector p of length n+1 that contains the polynomial coefficients in order of descending powers The polyfit function is based on the least-squares method. Its syntax is See page 315, Table

Regression using polynomials of first through fourth degree. Figure 5.6– The program is on pages 315 to 316.

Beware of using polynomials of high degree. An example of a fifth-degree polynomial that passes through all six data points but exhibits large excursions between points. Figure 5.6–4 5-77

Assessing the Quality of a Curve Fit: Denote the sum of the squares of the deviation of the y values from their mean y by S, which can be computed from (y t – y ) 2 (5.6–2)‏ m  i1i1 S S  5-78

This formula can be used to compute another measure of the quality of the curve fit, the coefficient of determination, also known as the r-squared value. It is defined as (5.6–3)‏ J S r 2  1  5-79 The value of S indicates how much the data is spread around the mean, and the value of J indicates how much of the data spread is unaccounted for by the model. Thus the ratio J / S indicates the fractional variation unaccounted for by the model.

For a perfect fit, J  0 and thus r 2  1. Thus the closer r 2 is to 1, the better the fit. The largest r 2 can be is 1. It is possible for J to be larger than S, and thus it is possible for r 2 to be negative. Such cases, however, are indicative of a very poor model that should not be used. As a rule of thumb, a good fit accounts for at least 99 percent of the data variation. This value corresponds to r 2  More? See pages

Try it out... Remember, J is sum of square of error read on for hints on calculation...

Calculating J, S, r Suppose we have a vector of x and y data pfit = polyfit(x, y, 2) ; % a quadratic fit ymod = polyval(pfit, x) ; % the model y values error = ymod - y; J = sum(error.^2) % sum of square of err ymean = mean(y); S = sum( (y-ymean).^2) r2 = 1 - J / S

Another LLS possibility model does not have to be polynomial calculate w = log(x) then use polyfit on w and y

Extra Credit: 2 pts

Three-Dimensional Line Plots: The following program uses the plot3 function to generate the spiral curve shown in Figure 5.8–1. t = [0:pi/50:10*pi]; plot3(exp(-0.05*t).*sin(t),exp(0.05*t).*cos(t),t) xlabel('x'),ylabel('y'),zlabel('z'),grid 5-89 See the next slide.

The curve x  e  0.05t sin t, y  e  0.05t cos t, z  t plotted with the plot3 function. Figure 5.8– More? See pages

Surface Plots: The following session shows how to generate the surface plot of the function z  xe  [(x  y 2 ) 2  y 2 ], for  2  x  2 and  2  y  2, with a spacing of 0.1. This plot appears in Figure 5.8–2. [X,Y] = meshgrid(-2:0.1:2); Z = X.*exp(-((X-Y.^2).^2+Y.^2)); mesh(X,Y,Z),xlabel('x'),ylabel('y'), zlabel(’z’)‏ 5-91 See the next slide.

A plot of the surface z  xe  [(x  y 2 ) 2  y 2 ] created with the mesh function. Figure 5.8– More? See pages

Contour Plots: The following session generates the contour plot of the function whose surface plot is shown in Figure 5.8–2; namely, z  xe  [(x  y 2 ) 2  y 2 ], for  2  x  2 and  2  y  2, with a spacing of 0.1. This plot appears in Figure 5.8–3. [X,Y] = meshgrid(-2:0.1:2); Z = X.*exp(-((X- Y.^2).^2+Y.^2)); contour(X,Y,Z),xlabel('x'),ylabel('y')‏ 5-93 See the next slide.

A contour plot of the surface z  xe  [(x  y 2 ) 2  y 2 ] created with the contour function. Figure 5.8– More? See page 337.

Function contour(x,y,z)‏ mesh(x,y,z)‏ meshc(x,y,z)‏ meshz(x,y,z)‏ surf(x,y,z)‏ surfc(x,y,z)‏ [X,Y] = meshgrid(x,y)‏ [X,Y] = meshgrid(x)‏ waterfall(x,y,z)‏ Description Creates a contour plot. Creates a 3D mesh surface plot. Same as mesh but draws contours under the surface. Same as mesh but draws vertical reference lines under the surface. Creates a shaded 3D mesh surface plot. Same as surf but draws contours under the surface. Creates the matrices X and Y from the vectors x and y to define a rectangular grid. Same as [X,Y]= meshgrid(x,x). Same as mesh but draws mesh lines in one direction only. Three-dimensional plotting functions. Table 5.8–15-95

Plots of the surface z  xe  (x 2  y 2 ) created with the mesh function and its variant forms: meshc, meshz, and waterfall. a) mesh, b) meshc, c) meshz, d) waterfall. Figure 5.8–4 5-96

Use plot3

Use meshgrid, mesh, contour