% hwk1_1e.m % Find all roots of f(x) = sin(x)-exp(-x) % using Bisection, Newton and Secant methods % function hwk1_1e tol=10^(-7); % Plot ezplot(@f,[-1.0,10]); grid
% Guess intervals for roots % [0.5,0.6], [3.0,3.1], [6.2,6.3], [9.4,9.5]
disp(' ') disp('Bisect method') % Call Bisection method x = []; x = [x, bisect(0.5,0.6,tol)]; x = [x, bisect(3.0,3.1,tol)]; x = [x, bisect(6.2,6.3,tol)]; x = [x, bisect(9.4,9.5,tol)]; xb=x;
disp(' ') disp('Newton method') % Call Newton method x = []; x = [x, newton(0.55,tol)]; x = [x, newton(3.05,tol)]; x = [x, newton(6.25,tol)]; x = [x, newton(9.45,tol)]; xn=x;
disp(' ') disp('Secant method') % Call Secant method x = []; x = [x, secant(0.5,0.6,tol)]; x = [x, secant(3.0,3.1,tol)]; x = [x, secant(6.2,6.3,tol)]; x = [x, secant(9.4,9.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 = sin(x)-exp(-x); function y = fp(x) y = cos(x)+exp(-x); %%%%%% 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 x2 f(x2) \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))
<< °á °ú ¹° >>
Bisect method ------------------------------------------------------ n a b c f(c) ------------------------------------------------------ 1 0.50000000 0.60000000 0.55000000 -5.4263e-002 20 0.58853264 0.58853283 0.58853273 -1.3959e-008 ------------------------------------------------------ n a b c f(c) ------------------------------------------------------ 1 3.00000000 3.10000000 3.05000000 4.4106e-002 16 3.09636230 3.09636536 3.09636383 9.7135e-008 ------------------------------------------------------ n a b c f(c) ------------------------------------------------------ 1 6.20000000 6.30000000 6.25000000 -3.5110e-002 19 6.28504906 6.28504944 6.28504925 -2.5689e-008 ------------------------------------------------------ n a b c f(c) ------------------------------------------------------ 1 9.40000000 9.50000000 9.45000000 -2.5298e-002 19 9.42469711 9.42469749 9.42469730 -4.9029e-008 Newton method ------------------------------------ n x f(x) ------------------------------------ 1 0.58795982 -7.9478e-004 3 0.58853274 -9.5479e-015 ------------------------------------ n x f(x) ------------------------------------ 1 3.09650297 -1.3261e-004 2 3.09636393 -8.7314e-010 ------------------------------------ n x f(x) ------------------------------------ 1 6.28506129 1.2040e-005 2 6.28504927 -2.6948e-013 ------------------------------------ n x f(x) ------------------------------------ 1 9.42469190 5.3500e-006 2 9.42469725 -2.4913e-015 Secant method ------------------------------------ n x2 f(x2) ------------------------------------ 1 0.58892452 5.4327e-004 3 0.58853274 3.9345e-010 ------------------------------------ n x2 f(x2) ------------------------------------ 1 3.09634126 2.1624e-005 2 3.09636393 3.6749e-009 ------------------------------------ n x2 f(x2) ------------------------------------ 1 6.28503683 -1.2463e-005 2 6.28504927 8.0893e-010 ------------------------------------ n x2 f(x2) ------------------------------------ 1 9.42471280 -1.5543e-005 2 9.42469724 1.4610e-008 The roots by Bisection, Newton and Secant methods
ans =
0.5885 0.5885 0.5885 3.0964 3.0964 3.0964 6.2850 6.2850 6.2850 9.4247 9.4247 9.4247
|
|
LAST UPDATE: 2013.04.15 - 14:29 |
|