You are on page 1of 4

1

Introduction to Numerical Analysis (MTH 351)


Matlab codes for Newton, Secant, and Fixed-poit methods

function Newton(fun, fun_pr, x1, tol, max)


% Find zero near x1 using Newton’s method.
% Input:
% fun string containing name of function
% fun_pr name of derivative of function
% x1 starting estimate
% tol allowable tolerance in computed zero
% max maximum number of iterations
% Output
% x (row) vector of approximations to zero
% y (row) vector fun(x)
x(1) = x1;
y(1) = feval(fun, x(1));
y_pr(1) = feval(fun_pr, x(1));
if y_pr(1) == 0.0
disp(’Error: Derivative is zero at the initial point’);return;
end
for i = 2 : max
x(i) = x(i-1) - y(i-1)/y_pr(i-1);
y(i) = feval(fun, x(i));
y_pr(i) = feval(fun_pr, x(i));
if abs(x(i)-x(i-1)) < tol
disp(’Newton method has converged’); break;
end
iter = i;
end
n = length(x);
format long
X = fzero(’fun’,x(1))
for i = 1 : n-1
err(i+1) = abs(X - x(i));
end
for i = 2:n
error(i) = abs(x(i) - x(i-1));
end
for i = 3 : n
lambda(i) = error(i)/(error(i-1))^2;
lamb(i) = err(i)/(err(i-1))^2;
end
if (iter >= max)
disp(’Zero not found to desired tolerance’);
end
2

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

error(i) = abs(x(i) - x(i-1));


end
for i = 3 : n
lambda(i) = error(i)/(error(i-1))^((sqrt(5)+1)/2);
lamb(i) = err(i)/(err(i-1))^((sqrt(5)+1)/2);
end
if (iter >= max)
disp(’Zero not found to desired tolerance’);
end
k = 1:n; out = [k’ x’ y’ error’ err’ lamb’];
disp(’ step x y x_n - x_n-1
disp(out)
yprime= feval(’fun_pr’, X);
ydoubleprime=feval(’fdprime’,X);
c = abs((ydoubleprime)/(2*yprime))^(((sqrt(5)+1)/2)-1)
format
%-----------------------------------------------------------------------------------
function Fixed(fun, a, tol, max)
% Find a fixed point using fixed point iteration.
% Input and putput variables
% fun string containing name of function
% a initial point
% tol allowable change in successive iterates
% max maximum number of iterations
x(1) = a;
for i = 1 : max - 1
x(i+1) = feval(fun, x(i));
if (abs(x(i+1)-x(i)) < tol)
disp(’method has converged’); break;
end
iter = i;
end
n = length(x);
X = fzero(’x-(1+x-(1/5)*x^2)’,x(1));
%X = sqrt(5);
for i = 1 : n
err(i) = abs(X - x(i));
end
for i = 2:n
lamb(i) = err(i)/err(i-1);
error(i) = abs(x(i) - x(i-1));
end
for i = 3:n
4

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

You might also like