Professional Documents
Culture Documents
k = 1:n;
out = [k’ x’ y’ error’ err’];
disp(’ step x y x_n - x_n-1
disp(out)
out2 = [k’ x’ y’ lamb’ lambda’];
disp(’ step x y ratio(3.23)
disp(out2)
yprime= feval(’fun_pr’, X);
ydoubleprime=feval(’fdprime’, X);
M = abs((-ydoubleprime)/(2*yprime))
one_over_M = abs(1/M)
aplpha_minus_x_0 =abs(X-x(1))
format
%-----------------------------------------------------------------------------------
function Secantclass(fun, a, b, tol, max)
% Find a zero using secant method.
% Input and putput variables
% fun string containing name of function
% [a, b] interval containing zero
% tol allowable change in successive iterates
% max maximum number of iterations
% x vector of approximation to zero
% y vector of function values, fun(x)
x(1) = a; x(2) = b;
y(1) = feval(fun, x(1)); y(2) = feval(fun, x(2));
for i = 2 : max
x(i+1) = x(i) - y(i) * (x(i) - x(i-1))/(y(i) - y(i-1));
y(i+1) = feval(fun, x(i+1));
if (abs(x(i+1)-x(i)) < tol)
disp(’method has converged’); break;
end
if y(i) == 0.0
disp(’Exact zero found’);break;
end
iter = i;
end
if (iter >= max)
disp(’Zero not found to desired tolerance’);
end
format long
n = length(x);
X = fzero(’fun’,x(1))
for i = 1 : n-1
err(i+1) = abs(X - x(i));
end
3
for i = 2:n
lambda(i) = error(i)/error(i-1);
EST(i) = (lambda(i)/(1-lambda(i)))*(x(i)-x(i-1));
end
if (iter >= max)
disp(’Zero not found to desired tolerance’);
end
format long
k = 1:n; out = [k’ x’ err’ lamb’];
disp(’ step x x* - x_n ratio’)
disp(out)
outaitken = [k’ x’ error’ lambda’ EST’];
disp(’ step x x_n - x_n-1 lambda_n
disp(outaitken)
yprime_at_the_root = feval(’fun_pr’, X)
format