MATH2070: LAB 3: Roots of Equations

Similar documents
MATH2070: LAB 3: Roots of Equations

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

f( x ), or a solution to the equation f( x) 0. You are already familiar with ways of solving

MATH2070: LAB 4: Newton s method

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

LECTURE 0: Introduction and Background

2.1 Basics of Functions and Their Graphs

AC : MATHEMATICAL MODELING AND SIMULATION US- ING LABVIEW AND LABVIEW MATHSCRIPT

Handout 2 - Root Finding using MATLAB

Lecture 7 Symbolic Computations

Reals 1. Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method.

MATLAB Lecture 4. Programming in MATLAB

Math 56 Homework 1. Matthew Jin. April 3, e n 10+ne. = O(n 1 ) n > n 0, where n 0 = 0 be- 10+ne

10.4 Linear interpolation method Newton s method

Math 2250 Lab #3: Landing on Target

Fall 2017: Numerical Methods I Assignment 1 (due Sep. 21, 2017)

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

1

Computational Photonics, Summer Term 2012, Abbe School of Photonics, FSU Jena, Prof. Thomas Pertsch

Unit 1 Algebraic Functions and Graphs

Exercises: Instructions and Advice

4.0 Programming with MATLAB

Sec 4.1 Coordinates and Scatter Plots. Coordinate Plane: Formed by two real number lines that intersect at a right angle.

(Refer Slide Time: 02:59)

Second Edition. Concept Builders. Jana Kohout

Here are some of the more basic curves that we ll need to know how to do as well as limits on the parameter if they are required.

Exploring Slope. We use the letter m to represent slope. It is the ratio of the rise to the run.

Dr Richard Greenaway

Laboratory 5: Implementing Loops and Loop Control Strategies

Vertical Line Test a relationship is a function, if NO vertical line intersects the graph more than once

CCNY Math Review Chapter 2: Functions

APPM 2460 PLOTTING IN MATLAB

Fondamenti di Informatica

Basics of Computational Geometry

MITOCW watch?v=kz7jjltq9r4

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple

CONNECTED SPACES AND HOW TO USE THEM

1 Introduction to Matlab

Lesson 20: Every Line is a Graph of a Linear Equation

MAT 003 Brian Killough s Instructor Notes Saint Leo University

This assignment is due the first day of school. Name:

DOWNLOAD PDF BIG IDEAS MATH VERTICAL SHRINK OF A PARABOLA

Scope of Variables. In general, it is not a good practice to define many global variables. 1. Use global to declare x as a global variable.

APPM 2460: Week Three For, While and If s

Computer Graphics Prof. Sukhendu Das Dept. of Computer Science and Engineering Indian Institute of Technology, Madras Lecture - 14

MATH2071: LAB 2: Explicit ODE methods

Cardinality of Sets. Washington University Math Circle 10/30/2016

Introduction to Matlab

AMS 27L LAB #1 Winter 2009

MAT 090 Brian Killough s Instructor Notes Strayer University

Math 1206 Calculus Sec. 5.6: Substitution and Area Between Curves (Part 2) Overview of Area Between Two Curves

Rational Numbers: Graphing: The Coordinate Plane

Hi everyone. Starting this week I'm going to make a couple tweaks to how section is run. The first thing is that I'm going to go over all the slides

Floating-Point Numbers in Digital Computers

Key Concepts: Economic Computation, Part II

Floating-Point Numbers in Digital Computers

3.7. Vertex and tangent

Physics 326G Winter Class 6

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221

Table of Contents. Introduction.*.. 7. Part /: Getting Started With MATLAB 5. Chapter 1: Introducing MATLAB and Its Many Uses 7

Chapter 1. Math review. 1.1 Some sets

Applied Calculus. Lab 1: An Introduction to R

Graphing Techniques. Domain (, ) Range (, ) Squaring Function f(x) = x 2 Domain (, ) Range [, ) f( x) = x 2

Top bar items. Current folder should be shown. Command Window This has the the prompt >>

Numerical Methods in Scientific Computation

Statistics Case Study 2000 M. J. Clancy and M. C. Linn

Introduction to Homogeneous coordinates

lab MS Excel 2010 active cell

What Secret the Bisection Method Hides? by Namir Clement Shammas

Question. Insight Through

Getting Started with MATLAB

Matlab and Octave: Quick Introduction and Examples 1 Basics

CSE/Math 485 Matlab Tutorial and Demo

MTH 122 Calculus II Essex County College Division of Mathematics and Physics 1 Lecture Notes #11 Sakai Web Project Material

