% % nlineq_driver.m % % use bisect.m and newt1d.m to compute an approximation % of the root of f(x) = cos(x)*cosh(x)+1 % % Matthias Heinkenschloss % Department of Computational and Applied Mathematics % Rice University % disp(' ') disp(' Find the root of f(x) = cox(x)*cosh(x)+1 ') disp(' ') disp(' Hit return to continue with the Bisection Method') disp(' ') pause f = 'coscosh'; a = 1.6; b = 2; tolx = 1.e-7; [a, b, x, ithist, iflag] = bisect( f, a, b, tolx ); fprintf('\n The Bisection Method returned with iflag = %d \n ', iflag) fprintf('\n Bisection Method \n ') fprintf(' iter a b c f(c) \n ') for i = 1:size(ithist,1) fprintf('%4d &$ %13.8e $&$ %13.8e $&$ %13.8e $&$ %13.8e $\\\\ \n ', ithist(i,:)) end disp(' ') disp(' Hit return to continue with Regula Falsi') disp(' ') pause a = 1.6; b = 2; tolf = 1.e-14; [a, b, x, ithist, iflag] = regulafalsi( f, a, b, tolf ); fprintf('\n Regula falsi returned with iflag = %d \n ', iflag) fprintf('\n Regula Falsi \n ') fprintf(' iter a b c f(c) \n ') for i = 1:size(ithist,1) fprintf('%4d &$ %13.8e $&$ %13.8e $&$ %13.8e $&$ %13.8e $\\\\ \n ', ithist(i,:)) end disp(' ') disp(' Hit return to continue with Newton''s Method') disp(' ') pause x = 2; [x, ithist, iflag] = newt1d( f, x, 1.e-15, 1.e-15, 20 ); fprintf('\n Newton''s Method returned with iflag = %d \n ', iflag) fprintf('\n Newton''s Method \n ') fprintf(' iter x f(x) -f(x)/f''(x) \n ') for i = 1:size(ithist,1) fprintf('%4d &$ %13.8e $&$ %13.8e $&$ %13.8e $\\\\ \n ', ithist(i,:)) end disp(' ') disp(' Hit return to continue with Secant Method') disp(' ') pause x = 2; x1 = 2.01 [x, ithist, iflag] = secant( f, x, x1, 1.e-15, 1.e-15, 20 ) fprintf('\n Secant Method returned with iflag = %d \n ', iflag) fprintf('\n Secant Method \n ') fprintf(' iter x f(x) -f(x)/f''(x) \n ') for i = 1:size(ithist,1) fprintf('%4d &$ %13.8e $&$ %13.8e $&$ %13.8e $\\\\ \n ', ithist(i,:)) end disp(' ') disp(' Hit return to continue with Newton''s Method') disp(' ') pause f = 'xsinx2'; x = 0.5; [x, ithist, iflag] = newt1d( f, x, 1.e-12, 1.e-15, 20 ); fprintf('\n Newton''s Method returned with iflag = %d \n ', iflag) fprintf('\n Newton''s Method \n ') fprintf(' iter x f(x) -f(x)/f''(x) \n ') for i = 1:size(ithist,1) fprintf('%4d &$ %13.8e $&$ %13.8e $&$ %13.8e $\\\\ \n ', ithist(i,:)) end