Regula Falsi Method

  • Follow


Hello Guys,

I've been trying to generate code for the Regula Falsi Method to find the roots of a function. Below is the code but i keep the following error message: 

"""   ??? Undefined function or method 'Regula_Falsi' for input arguments of type 'inline'.   """""

function [c,k] = Regula_Falsi(f, a, b, tol,Kmax)
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';



Please Help.

Thank you!
0
Reply Green 2/24/2011 5:51:04 AM

You haven't shown your calling syntax, which is what the error is related to.
I assume that it is because you are not correctly declaring the function handle f.
Try the following

f = @(x) x.^2-1;  % roots at +/- 1
a = 0;
b = 2;
[c,k] = Regula_Falsi(f,a,b)

Now this should stop your error, but you have another problem.
This test on the value at the root

    if f(c)==0
        return
    end

will never succeed due to floating-point computation. You should replace it with something like

if abs(f(c)) < tol
    return
end

Hth
Darren
0
Reply Darren 2/24/2011 8:26:20 AM


1 Replies
615 Views

(page loaded in 1.795 seconds)

Similiar Articles:













7/24/2012 9:58:37 PM


Reply: