> help bisect bisection method looks for a zero of function f on the interval [a,b]. It stops when the interval length < 2 tol. function root = bisect(a,b,tol) % first we find the roots of x^2-1. These are +/- 1. We use the % bisection method. It takes 17 steps to reach the tolerance when % starting with the interval [-1/2,3/2] > root = bisect(-1/2,3/2,10^(-5)); i = 17 > root root = 1.0000 > format long e, format compact % we see that the approximate root is 7.6 10^(-6) from the true root. > root root = 1.000007629394531e+000 % if we ask for a smaller tolerance then it takes 34 iterations: > root = bisect(-1/2,3/2,10^(-10)); i = 34 % we see that the approximate root is 5.8 10^(-11) from the true root. > root root = 1.000000000058208e+000 % change the interval > root = bisect(-1/2,3,10^(-10)); i = 35 % it takes longer, but the approximate root is still close 2.2 % 10^(-11). this isn't surprising since we know _exactly_ when the % bisection method will stop: (b-a)*(1/2)^n determines it all > root root = 9.999999999781721e-001 % go after the negative root: > root = bisect(-10,3,10^(-10)); i = 36 > root root = -1.000000000036380e+000 > help newton Newton's method looks for a zero of function f using an iterative method, with first guess x=a. It stops when |f(x_n)| < tol. it returns the root and the iterates (x). function [root,x] = newton(a,tol) % try newtons method: > [root,x] = newton(-2,10^(-14)); > format short e > root root = -1 % it meets the tolerance in 6 steps: > x x = -2.0000e+00 -1.2500e+00 -1.0250e+00 -1.0003e+00 -1.0000e+00 -1.0000e+00 % starting further away, it takes 7 steps: > [root,x] = newton(-3,10^(-14)); > root root = -1 % the iterates are converging to the root x=-1 > x Columns 1 through 6 -3.0000e+00 -1.6667e+00 -1.1333e+00 -1.0078e+00 -1.0000e+00 -1.0000e+00 Column 7 -1.0000e+00 % the error is going to zero quadratically once we get close enough % (the third iterate) > x + 1 Columns 1 through 6 -2.0000e+00 -6.6667e-01 -1.3333e-01 -7.8431e-03 -3.0518e-05 -4.6566e-10 Column 7 0 % let's look at the significant digits: > format long e % we see that we get 1, then 3, then 5, then 10 digits of accuracy. % Then we run into round-off error. > x Columns 1 through 3 -3.000000000000000e+00 -1.666666666666667e+00 -1.133333333333333e+00 Columns 4 through 6 -1.007843137254902e+00 -1.000030518043793e+00 -1.000000000465661e+00 Column 7 -1.000000000000000e+00 % now we change the function. Now we'll look for the zeros of f(x) = % x^2. > [root,x] = newton(1,10^(-14)); % it takes 25 iterates to meet the tolerance that we met in 7 steps % when zeroing x^2-1. Plus, we know the root is x=0. The final % iterate is 6.0 10^(-8) which isn't that close. We do check that % f is small then --- (6.0 10^(-8))^2 = 3.6 10^(-16) which is smaller % than the tolerance. > x x = Columns 1 through 6 1.0000e+00 5.0000e-01 2.5000e-01 1.2500e-01 6.2500e-02 3.1250e-02 Columns 7 through 12 1.5625e-02 7.8125e-03 3.9062e-03 1.9531e-03 9.7656e-04 4.8828e-04 Columns 13 through 18 2.4414e-04 1.2207e-04 6.1035e-05 3.0518e-05 1.5259e-05 7.6294e-06 Columns 19 through 24 3.8147e-06 1.9073e-06 9.5367e-07 4.7684e-07 2.3842e-07 1.1921e-07 Column 25 5.9605e-08 > root^2 ans = 3.5527e-15 > diary off