MATH 51: MATLAB HOMEWORK 3

Similar documents
Sketching graphs of polynomials

Meeting 1 Introduction to Functions. Part 1 Graphing Points on a Plane (REVIEW) Part 2 What is a function?

This is called the vertex form of the quadratic equation. To graph the equation

ACTIVITY 8. The Bouncing Ball. You ll Need. Name. Date. 1 CBR unit 1 TI-83 or TI-82 Graphing Calculator Ball (a racquet ball works well)

CCNY Math Review Chapter 2: Functions

Optimizations and Lagrange Multiplier Method

, etc. Let s work with the last one. We can graph a few points determined by this equation.

5.1 Introduction to the Graphs of Polynomials

Math 182. Assignment #4: Least Squares

PR3 & PR4 CBR Activities Using EasyData for CBL/CBR Apps

The Interpolating Polynomial

Sample: Do Not Reproduce QUAD4 STUDENT PAGES. QUADRATIC FUNCTIONS AND EQUATIONS Student Pages for Packet 4: Quadratic Functions and Applications

Name. Center axis. Introduction to Conic Sections

Beginning of Semester To Do List Math 1314

UNIT 8: SOLVING AND GRAPHING QUADRATICS. 8-1 Factoring to Solve Quadratic Equations. Solve each equation:

Finding the Maximum or Minimum of a Quadratic Function. f(x) = x 2 + 4x + 2.

MATH SPEAK - TO BE UNDERSTOOD AND MEMORIZED

5.5 Newton s Approximation Method

1

3.7. Vertex and tangent

Honors Algebra 2 Function Transformations Discovery

Exercise: Graphing and Least Squares Fitting in Quattro Pro

10.3 vertex and max values with comparing functions 2016 ink.notebook. March 14, Vertex and Max Value & Page 101.

Student Exploration: Quadratics in Polynomial Form

Quadratic Functions Dr. Laura J. Pyzdrowski

EXAMPLE. 1. Enter y = x 2 + 8x + 9.

Section 4.4: Parabolas

The Bisection Method versus Newton s Method in Maple (Classic Version for Windows)

Topic. Section 4.1 (3, 4)

ME422 Mechanical Control Systems Matlab/Simulink Hints and Tips

Matlab and Coordinate Systems

MATHS METHODS QUADRATICS REVIEW. A reminder of some of the laws of expansion, which in reverse are a quick reference for rules of factorisation

Algebra II Quadratic Functions and Equations - Extrema Unit 05b

Cubic Splines By Dave Slomer

UNIT 3B CREATING AND GRAPHING EQUATIONS Lesson 4: Solving Systems of Equations Instruction

2. The diagram shows part of the graph of y = a (x h) 2 + k. The graph has its vertex at P, and passes through the point A with coordinates (1, 0).

1.1 Functions. Cartesian Coordinate System

QUADRATIC AND CUBIC GRAPHS

Quadratic Functions. *These are all examples of polynomial functions.

The simplest quadratic function we can have is y = x 2, sketched below.

9.1 Linear Inequalities in Two Variables Date: 2. Decide whether to use a solid line or dotted line:

Module 3: Stand Up Conics

Social Science/Commerce Calculus I: Assignment #6 - Solutions Page 1/14

18.02 Multivariable Calculus Fall 2007

Optimization and least squares. Prof. Noah Snavely CS1114

Chapter 1 Polynomials and Modeling

Quadratic Functions. Chapter Properties of Quadratic Functions... p Investigating Quadratic Functions... p. 6 in Vertex Form: Part 1

Chapter 12: Quadratic and Cubic Graphs

CHAPTER 6 Quadratic Functions

3-1 Writing Linear Equations

Tutorial Four: Linear Regression

x y

Core Mathematics 3 Functions

College Algebra. Fifth Edition. James Stewart Lothar Redlin Saleem Watson

2.3 Graph Sketching: Asymptotes and Rational Functions Math 125

Why Use Graphs? Test Grade. Time Sleeping (Hrs) Time Sleeping (Hrs) Test Grade

