📄 secant1.m
字号:
function [x,it,hist]=secant1(x0,x1,f,maxit,tol,opt)
%%
%% Solve f(x)==0 by secant method,
%% Usage [x,it,hist]=secant(x0,x1,f,maxit,tol,opt)
%% opt == 1: 1-point secant
%% 2: 2-point secant
%%
if nargin<6, opt = 2;
if nargin<5, tol = 1e-4;
if nargin<4, maxit = 100;
if nargin<3, error('too few input!!');
end; end; end; end;
xold = x0;
x = x1;
fold = feval(f,xold);
fx = feval(f,x);
f0 = fold;
hist = [ x0 x1; fold fx];
for it = 1:maxit,
if opt==1,
xp = x0;
fp = f0;
elseif opt==2,
xp = xold;
fp = fold;
end
xn = x - fx * (x-xp) / (fx-fp) ;
fn = feval(f,xn);
hist = [hist [xn;fn] ];
if abs(fn)<tol,
disp('Secant method successes!!');
return;
end
if opt==2,
xold = x;
fold = fx;
end
x = xn;
fx = fn;
end
disp('Secant method fails!!');
%% end of secant
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -