% hwk1_1e.m % Find all roots of f(x) = exp(x)-x-2 % using Bisection, Newton and Secant methods % function hwk1_1e tol=10^(-8); % Plot ezplot(@f,[-2,2]); grid return % Guess intervals for roots % [1.0,1.5], [-2.5,-1.5] % Call Bisection method x = []; x = [x, bisect(1.0,1.5,tol)]; x = [x, bisect(-2.5,-1.5,tol)]; xb=x;
% Call Newton method x = []; x = [x, newton(1.25,tol)]; x = [x, newton(-2,tol)]; xn=x;
%Call Secant method x = []; x = [x, secant(1.0,1.5,tol)]; x = [x, secant(-2.5,-1.5,tol)]; xs=x; disp('') disp('') disp('The roots by Bisection, Newton and Secant methods') [xb; xn; xs]'
%%%%%% Define functions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function y = f(x) y = exp(x)-x-2; function y = fp(x) y = exp(x)-1; %%%%%% bisect Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function c=bisect(a,b,tol) fprintf('------------------------------------ \n') fprintf(' n a b c f(c) \n') fprintf('------------------------------------ \n') n = 1; c = (a+b)/2; fprintf('%3.0f %12.8f %12.8f %12.8f %12.4e \n',n,a,b,c,f(c))
while( abs(f(c))> tol ) if f(b)*f(c)>=0, b=c; else, a=c; end n=n+1; c = (a+b)/2; end fprintf('%3.0f %12.8f %12.8f %12.8f %12.4e \n',n,a,b,c,f(c)) %%%%%%%%%% Newton method%%%%%%%%%%%%%% function x=newton(x,tol) fprintf('------------------------------------ \n') fprintf(' n x f(x) \n') fprintf('------------------------------------ \n') n = 1; x= x - f(x)/fp(x); fprintf('%3.0f %12.8f %12.4e \n',n,x,f(x)) while ( abs(f(x))>tol ) n=n+1; x= x - f(x)/fp(x); end fprintf('%3.0f %12.8f %12.4e \n',n,x,f(x)) %%%%%%%%% Secant method%%%%%%%%%%%%%%%% function x2=secant(x0,x1,tol) fprintf('------------------------------------- \n') fprintf(' n x f(x) \n') fprintf('------------------------------------- \n') n = 1; x2= x1 - f(x1)*(x1-x0)/(f(x1)-f(x0)); fprintf('%3.0f %12.8f %12.4e \n',n,x2,f(x2)) while ( abs(f(x2))>tol ) x0=x1; x1=x2; n=n+1; x2= x1 - f(x1)*(x1-x0)/(f(x1)-f(x0)); end fprintf('%3.0f %12.8f %12.4e \n',n,x2,f(x2))
½ÇÇà°á°ú ------------------------------------ n a b c f(c) ------------------------------------ 1 1.00000000 1.50000000 1.25000000 2.4034e-001 25 1.14619321 1.14619324 1.14619322 1.2681e-009 ------------------------------------ n a b c f(c) ------------------------------------ 1 -2.50000000 -1.50000000 -2.00000000 1.3534e-001 25 -1.84140569 -1.84140563 -1.84140566 -4.4001e-010 ------------------------------------ n x f(x) ------------------------------------ 1 1.15349002 1.5744e-002 3 1.14619322 2.3681e-009 ------------------------------------ n x f(x) ------------------------------------ 1 -1.84348236 1.7477e-003 3 -1.84140566 1.2879e-014 ------------------------------------- n x f(x) ------------------------------------- 1 1.11149143 -7.2604e-002 5 1.14619322 -3.8866e-010 ------------------------------------- n x f(x) ------------------------------------- 1 -1.82233341 -1.6018e-002 4 -1.84140566 -7.4809e-011 The roots by Bisection, Newton and Secant methods
ans =
1.14619322121143 1.14619322172397 1.14619322043949 -1.84140565991402 -1.84140566043698 -1.84140566034805
|
|
LAST UPDATE: 2013.04.15 - 15:39 |
|