Lecture 8. Divided Differences,Least-Squares Approximations. Ceng375 Numerical Computations at December 9, 2010

Algebra II: Strand 3. Quadratic Functions; Topic 2. Digging Deeper; Task 3.2.1

Yimin Math Centre. Year 10 Term 2 Homework. 3.1 Graphs in the number plane The minimum and maximum value of a quadratic function...

Test 3 review SHORT ANSWER. Write the word or phrase that best completes each statement or answers the question.

A-SSE.1.1, A-SSE.1.2-

II. Functions. 61. Find a way to graph the line from the problem 59 on your calculator. Sketch the calculator graph here, including the window values:

Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

Section 7.2 Characteristics of Quadratic Functions

Linear and Quadratic Least Squares

Section Graphs and Lines

Unit #3: Quadratic Functions Lesson #13: The Almighty Parabola. Day #1

Hot X: Algebra Exposed

Algebra 2 Honors Lesson 10 Translating Functions

Polynomial and Rational Functions. Copyright Cengage Learning. All rights reserved.

Exploring Quadratic Graphs

MATLAB QUICK START TUTORIAL

Here is a quick introduction to Matlab and a couple of its symbolic and control functions.

Lagrange Multipliers and Problem Formulation

Module 3: Graphing Quadratic Functions

Dynamics and Vibrations Mupad tutorial

4 Visualization and. Approximation

Kevin James. MTHSC 102 Section 1.5 Polynomial Functions and Models

Interpolation. TANA09 Lecture 7. Error analysis for linear interpolation. Linear Interpolation. Suppose we have a table x x 1 x 2...

Section 1.5 Transformation of Functions

Rational functions, like rational numbers, will involve a fraction. We will discuss rational functions in the form:

Math 213 Exam 2. Each question is followed by a space to write your answer. Please write your answer neatly in the space provided.

Graphs of Equations. MATH 160, Precalculus. J. Robert Buchanan. Fall Department of Mathematics. J. Robert Buchanan Graphs of Equations

Properties of Quadratic functions

