function [root, ea,iter]=bisect(func,xl,xu,es)
% [root,fx,ea,iter]=bisect(func,xl,xu,es):
% uses bisection method to find the root of 'func'
% input:
% func = name of function
% xl, xu = lower and upper guesses
% es = desired relative error (default = 0.1%)
%
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin < 3
error('at least 3 input arguments required');
end
if nargin<4
es=0.1;
end
test_init = func(xl)*func(xu);
if test_init>0
error('no sign change');
end
iter = 0;
xr = xl;
while (1)
xrold = xr;
xr = (xl + xu)/2;
iter = iter + 1;
ea = abs((xr - xrold)/xr) * 100;
fprintf('iter = %d: xl = %.2f, xu = %.2f, ea %.2f \n', iter, xl, xu, ea );
test = func(xl)*func(xr);
if test < 0
xu = xr;
elseif test > 0
xl = xr;
else
ea = 0;
end
if ea <= es
break
end
end
root = xr;
'기계공학 > Numerical Method (수치해석)' 카테고리의 다른 글
[수치해석] Linear Interpolation Matlab Code (0) | 2021.04.17 |
---|---|
[수치해석] Newton-Raphson Method Matlab Code (0) | 2021.04.17 |
[수치해석] Fixed Point Iteration Matlab Code (0) | 2021.04.17 |