Fondamenti di Informatica

Similar documents
Part I Matlab and Solving Equations

Lecture 7 Symbolic Computations

Fondamenti di Informatica

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

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

Computers in Engineering Root Finding Michael A. Hawker

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

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

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

From: Robert Sharpley Subject: Homeworks #6 Date: February 22, :09:53 AM EST Cc: Robert Sharpley

B. Examples Set up the integral(s) needed to find the area of the region bounded by

MATH2070: LAB 3: Roots of Equations

Exercises C-Programming

What Secret the Bisection Method Hides? by Namir Clement Shammas

Trigonometric Fourier series

Math 230 Final Exam December 22, 2015

Calculus I Review Handout 1.3 Introduction to Calculus - Limits. by Kevin M. Chevalier

MATH2070: LAB 3: Roots of Equations

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

Today s class. Roots of equation Finish up incremental search Open methods. Numerical Methods, Fall 2011 Lecture 5. Prof. Jinbo Bi CSE, UConn

COURSE: NUMERICAL ANALYSIS. LESSON: Methods for Solving Non-Linear Equations

4.2 and 4.6 filled in notes.notebook. December 08, Integration. Copyright Cengage Learning. All rights reserved.

4.0 Programming with MATLAB

Problem #3 Daily Lessons and Assessments for AP* Calculus AB, A Complete Course Page Mark Sparks 2012

4.7 Approximate Integration

Introduction to Programming for Engineers Spring Final Examination. May 10, Questions, 170 minutes

MATLAB Lecture 4. Programming in MATLAB

1.00 Lecture 19. Numerical Methods: Root Finding

MAT Business Calculus - Quick Notes

AP Calculus AB Unit 2 Assessment

and F is an antiderivative of f

AMS 27L LAB #2 Winter 2009

Handout 2 - Root Finding using MATLAB

And Now to Something Completely Different: Finding Roots of Real Valued Functions

AP Computer Science Unit 3. Programs

Remember Java Data Types

EE 216 Experiment 1. MATLAB Structure and Use

Section 6.1 Estimating With Finite Sums

More Ways to Solve & Graph Quadratics The Square Root Property If x 2 = a and a R, then x = ± a

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

APPM/MATH Problem Set 4 Solutions

Homework: Study 6.1 # 1, 5, 7, 13, 25, 19; 3, 17, 27, 53

Key Concepts: Economic Computation, Part II

Quadratic Functions CHAPTER. 1.1 Lots and Projectiles Introduction to Quadratic Functions p. 31

5.5 Newton s Approximation Method

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

Applications of Integration. Copyright Cengage Learning. All rights reserved.

UNIVERSITY OF CALIFORNIA COLLEGE OF ENGINEERING

QUADRATIC FUNCTIONS. PROTOTYPE: f(x) = ax 2 + bx + c. (1) The leading coefficient a 0 is called the shape parameter.

Math 226A Homework 4 Due Monday, December 11th

Homework #6 Brief Solutions 2012

Fathom Dynamic Data TM Version 2 Specifications

Supplemental 1.5. Objectives Interval Notation Increasing & Decreasing Functions Average Rate of Change Difference Quotient

Differentiation. The Derivative and the Tangent Line Problem 10/9/2014. Copyright Cengage Learning. All rights reserved.

Evaluating the polynomial at a point

Chapter 8: Applications of Definite Integrals

Math Analysis Chapter 1 Notes: Functions and Graphs

Part III Functions and Data

WebAssign Lesson 1-2a Area Between Curves (Homework)

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

The New Bisection Plus Algorithm by Namir Shammas

Chapter 5 Accumulating Change: Limits of Sums and the Definite Integral

Exam 1 Review. MATH Intuitive Calculus Fall Name:. Show your reasoning. Use standard notation correctly.

Introduction to numerical algorithms

Notice that the height of each rectangle is and the width of each rectangle is.

ESTIMATING AND CALCULATING AREAS OF REGIONS BETWEEN THE X-AXIS AND THE GRAPH OF A CONTINUOUS FUNCTION v.07

Math Analysis Chapter 1 Notes: Functions and Graphs

Rectangle Sums

Math Scientific Computing - Matlab Intro and Exercises: Spring 2003

Engineering Analysis ENG 3420 Fall Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00

Drawing fractals in a few lines of Matlab

5/27/12. Objectives 7.1. Area of a Region Between Two Curves. Find the area of a region between two curves using integration.

Euler s Method for Approximating Solution Curves

More About Factoring Trinomials

1.00 Lecture 25. Root Finding

Assignment #2: False Position Method

MATLAB Project: Getting Started with MATLAB

9.3 Hyperbolas and Rotation of Conics

1.00 Lecture 19. Packaging Functions in Objects

Announcements. Topics: To Do:

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

