Code covered by the BSD License  

Highlights from
False Position with Newton

False Position with Newton

by

 

2 methods in one code

FalsePosition(f, a, b)
(* ::Package:: *)

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(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(' 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
if fpa < 1e-5
disp(' Derivative not big enough');
flag = 1;
continue;
end
d=fa/fpa;
c = a-d;
fprintf(' c = % f', c);
if c < Aa || c > b

disp(' Newton outside Bounds');
flag = 1;
continue;

else
disp(' Successful');
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 = 2; b = 3;
eps = 1e-8;
max = 25;
fox = @(x) % define functions;
fop = @(x) % define functions;
root = Newton(fox, fop, a, b, eps, max);

Contact us