oneptrx.m
% Demonstration of One Point Iteration WITH RELAXATION
clear;
% define the function and its x = g(x) version
f = 'x^4 - 5*x^3 - 124*x^2 + 380*x + 1200';
% version 1 of g(x)
disp('Version 1 of g')
g = '-1200/(x^3 - 5*x^2 - 124*x + 380)';
% version 2 of g(x)
%disp('Version 2 of g')
%g = '(5*x^3 + 124*x^2 - 380*x - 1200)/x^3';
% version 3 of g(x)
%disp('Version 3 of g')
%g = '(-x^4 + 5*x^3 - 380*x - 1200)/(-124*x)';
x = input ('Enter initial guess of root : ');
W = input ('Enter relaxation factor W : ');
maxerror = 0.1; % maximum error
maxit = 30; % maximum number of iterations
count = 0; % iteration counter
error = 1; % starting error (100 percent)
fprintf('\n Iteration x g(x) error(%%) f(x)\n\n');
% this is where all the work gets done.....
while (error > maxerror) & (count < maxit)
count = count + 1;
temp = eval(g);
xnew = x + W*(temp - x);
error = abs((xnew - x)/xnew) * 100;
fprintf('%7g %10.4f %10.4f %10.4f %10.4f\n',count,x,xnew,error,eval(f));
x = xnew;
end
fprintf('\nRoot is %g Function at root is %g\n',x,eval(f))