`Three sides of a 500 square foot rectangle are fenced. Express the fence s length f as a function of height x.

SOLVING SIMULTANEOUS EQUATIONS. EXAMPLE e4.1 Solve Corresponding augmented matrix 2x 4y 2 a 242 x 3y 3

Graphics calculator instructions

Fondamenti di Informatica

Unit 2 Day 9. FRED Functions

1 0 3 y 2. In SciNotes, enter the augmented matrix, then rref(a).

The New Trisection and Trisection Plus Root- Seeking Algorithms by Namir Shammas

MS213: Numerical Methods Computing Assignments with Matlab

mathcad_homework_in_matlab.m Dr. Dave S#

C3 Numerical methods

CSCI 135 Software Design and Analysis, C++ Homework 1 Solution

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

Math Homework 3

The Application of Numerical Approximation Methods upon Digital Images

Lesson 13: Exploring Factored Form

Announcements. Topics: To Do:

= f (a, b) + (hf x + kf y ) (a,b) +

9.1 Bracketing and Bisection

B.Stat / B.Math. Entrance Examination 2017

Transcription:

Fondamenti di Informatica Scripts and Functions: examples lesson 9 2012/04/16 Prof. Emiliano Casalicchio emiliano.casalicchio@uniroma2.it

Agenda Examples Bisection method Locating roots Secant methods 2

Bisection method Suppose that c = f (a) < 0 and d = f (b) > 0. If f is continuous, then obviously it must be zero at some x between a and b. The bisection method then consists of looking half way between a and b for the zero of f, i.e. let x = (a + b)/2 and evaluate y = f (x). Unless this is zero, then from the signs of c, d and y we can decide which new interval to subdivide. In particular, if c and y have the same sign, then [x, b] should be the new interval, but if c and y have different signs, then [a, x] should be the new interval. a 0 x 1 x 2 x 0 b 0 a 1 b 1 a 2 b 2 3

Bounding the error One good thing about the bisection method, that we don t have with Newton s method, is that we always know that the actual solution x is inside the current interval [a, b], since f (a) and f (b) have different signs. This allows us to be sure about what the maximum error can be. Precisely, the error is always less than half of the length of the current interval [a, b], i.e. {Absolute Error} = x x < (b a)/2, where x is the center point between the current a and b. 4

The algorithm a,b,n,f c=f(a); d=f(b) c*d>0.0 x=(b-a)/2; y=f(x) Y=0.0 YES YES NO YES f(x)>0 (or <0) for all x in [a,b] e=0; exit b=x YES c*y<0.0 NO NO a=x n>0.0 YES NO e=(b-a)/2; exit n=n-1 5

function [x e] = mybisect(f,a,b,n) % function [x e] = mybisect(f,a,b,n) % Does n iterations of the bisection method for a function f % Inputs: f -- an inline function % a,b -- left and right edges of the interval % n -- the number of bisections to do. % Outputs: x -- the estimated solution of f(x) = 0 % e -- an upper bound on the error format long c = f(a); d = f(b); if c*d > 0.0 error( Function has same sign at both endpoints. ) end disp( x y ) for i = 1:n x = (a + b)/2; y = f(x); disp([ x y]) if y == 0.0 % solved the equation exactly e = 0; break % jumps out of the for loop end if c*y < 0 b=x; else a=x; end end e = (b-a)/2; 6

Root finder (graphical method!!!) Use Let us define f(x) as a permanent function rather then inline function y=f(x) y= 3*x.^2+2*x.^3+12; >> plot(f(-100:1:100)); >> plot(-100:1:100,f(-100:1:100)); 0-100 7

let try >> mybisect(f,-100,100,40)??? Attempted to access f(-100); index must be a positive integer or logical. Error in ==> mybisect at 10 c = f(a); d = f(b); NON POSSO USARE UNA FUNZIONE PERMANENTE COME INPUT DI UNA FUNZIONE DEVO USARE UN OGGETTO INLINE 8

>> f=inline('3*x.^2+2*x.^3+12'); >> [x e]=mybisect(f,-20,20,10) x y 0 12-10 -1688-5 -163-2.500000000000000-0.500000000000000-1.250000000000000 12.781250000000000-1.875000000000000 9.363281250000000-2.187500000000000 5.420410156250000-2.343750000000000 2.730285644531250-2.421875000000000 1.185523986816406-2.460937500000000 0.360714912414551 x = -2.460937500000000 e = 0.019531250000000 9

Locating a Root The bisection method and Newton s method are both used to obtain closer and closer approximations of a solution, but both require starting places. The bisection method requires two points a and b that have a root between them, and Newton s method requires one point x0 which is reasonably close to a root. How do you come up with these starting points? It depends. If you are solving an equation once, then the best thing to do first is to just graph it. If you have to solve the same equation many times, but with different coefficients the first thing you want to take advantage of is the natural domain of the problem, i.e. on what interval is a solution physically reasonable. 10

The algorithm function [a,b] = myrootfind(f,a0,b0) % function [a,b] = myrootfind(f,a0,b0) % Looks for subintervals where the function changes sign % Inputs: f -- an inline function % a0 -- the left edge of the domain % b0 -- the right edge of the domain % Outputs: a -- an array, giving the left edges of subintervals % on which f changes sign % b -- an array, giving the right edges of the subintervals n = 1001; % number of test points to use a = []; % start empty array b = []; x = linspace(a0,b0,n); y = f(x); for i = 1:(n-1) end if a == [] end if y(i)*y(i+1) < 0 % The sign changed, record it a = [a x(i)]; b = [b x(i+1)]; end warning('no roots were found') >> f=inline('3*x.^2+2*x.^3+12'); >> [a,b]=myrootfind(f,-100,100) a = -2.599999999999994 b = -2.400000000000006 11

Homework Modify mybisect to solve until the error is bounded by a given tolerance. Use a while loop to do this. How should error be measured? Run your program on the function f (x) = 2x3 +3x 1 with starting interval [0, 1] and a tolerance of 10 8. How many steps does the program use to achieve this tolerance? (You can count the step by adding 1 to a counting variable i in the loop of the program.) Turn in your program and a brief summary of the results. 12

Secant method The secant method requires two initial points x0 and x1 which are both reasonably close to the solution x. Preferably the signs of y0 = f (x0 ) and y1 = f (x1 ) should be different. Once x0 and x1 are determined the method proceeds by the following formula: x i+1 = x i x i x i 1 y i y i 1 y i 13

The algorithm Homework: Write down the flow chart diagram 14

The matlab code function x = mysecant(f,x0,x1,n) format long % prints more digits format compact % makes the output more compact % Solves f(x) = 0 by doing n steps of the secant method starting with x0 and x1. % Inputs: f -- the function, input as an inline function % x0 -- starting guess, a number % x1 -- second starting geuss % n -- the number of steps to do % Output: x -- the approximate solution y0 = f(x0); y1 = f(x1); for i = 1:n % Do n times x = x1 - (x1-x0)*y1/(y1-y0) % secant formula. y=f(x) % y value at the new approximate solution. % Move numbers to get ready for the next step x0=x1; y0=y1; x1=x; y1=y; end 15

Homework modify the program mysecant to accept a tolerance using a while loop. 16

The Regula Falsi Method The Regula Falsi method is somewhat a combination of the secant method and bisection method. The idea is to use secant lines to approximate f (x), but choose how to update using the sign of f (x n ). Just as in the bisection method, we begin with a and b for which f (a) and f (b) have different signs. Then let: x = b b a f(b) f(a) f(b). Next check the sign of f (x). If it is the same as the sign of f (a) then x becomes the new a. Otherwise let b = x. 17

Homework Realize a program that implement the Regula Falsi Method. 18

Integration In this section, we wish to approximate a definite integral: b a f(x) dx where f(x) is a continuous function. In calculus we learned that integrals are (signed) areas and can be approximated by sums of smaller areas, such as the areas of rectangles. We begin by choosing points {x i } that subdivide [a, b]: a = x 0 <x 1 <... < x n 1 <x n = b. The subintervals [x i 1,x i ] determine the width x i of each of the approximating rectangles. For the height, we learned that we can choose any height of the function f(x i ) where x i [x i 1,x i ]. The resulting approximation is: b n f(x) dx f(x i ) x i. a To use this to approximate integrals with actual numbers, we need to have a specific x i in each interval. The two simplest (and worst) ways to choose x i are as the left-hand point or the right-hand point of each interval. This gives concrete approximations which we denote by L n and R n given by n L n = f(x i 1 ) x i and R n = i=1 i=1 n f(x i ) x i. i=1 19

The matlab code function L = myleftsum(x,y) % produces the left sum from data input. % Inputs: x -- vector of the x coordinates of the partition % y -- vector of the corresponding y coordinates % Output: returns the approximate integral n = max(size(x)); % safe for column or row vectors L = 0; for i = 1:n-1 end L = L + y(i)*(x(i+1) - x(i)); Given f(x) how can I obtain x and y 20

function y=f(x) y= 3*x.^2+2*x.^3+12; >> y=f(0:1:100) >> x=0:1:100 >> L = myleftsum(x,y) 21

Invocation of function in a function function z = integralapproximation(f,a,b,n,int_max) for i=1:n end x=a:floor(int_max/i):b; y=f(x); z(i) = myleftsum(x,y); integralapproximation(f,0,100,10,20) ans = Columns 1 through 5 32721200 41356200 38129472 46052450 47022000 Columns 6 through 10 46090836 48991400 48991400 48991400 48991400 22

Instead of two separated functions you can have two functions in the same.m file Useful for service functions 23

Homeworks evaluate the difference in using round and ceil instead of floor Plot the three z vectors 24