Using C Programming Language gxn x fxnfxn The function x 2
Using C Programming Language
g(xn)= x- f(xn)/f\'(xn)
Solution
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 format compact c = f(a); d = f(b); if c*d > 0.0 error(’Function has same sign at both endpoints.’) end for i = 1:n x = (a + b)/2 y = f(x) if c*y < 0 % compare signs of f(a) and f(x) b=x; % move b else a=x; % move a end end e = (b-a)/2;
