Code covered by the BSD License  

Highlights from
Falseposition with Newton

Falseposition with Newton

by

 

17 May 2013 (Updated )

How to use 2 methods in one code

FalsePosition(f, a, b)
function [a, b] = FalsePosition(f, a, b)
fa = f(a);
fb = f(b);
c = (a*fb-b*fa)/(fb-fa);
fc = f(c);
if fa*fc > 0
a = c;
else
b = c;
end
Function for Newton(hybrid method with false position):
function a = Newton(f, fp, a, b, epsi, nmax)

Aa = a;
flag = 0;
for i =1: nmax
    if flag
flag = 0;
disp(' Using False Position ');
[Aa, b] = FalsePosition(f, Aa, b);
a = Aa; fa = f(a);
fprintf('new a = %f', a)
else
fa = f(a);
end
fprintf('Iteration %i a = %f, b = %f',i,a,b)
fpa = fp(a);
%Condition for Newton Bound
if fpa < 1e-5
disp(' Derivative Small');
flag = 1;
continue;
end
d=fa/fpa;
c = a-d;
fprintf(' c = %f', c);
if c < Aa || c > b

disp(' Newton not within Bounds');
flag = 1;
continue;

else
disp(' Successful Newton');
a = c;
if f(b)*f(c) < 0;
fprintf(' Lower bound(new) a = %f', a);
Aa = c;
end
end

if abs(d) < epsi
fprintf('CONVERGED ROOT = %f', a);
return;
end
end
disp('iterations Exceeded');
Function call :

a = 6.25; b = 7.83;
eps = 1e-6;
max = 35;
fox = @(x) tan(x)-x;
fop = @(x) sec(x)^2-1;
root = Newton(fox, fop, a, b, eps, max);

Contact us