Week 1: Introduction to R, part 1

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

A GENTLE INTRODUCTION TO THE BASIC CONCEPTS OF SHAPE SPACE AND SHAPE STATISTICS

ME 121 MATLAB Lesson 01 Introduction to MATLAB

Short Version of Matlab Manual

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Computational Mathematics/Information Technology. Worksheet 2 Iteration and Excel

Dynamics and Vibrations Mupad tutorial

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Table of Laplace Transforms

ECE 202 LAB 3 ADVANCED MATLAB

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Lab1: Use of Word and Excel

About Graphing Lines

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

Introduction to C++ Introduction to C++ Week 6 Dr Alex Martin 2013 Slide 1

Continuous functions and homeomorphisms

Calculus Limits Images in this handout were obtained from the My Math Lab Briggs online e-book.

JUST THE MATHS SLIDES NUMBER 5.2. GEOMETRY 2 (The straight line) A.J.Hobson

Shared Memory Programming With OpenMP Computer Lab Exercises

Unit 2: Function Transformation Chapter 1. Basic Transformations Reflections Inverses

MATLAB Introductory Course Computer Exercise Session

Chapter 1. Linear Equations and Straight Lines. 2 of 71. Copyright 2014, 2010, 2007 Pearson Education, Inc.

Statistics 13, Lab 1. Getting Started. The Mac. Launching RStudio and loading data

Math 1191 Mathematica Introduction

Transcription:

MATH2070: LAB 3: Roots of Equations 1 Introduction Introduction Exercise 1 A Sample Problem Exercise 2 The Bisection Idea Exercise 3 Programming Bisection Exercise 4 Variable Function Names Exercise 5 Exercise 6 The root finding problem is the task of finding one or more values for a variable x so that an equation f(x) = 0 is satisfied. Denote the desired solution as x 0. As a computational problem, we are interested in effective and efficient ways of finding a value x so that f(x), called the residual error, is small. We hope this means that x x 0, called the approximation error is small. Today we will look at some nonlinear equations and a very simple method of finding an approximate solution. This lab will take two sessions. If you print this lab, you may find the pdf version more appropriate. 2 A Sample Problem Suppose we want to know if there is a solution to: cos x = x if the solution is unique, and what that solution is. This is not an algebraic equation, and there is little hope of forming an explicit expression for the solution. Since we can t solve this equation exactly, it s worth knowing whether there is anything we can say about it. In fact, if there is a solution x 0 to the equation, we can probably find a computer representable number x which approximately satisfies the equation (has low residual error) and which is close to x 0 (has a low approximation error). The exercise below uses a plot to illustrate the existance of a solution. Note: Matlab has the capability of plotting some functions by name. We won t be using this capability in this course because it is too specialized. Instead, we will construct plots the way you probably learned when you first learned about plotting: by constructing a list of x-values (abscissæ) and corresponding y-values (ordinates) and then plotting the points with lines connecting them. Exercise 1: The following steps show how to use Matlab to plot the functions y = cos x and y = x together in the same graph from π to π. (a) Define a Matlab (vector) variable xplot to take on 100 evenly-spaced values between -pi and pi. You can use linspace to do this. (b) Define the (vector) variable y1plot by y1plot=cos(xplot). (c) Define the (vector) variable y2plot by y2plot=xplot. (d) Plot both y1plot and y2plot by plotting the first, plot(xplot,y1plot) 1