You should be able to plot points on the coordinate axis. You should know that the the midpoint of the line segment joining (x, y 1 1

To Measure a Constant Velocity. Enter.

Unit: Quadratic Functions

Matlab and Coordinate Systems

Note that ALL of these points are Intercepts(along an axis), something you should see often in later work.


Graphing with a Graphing Calculator

EXERCISE SET 10.2 MATD 0390 DUE DATE: INSTRUCTOR

F.BF.B.3: Graphing Polynomial Functions

1 MATH 253 LECTURE NOTES for FRIDAY SEPT. 23,1988: edited March 26, 2013.

Maximizing an interpolating quadratic

Quadratic Functions, Part 1

Remember to SHOW ALL STEPS. You must be able to solve analytically. Answers are shown after each problem under A, B, C, or D.

Math F302: Octave Miscellany September 28, e 1 x3 dx. Here s how to find a numerical approximation with Octave

Section 9.3: Functions and their Graphs

MA 323 Geometric Modelling Course Notes: Day 14 Properties of Bezier Curves

Transcription:

MATH 5: MATLAB HOMEWORK Experimental data generally suffers from imprecision, though frequently one can predict how data should behave by graphing results collected from experiments. For instance, suppose an experimenter observing a falling anvil gathers the following data (measured in seconds and meters per second, as appropriate): time t velocity v t time t velocity v t... 9.6.5 4.9.5 4.. 9.7 4. 9..5 4.4 4.5 4.9. 9. 5. 49..5 4. One can verify quickly that these points don t lie on a line, but when we plot them we see that they nearly do: 5 45 4 5 5 5 5.5.5.5.5 4 4.5 5 Such observations are the critical step which allow experimental data to develop into theories which predict future behavior. A natural question, then, is to find the line which best fits the given data set. This problem set will deal with solving problems such as this. The technology we will use to solve these problems is multivariable calculus and basic linear algebra. The first section will discuss one of the most common types of data fitting. In the second section we will use MATLAB to fit data points to higher degree polynomials. In the final section, we will experiment with modifications to the least squares approach to curve fitting.

MATH 5: MATLAB HOMEWORK. Fitting Lines with Least Squares We first attempt to find the best linear approximation to a collection of points. More specifically, given a collection of points (x, y ),, (x n, y n ) in the plane (hereafter called our data set), we seek the line y = mx + b which is closest to our collection. Of course, our answer depends on how we interpret the word closest, and there are plenty of viable interpretations. One common interpretation is to say that a line y = mx+b is closest to the data set (x, y ),, (x n, y n ) provided the quantity n (y i (mx i + b)) i= is minimal. Notice that the term y i (mx i + b) measures up to sign the distance between the actual value of the the y-variable corresponding to x i and the predicted value of the y-variable corresponding to x i. Hence each summand measures the square of this distance, and so we are minimizing the sum of these squares. Perhaps not suprisingly, therefore, this technique is called least squares approximation. In the rest of this section we ll solve the least squares problem for a given collection of data points using multivariable calculus and MATLAB. Example. Suppose we are given the points (, ), (, ) and (4, ) and asked to find the line which is a best approximation to this data set using the Least Squares technique. 4.5.5.5.5.5.5.5.5 4 4.5 5 From our discussion above, this is the same as finding values of m and b so that f(m, b) := ( (m + b)) + ( (m + b)) + ( (m 4 + b)) is minimized. Since we ll be using MATLAB to minimize f(m, b), we first enter this polynomial into MATLAB. After declaring m and b to be variables using the syms command, we define f as the desired polynomial. >> syms m b; >> f=(-(m + b))^ + (-(*m + b))^ + (-(4*m + b))^ f = (-m-b)^+(-*m-b)^+(-4*m-b)^ Note: If you don t first declare m and b as variables, MATLAB will have a fit.

MATH 5: MATLAB HOMEWORK So we are left with finding values of m and b that minimize the polynomial f(m, b) above. Of course, we use multivariable calculus to solve this problem. Indeed, the minimal value for this polynomial is given by solving =, which we can (again!) use MATLAB to compute f m = f b First we compute the partial derivatives using the diff function in MATLAB. The command diff(f,m) computes the partial derivative of f with respect to m, and similarly diff(f,b) computes the partial derivative of f with respect to b. >> diff(f,m) ans = -8+4*m+4*b >> diff(f,b) ans = -4+4*m+6*b Hence our minimizing values will be solutions to the system 8 + 4m + 4b = 4 + 4m + 6b =. This is equivalent to solving the matrix equation ( ) ( ) ( ) 4 4 m 8 =. 4 6 b 4 >> A=[4, 4; 4, 6]; B=[8; 4]; >> A^-*B ans =.574. Hence the line which solves the least squares problem for the points (, ), (, ) and (4, 5) is given (approximately) by y =.574x +. 4.5.5.5.5.5.5.5.5 4 4.5 5 Values where f m = f = need not be local minima of the function, just as a critical point is not necessarily a minimum of b a function of one variable. One should really check that the critical values we compute produce bona fide minima of the function, but we won t here.

4 MATH 5: MATLAB HOMEWORK Exercises () Use the least squares technique to find the line which best fits the following collection of points (, ), (,.5), (,.5), (,.5), (,.5), (,.5), (, ), (4,.5). () One doesn t have to limit the least squares technique to finding lines which fit data sets in the plane. Indeed, if (x, y, z ),, (x n, y n, z n ) are points in space, one can try to find the plane z = c ax by which best fits the given data set. This is done by finding values of a, b and c which minimize the quantity n (z i (c ax i by i )). i= Using techniques analogous to those developed above, find the plane which best fits the data set (,, ), (,, 4), (5, 6, 7), (8,, ), (,, 5). For the pedants: we re excluding planes which are vertical by considering only those of the form z = c ax by, just as when using least squares to fit lines we exclude veritical lines by assuming a line is of the form y = mx + b.

MATH 5: MATLAB HOMEWORK 5. Fitting in Practice Although fitting lines to data sets can be very useful, in practice we often find that data sets are not wellapproximated by straight lines. More often, a data set can be much better approximated by a polynomial of higher degree, such as a parabola or cubic equation. Although fitting higher degree polynomials is straightforward generalization of fitting lines, it can present a computational burden to manually fit polynomials of higher and higher degree. Fortunately for us weary mathematicians, MATLAB can greatly simplify our calculations. We ll learn how to use MATLAB to do this by working through an example. Suppose we wish to find the polynomial which best approximates the points (, 8.5), (, 4.), (,.8), (,.), (, ), (,.8), and (, 9.). The first question we should ask ourselves is: What type of polynomial should we guess? For instance, do the points almost seem to lie on a straight line? If not, do they look like they might be better fitted by a parabola? To help answer these questions, we first need to plot the points. We can use MATLAB to do this. First, we store the x- and y-coordinates of the given points: >> x = [- - - ]; y = [8.5 4..8..8 9.]; (The semi-colon at the end of the line suppresses the output.) We then ask MATLAB to plot our points: >> plot(x,y,'o'), grid on A new window will open, with the following graph: 9 8 7 6 5 4 From the graph, it appears that the points don t lie near a straight line, but might lie near a parabola. We can use MATLAB to find the best parabola approximating these points. The appropriate command is called polyfit, and it requires three inputs: the x- and y-coordinates of the points to be fit, and the degree of the polynomial we want to fit to the points. In our case, we enter: >> p = polyfit(x,y,) p =.9845.464.9

6 MATH 5: MATLAB HOMEWORK The output reads off the coefficients of the degree polynomial best fitting the data, starting with the highest power. In our case, then, we see that the quadratic best fitting out data set is given by p(x) =.9845x +.464x +.9. We can plot this polynomial on our previous graph, to see how well it actually fits the data: >> x = -:.:; y = polyval(p,x); >> plot(x,y,'o',x,y), grid on 9 8 7 6 5 4 The parabola appears to fit the points quite well. However, suppose we tried to fit a cubic to these points: >> p = polyfit(x,y,); >> x = -:.:; y = polyval(p,x); >> plot(x,y,'o',x,y), grid on 9 8 7 6 5 4 It appears that the cubic fits the data points equally well, if not better! If we expand our view of the graph, however, we see that we may have been deceived: >> x = -:.:; y = polyval(p,x); >> plot(x,y,'o',x,y), grid on

MATH 5: MATLAB HOMEWORK 7 4 x 4 8 6 4 4 6 8 The long-term behavior of this function is not what we would predict based only on the original data points. For example, based on the data points alone, there is no reason to expect the points should be negative for large negative values of x. As such, the parabola seems to represent a better fit to the points. In general, we look for the polynomial of smallest degree which seems to closely fit the data points.

8 MATH 5: MATLAB HOMEWORK.. Exercises. () Find the quartic polynomial which best fits the following points: ( 5, 6), (, 5), (, 4), (, ), (, ), (, ), (4, ), (5, 9) () Sketch a graph of this quartic, along with the original data points. () Repeat the previous two exercises for fitting a quadratic curve to the points.

MATH 5: MATLAB HOMEWORK 9 (4) Find the polynomial of smallest degree which appears to best approximate the following points, and sketch a graph of this polynomial along with the data points: (.7, 8), (.7, ), (.8, 5.5), (., 8.7), (, 4), (.6,.), (.6, 4)

MATH 5: MATLAB HOMEWORK. Bonus: An Approach other than Least Squares There are many interpretations for what a best approximation to a set of data points can mean. In the Least Squares approach, the best line minimizes the sum of the squares of the differences between the y values of the given points and the corresponding y values of the line. (x i, y i ) y = mx + b (x i, mx i + b) We could, however, consider minimizing the actual distances between the given points and the line. (x i, y i ) y = mx + b (x i, mx i + b) () For a given line l with equation y = mx + b and point P = (x i, y i ), find the distance between P and l.

MATH 5: MATLAB HOMEWORK () Suppose we are given the points (, ), (, ), and (4, ). Find the line l which best approximates these points, in the sense that the sum of the distances between the given points and l is minimized. () Compare this line with the line found using Least Squares.