0
私はこの関数をしばらく使用していませんでした。私が持っている問題を回避する方法を見つけることができません。MATLABのセカント根発見関数の問題
私のコードは次のとおりです。私は、コードを実行すると、私は手で行うよう
function [x,i] = secant(f, x0, x1, tol, maxiters)
%SECANT Secant method
% [X,I] = SECANT(F, X0, X1, TOL, MAXITERS) performs the secant
% method with F(x), starting at x_0 = X0 and x_1 = X1, and continuing
% until either |X_i+1 - X_i| <= TOL, or MAXITERS iterations have
% been taken. The number of iterations, I, is also returned.
% An error is raised if the first input is not a function handle.
% A warning is raised if the maximum number of iterations is reached
% without achieving the tolerance.
if ~isa(f, 'function_handle')
error('Your first input was not a function handle')
end
i = 0; % initialise iterate counter
x = x1;
x_old = x0;
while abs(x - x_old) > tol && i < maxiters
x_old = x0;
x = x - f(x)*(x - x_old)/(f(x) - f(x_old)); % compute the new x
i = i + 1; % increase our counter by one
end
if abs(x - x_old) > tol
warning('Maximum number of iterations reached without achieving tolerance.')
end
が、私は同じ答えを得ることはありません。現在、私は、私は私の問題は下while abs(x - x_old) > tol && i < maxiters
.Iあるラインx_old = x0;
から来ていると信じて、
f = @(x) 0.9cos(x) - sqrt(x)
x0 = 0
x1 = 1
tol = 1e-08
maxiters = 4
を使用しています、私は関数を呼び出すたびに、それが戻っx0
からx
を設定だと思います。
どうすればこの問題を回避できますか?