MATLAB >> Regula Falsi Method.

by Green Sal » Fri, 25 Feb 2011 02:15:19 GMT

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';

Similar Threads

1. Newton-Raphson method or regula falsi method - MATLAB

2. Regula Falsi Method.

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';

3. Regula Falsi Method - MATLAB

4. "methods" doesn't work for Class Methods

I've got a class with many public methods defined in a classdef file,
but the "methods" call only shows the constructor and the externally-
defined methods that  I have.

Any ideas on what's going on?

5. callbacks, timers and OOP methods: help with using a method as timer callback - MATLAB

6. Methods block: constructor doesn't find other method

Hello There,

I've written a simple class in a classdefinition file. There are only two methods by now. The constructor and a function that should test if a condition is fullfilled for an instance of this class. The result is also saved in a property of that class. I want to call the helper method from the constructor but I get and error, that the method can't be found.

My Code looks like this - well actually it is not my code but it reproduces the error message:

classdef Untitled

  
  properties (SetAccess = private)
    Matrix;
    Condition;
  end
  
  methods
    function obj = Untitled(Matrix)
      obj.Matrix = [];
      obj.Condition = false;
      if nargin > 0
        obj.Matrix=Matrix;
        obj.Condition = test(obj.Matrix);
      end
    end

    function passed = test(Matrix)
       if any(size(Matrix)<2)
          passed = false;
       else
          passed = true;
       end
    end
  end
end

7. Euler method compared to RK4 method - MATLAB

8. Call private subclass method from a superclass method

I cannot find that the documentation forbid calls of private methods from methods that are inherited.  The example below demonstrates what I try to say. 

If the method, SubClassMethod, is public it works. 

Of course the method, SimpleTest, in the superclass cannot work in an instance of superclass. However, private or public does not change that. 

Am I mistaken? Is there a reason why it works this way?

/ per

Example in R2008a:

>> clear all, clear classes
>> sub = subclass;
>> sub.SimpleTest
??? Error using ==> SubClassMethod
Cannot access method 'SubClassMethod' in class 'subclass'.

Error in ==> superclass>superclass.SimpleTest at 4
            out = this.SubClassMethod;

classdef    superclass < handle    
    methods ( Access = public )
        function    out = SimpleTest( this )
            out = this.SubClassMethod;
        end
    end
end

classdef    subclass <  superclass  
    methods ( Access = private )
        function    out = SubClassMethod( this )
            out = 17;
        end
    end
end