Hello,

I am using the code below to find the zeros for a function f using Regula Falsi method. However, my code is not consistent. For example:

>> f=inline('4*x^3-13*x^2+x+17')

f =

Inline function:

f(x) = 4*x^3-13*x^2+x+17

>> [c,k]=Regula_Falsi(f,-1, 1, 1e-2)

c =

failure

k =

8

I know there should be a zero in the interval [-1,1] but my code is giving me back a "failure" result.

Please Help. Thank you!

------------------

CODE:

function [c,k] = Regula_Falsi(f, a, b, tol,Kmax)

%

% Regula_Falsi Uses the Regula Falsi iterative procedure to approximate a root of

% equation f(x) = 0 on the interval [a,b].

%

% [c,k] = Regula_Falsi(f, a, b, tol,Kmax) where

%

% f is an inline function representing f(x) in the equation f(x)=0.

% a and b are the limits of interval [a,b].

% tol is the scalar tolerance for convergence (default 1e-4).

% Kmax is the maximum number of iterations.

% c is the approximate root of f(x) = 0.

% k is the number of interation.

if nargin < 4, tol = 1e-4;end

Kmax =1+fix((log(b-a)-log(tol))/log(2));

if f(a)*f(b)>0

c = 'failure';

return

end

for k=1:Kmax

c=(b-((f(b)*(b-a))/(f(b)-f(a))));

if f(c)==0

return

end

if f(b)*f(c)>0

b=c;

else a=c;

end

if b-a<tol,return

end

end

c = 'failure';