Welcome! Log In Create A New Profile

Advanced

Octave code for Muller's method

Posted by ra 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
ra
Octave code for Muller's method
February 12, 2010 11:59AM
% Muller's method
function rtn = mullers(fx,x2,x0,x1,n)

X =[];

for i = 1:n
  x = x2; f2 = eval(fx);
  x = x0; f0 = eval(fx);
  x = x1; f1 = eval(fx);

  h1 = x1 - x0;
  h2 = x0 - x2;
  gamma = h2 / h1;

  c = f0;
  a = (gamma*f1 - f0*(1+gamma) + f2) / (gamma * h1^2 * (1+gamma));
  b = (f1 - f0 - a*h1^2) / h1;

  if b>0
    root = x0 - 2*c / (b + sqrt(b^2 - 4*a*c));
  elseif
    root = x0 - 2*c / (b - sqrt(b^2 - 4*a*c));
  endif

  if root > x0
    x2 = x0;
    if root > x1
      x0 = x1; x1 = root;
    else
      x0 = root;
    endif
  elseif
    x1 = x0;
    if root < x2
      x0 = x2; x2 = root;
    else
      x0 = root;
    endif
  endif

  X = [X; i, root];
endfor

disp (X)

endfunction




Seems to work, using example given in textbook:

octave:1> fx = '3*x+sin(x)-exp(x)'
fx = 3*x+sin(x)-exp(x)
octave:2> mullers(fx,0.0,0.5,1.0,3)
warning: suggest parenthesis around assignment used as truth value
warning: suggest parenthesis around assignment used as truth value
   1.00000   0.35491
   2.00000   0.36046
   3.00000   0.36042
octave:3> 
Re: Octave code for Muller's method
February 24, 2010 01:53PM
If anyone is interested I wrote some python code that implements all of the root-finding methods from chapter 1. pm me if interested.
Sorry, only registered users may post in this forum.

Click here to login