then hold on, plot the second, and hold off. You saw this done before in Lab 2. (e) The two lines intersect, clearly showing that a solution to the equation cos x = x exists. Include the Matlab commands you used in your summary file. Send me the plot in the form of a jpg file. To create such a file, use the command print -djpeg exer1.jpg or use the File menu, export and choose JPEG image. Exercise 2: (a) Write an m-file named cosmx.m ( COS Minus X ) that defines the function f(x) = cos x x Recall that a function m-file is one that starts with the word function. In this case it should start off as function y = cosmx ( x ) % y = cosmx(x) computes the difference y=cos(x)-x % your name and the date y =??? (This is very simple the object of this exercise is to get going.) Include a copy of this file with your summary. (b) Now use your cosmx function in the same plot sequence plot(??? hold on plot(??? hold off as above to plot cosmx and the horizontal axis on the interval π x π. Send me the plot file as exer2.jpg. Hint: The horizontal axis is the line y = 0. To plot this as a function, you need a vector x-values between π and π and a vector of y-values all of which are zero (use the Matlab function zeros). 3 The Bisection Idea The idea of the bisection method is very simple. We assume that we are given two values x = x a and x = x b (x a < x b ), and that the function f(x) is positive at one of these values (say at x a ) and negative at the other. (When this occurs, we say that [x a, x b ] is a change-of-sign interval for the function.) Assuming f is continuous, we know that there must be at least one root in the interval. Intuitively speaking, if you divide a change-of-sign interval in half, one or the other half must end up being a change-of-sign interval, and it is only half as big. Keep dividing in half until the change-of-sign interval is so small that any point in it is within the specified tolerance. More precisely, consider the point x c = (x a + x b )/2. If f(x c ) = 0 we are done. (This is pretty unlikely). Otherwise, depending on the sign of f(x c ), we know that the root lies in [x a, x c ] or [x c, x b ]. In any case, our change-of-sign interval is now half as large as before. Repeat this process with the new change of sign interval until the interval is sufficiently small and declare victory. We are guaranteed to converge. We can even compute the maximum number of steps this will take, because if the original change-of-sign interval has length l then after one bisection the current change-of-sign interval is of length l/2, etc. We know in advance how well we will approximate the root x 0. These are very powerful facts, which make bisection a robust algorithm that is, it is very hard to defeat it. 2

Exercise 3: (a) If we know the start points x a and x b and the interval size tolerance ɛ, we can predict beforehand the maximum number of steps the bisection code can take. What is the formula for this value? (b) Give an example of a continuous function that has a root in the interval [-1, 1], but for which bisection could not be used. 4 Programming Bisection Before we look at a sample bisection program, let s discuss some things. If you haven t done much programming before, this is a good time to try to understand the logic behind how we choose variables to set up, what names to give them, and how to control the logic. First of all, we should write the bisection algorithm as a Matlab function. There are two reasons for writing it as a function instead of a script m-file (without the function header) or by typing everything at the command line. The first reason is a practical one: we will be executing the algorithm several times, with differing end points, and functions allow us to use temporary variables without worrying about whether or not they have already been used before. The second reason is pedantic: you should learn how to do this now because it is an important tool in your toolbox. Atkinson describes the bisection algorithm (Section 2.1) in the following manner. (The variable names have changed slightly.) Algorithm Bisect(f,x a,x b,root,ɛ) 1. Define x c :=(x a + x b )/2. 2. If x b x c < ɛ, then accept root:=x c, and exit. 3. If sign(f(x b )) * sign(f(x c )) < 0, then x a :=x c ; otherwise, x b :=x c. 4. Return to step 1. The language of this description is based on a computer language called Algol but is intended to be a clear means of specifying algorithms in books and papers. One objective of this lab is to show how to rewrite it in the Matlab language. As a starting point, let s fix cosmx as the function and leave it out of the calling sequence. Let s also fix epsilon=1.0e-6. Note also that, in Matlab, returned values are separated from arguments, so the variable root, which I will call approxroot to make a point, is written on the left of an equal sign. The first few lines of the file bisect cosmx.m should be: function approxroot = bisect_cosmx( xa, xb) % approxroot = bisect_cosmx( xa, xb) uses bisection to find a % root of cosmx between xa and xb to tolerance 0.000001 % xa=left end point of interval % xb=right end point of interval % cosmx(xa) and cosmx(xb) should be of opposite signs % your name and the date Now think about the iteration. Atkinson s algorithm employs the return to Step 1 device to indicate uncounted repetition of Steps 1-4. Matlab provides a more elegant construct for uncounted repetition: the while loop. In Atkinson s Step 2, the algorithm is terminated. Matlab while loops usually terminate at their end, so we continue iterating for as long as x b x c is larger than epsilon, rather than stopping when x b x c is smaller. This reversal of the test is sometimes confusing, but it is how it is done. Here is a Matlab function that carries out the bisection algorithm for our cosmx function. The endpoints of the initial interval are variables that you specify when you invoke the function. The Matlab variables xa, xb and xc represent the mathematical quantities x a, x b and x c. 3

function approxroot = bisect_cosmx( xa, xb) % approxroot = bisect_cosmx( xa, xb) uses bisection to find a % root of cosmx between xa and xb to tolerance 0.000001 % xa=left end point of interval % xb=right end point of interval % cosmx(xa) and cosmx(xb) should be of opposite signs % your name and the date EPSILON = 1.0e-6; fa = cosmx(xa); fb = cosmx(xb); approxroot = (xb+xa)/2; itcount = 0; while ( abs ( xb - xa ) > EPSILON ) itcount = itcount + 1; xc = ( xa + xb ) / 2; approxroot = xc; fc = cosmx(xc); % The following statement prints the progress of the algorithm disp(strcat( xa=,num2str(xa),, fa=,num2str(fa),, xc=,num2str(xc),..., fc=,num2str(fc),, xb=,num2str(xb),, fb=,num2str(fb))) if ( fc == 0 ) break; elseif ( sign(fb) * sign(fc) <= 0 ) xa = xc; fa = fc; else xb = xc; fb = fc; end end disp(strcat( bisect_cosmx took itcount=,num2str(itcount), steps. )) Note: I have violated my own rule twice about having functions print things. The first disp statement merely prints progress of the function and can be discarded once you are confident it is correct. The print of the number of steps at the end is different, and really should be returned along with the approximate root. Matlab has the capability of returning more than one quantity, but we will not use that capability until later in this course. Exercise 4: Create the file bisect cosmx.m by typing it in yourself, by using copy-and-paste to copy it from this window or by downloading my version of bisect cosmx.m. (a) Why is the name EPSILON all capitalized? (b) What does the sign(x) function do? What if x is 0? (c) What does the expression sign(fb) * sign(fc) <= 0 say about fb and fc? 4

(d) What is the purpose of the statement approxroot = (xb+xa)/2; preceeding the while statement? (e) The disp command is used only to monitor progress. Note the use of... as the continuation character. (f) Try the command: x = bisect_cosmx ( 0, 3 ) Is the value x close to a root of the equation cos(x)=x? (g) Type the command help bisect cosmx at the Matlab command line. You should see your comments reproduced as a help message. This is one reason for putting comments at the top. (h) For what input numbers will this program produce an incorrect answer (i.e., return a value that is not close to a root)? Add a check before the loop so that the function will call the Matlab error function (use the help facility for details on using error) instead of returning an incorrect answer. (i) In your opinion, why did I use abs in the test inside the while statement? 5 Variable Function Names Now we have written an m-file called bisect cosmx.m that can find roots of the function cosmx on any interval, but we really want to use it on an arbitrary function. To do this, we could create an m-file for the other function, but we would have to call it cosmx.m, because that is what the bisect cosmx function expects. Suppose that we have three files, called f1.m through f3.m, each of which evaluates a function that we want to use bisection on. We could rename f1.m to cosmx.m, and change its name inside the file as well, and repeat this for the other files. But who wants to do that? It is more convenient to introduce a dummy variable for the function name, just as we used the dummy variables a and b for numerical values. In Matlab, function names are passed as quoted strings and their use is only a little more difficult than for numbers. Warning: If you forget the quotes around the function name, you will get strange errors. Note: Matlab also uses the @ character to denote function handles that are used in much the same way as quoted strings for function names. Release 14 and newer versions of Matlab also use the @ character to define anonymous functions, and it can be used instead of the feval function below. Since Release 13 is installed on the computers in GSCC, and since the syntax below remains valid in newer versions, we will continue using feval. Exercise 5: Copy the file bisect cosmx.m to a new file named bisect.m, or use the Save as menu choice to make a copy with the new name bisect.m. (a) In the declaration line for bisect, include a dummy function name. We might as well call it f, so the line becomes function approxroot = bisect ( f, xa, xb ) (b) Change the comments describing the purpose of the function, and explain the variable f. (c) Where we evaluated the function by writing f(x), we now have to use a Matlab helper function called feval (Function EVALuation). For instance, the line fa = cosmx(xa); must be rewritten as: fa = feval ( f, xa ); Make this change for fa, and a similar change for fb and fc. 5

(d) When we use bisect, we must include the function name, in quotes: x = bisect ( cosmx, 0, 3 ) Execute this command and make sure that you get the same result as before. (This is called regression testing. ) Warning: If you forget the quotes around cosmx and use the command x = bisect ( cosmx, 0, 3 ) % error! you will get the following mysterious error message: >> bisect(cosmx,0,3)??? Input argument x is undefined. Error in ==> cosmx.m On line 2 ==> y=cos(x)-x; Exercise 6: The following table contains formulas and search intervals for three functions. Write three simple function files, f1.m, f2.m, and f3.m, each one similar to cosmx.m but with formulas from the table. Then use bisect to find a root in the given interval, and fill in the table. I have formatted the table as ASCII text so that you can easily include it in your summary file. Name Formula Interval approxroot No. Steps f1 x^2-9 [0,5] f2 x^6-x-1 [1,2] f3 2*x-exp(-x) [-10,100] 6