Advertisement
Guest User

Regula Falsi

a guest
Apr 21st, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 1.13 KB | None | 0 0
  1. function root = regulafalsi(f,a,b,tolerance)
  2. if (nargin < 3)
  3.     error('Wrong number of input arguments');
  4. elseif (nargin == 3 || nargin == 4)
  5.     i = 0;
  6.     tol = 1e-6;
  7.     g = 1;
  8.     disp('Iteration       a          b          x          f(a)          f(b)             f(c)');
  9.     disp('=========    =======    =======    =======    ==========    ==========       ========');
  10.     while(g > tol)
  11.         i = i + 1;
  12.         if (f(a) == f(b))
  13.             fprintf('Function has the same value on a and b on iteration %d\n', Iter);
  14.             disp('Denominator in Regula falsi is zero');
  15.             root = 'Regula falsi can''t compute the root';
  16.             beep
  17.             disp('Go for another iteration');
  18.             return;
  19.         end
  20.         c = a - ((f(a)*(b-a))/(f(b) - f(a)));
  21.         if(f(c)*f(a) > 0)
  22.             b = c;
  23.             g = f(b);
  24.             root = b;
  25.         else
  26.             a = c;
  27.             g = f(a);
  28.             root = a;
  29.         end
  30.         fprintf('%3d%17.4f%11.4f%11.4f%14.4f%14.4f%14.4f\n', i,a,b,c,f(a),f(b),f(c));
  31.         end
  32.     elseif (nargin > 4)
  33.     error('Too many input arguments');
  34. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement