📄 falsepos.m
字号:
function [x,k] = falsepos (x0,x1,tol,m,f)
%----------------------------------------------------------------
% Usage: [x,k] = falsepos (x0,x1,tol,m,f)
%
% Description: Apply the false position method to find a root of:
%
% f(x) = 0
%
% Inputs: x0 = lower limit of search
% x1 = upper limit of search (x1 > x0)
% tol = error tolerance used to terminate search
% (tol >= 0)
% m = maximum number of iterations (m >= 1)
% f = string containing name of user-supplied
% function. The function f is of the form:
%
% function y = f(x)
%
% Outputs: x = Estimated root of f(x) = 0
% k = number of iterations performed. If k < m,
% then the following convergence criterion
% was satisfied:
%
% |f(x)| < tol
%
% Notes: 1. x0 and x1 must be selected such that
% f(x0)*f(x1) < 0 to ensure that there is a
% root in [x0,x1]
%----------------------------------------------------------------
% Initialize
k = 0;
x1 = args (x1,x0,x1,2,'falsepos');
tol = args (tol,0,tol,3,'falsepos');
m = args (m,1,m,4,'falsepos');
f0 = feval(f,x0);
f1 = feval(f,x1);
if f0*f1 >= 0
fprintf ('\nIn falsepos, x0 and x1 must bracket a root.\n');
return
end
% Find root
y = tol + 1;
while (y > tol) & (k <= m)
x2 = x1 - (x1-x0)*f1/(f1-f0);
f2 = feval(f,x2);
if f0*f2 < 0
x1 = x2;
f1 = f2;
else
x0 = x2;
f0 = f2;
end
y = abs(f2);
k = k + 1;
end
x = x2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -