Professor Diomar Cesar Lobao

Universidade Federal Fluminense-Volta Redonda, RJ, Brasil

Diomar Cesar


Dept. Ciências Exatas - Exact Science Dept.

Search

oneptrlx.m

% Demonstration of Gauss-Seidel (One Point Iteration) Root Finding
%
% The function is defined in the file 'fcn.m'. The contents of this file are:
%
%      function y = fcn(x)
%      y = -1200/(x^3 - 5*x^2 - 124*x + 380);

fprintf('\nDemonstration of One Point Iteration with relaxation\n');

       x = input ('Enter initial guess of root:        ');
       w = input ('Enter relaxation factor:            ');
maxerror = input ('Enter maximum error (percent):      ');
   maxit = input ('Enter maximum number of iterations: ');

xinit = x;
for wloop = 1:14    % test out 0.1 <= w <= 1.4
  x = xinit;
  w = wloop * 0.1;
  count = 0;   % iteration counter
  error = 1;   % starting error (100 percent)

%  fprintf('\n Iteration      x    g(x)=xnew  error(percent)\n\n');

  while (error > maxerror) & (count < maxit)   % '&' is logical AND
    count = count + 1;
    temp = fcn(x);
    xnew = x + w * (temp - x);
    error = abs((xnew - x)/xnew) * 100;
%   fprintf('%7g %10.4f %10.4f %10.4f\n',count,x,xnew,error);
    x = xnew;
  end
  wvect(wloop) = w;
  countvect(wloop) = count;
  fprintf('w = %g   # iter = %g\n',w,count)
end

quickplt(wvect,countvect,'relaxation factor (w)','# of iterations',...
         'optimal relaxation for one-point iteration')
Skip to content