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