function [ x, k, fx ] = bisect( a,b,tol) %UNTITLED2 Summary of this function goes here % Detailed explanation goes here % find a zero for the function f(x) on [a,b] % using bisection. tol is the tolerance, say, 1e-10 % % x is the answer if any, k is the number of iterations, and % fx = f(x) % f(x) is defined at the end if f(a)*f(b)>0, disp('f(a) and f(b) have the same sign. quit'); return; end if abs(f(a))tol % abs(f(c))>tol if f(a)*f(c)>0 a=c; else b=c; end c = (a+b)/2; k = k+1; end x = c; fx = f(x); % define the function f(x) function [y] = f(x) y = x + sin(x) - 2; --------------------------------------------------------------- function [ x, k, fx ] = newtonbacktrack( x0, tol, kmax ) %UNTITLED Summary of this function goes here % Detailed explanation goes here % Newton with backtracking to find a zero for f(x) % % x = x - f(x) / df(x) where df is f'(x) x = x0; k = 0; while abs(f(x)) > tol && k < kmax if abs(df(x))= abs(f(x)) xtrial = (x + xtrial) / 2; end x = xtrial, k = k + 1, end fx = f(x); % define f(x) function [y] = f(x) y = x^2 + 1; % define df(x) function [y] = df(x) y = 2*x; ---------------------------------------------------------------