📄 bisect.m
字号:
function [a, b, x, ithist, iflag] = bisect( f, a, b, tolx, maxit )%% function [a, b, x, ithist, iflag] = bisect( f, a, b, tolx, maxit )%% use the bisection method to compute an approximate root of f.%% Input parameters:% f name of a matlab function that evaluates % a, b real numbers satisfying f(a)*f(b) < 0% tolx stopping tolerance (optional. Default tolx = 1.e-7)% the bisection method stops if b-a < tolx% maxit maximum number of iterations (optional. Default maxit = 100)%%% Output parameters:% a, b real numbers satisfying f(a)*f(b) < 0; a root of f is% located between a and b% x approximation of the solution. x = 0.5*(a+b).% ithist array with the iteration history% The i-th row of ithist contains [it, a, b, c, fc]% ifag return flag% iflag = -1 error in input data, f(a)*f(b) > 0% iflag = 0 |b-a| < tolx and |x-x*| < 0.5*tolx, where x*% is a root of f% iflag = 1 iteration terminated because maximum number of % iterations was reached. |b-a| >= tolx%%% Matthias Heinkenschloss% Department of Computational and Applied Mathematics% Rice University% Jan 17, 2002%%fa = feval(f, a);fb = feval(f, b);if( fa*fb > 0 ); iflag = -1; returnend% set tolerances if necessaryif( nargin <= 3 ) tolx = 1.e-7; maxit = 100; endif( nargin <= 4 ) maxit = 100; end it = 0;iflag = 0;while( it < maxit & abs(b-a) >= tolx ) c = 0.5*(a+b); fc = feval(f, c); ithist(it+1,:) = [it, a, b, c, fc]; if( fc == 0 ) x=c; return; end if( fa*fc < 0 ); b = c; fb = fc; else a = c; fa = fc; end it = it+1;endx = 0.5*(a+b); % check why the bisection method truncated and set iflagif( abs(b-a) >= tolx ) % the bisection method truncated because the maximum number of iterations % was reached iflag = 1; returnend
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -