Laboratory 2 False-Position
Laboratory 2 False-Position
BSECE 5
False-Position Method
1 SCINOTE CODE
//Gerfel Philip C. Gonzales
//BSECE 5
//Laboratory 2
//False Position Method
function false_position()
f = input("Input the function to be evaluated:","string");
a = input("Input initial lower guess a:");
b = input ("Input initial upper guess b:");
ea = input("Input your desired approximation error:");
maxiter = input("Maximum no. of iterations:");
disp(['Lower
Root
Upper
iter = 0;
while 1==1;
iter = iter + 1;
x = a;
fa = evstr(f);
x = b;
fb = evstr(f);
m = (b*fa - a*fb)/(fa-fb);
x = m;
fm = evstr(f);
er = abs(fm);
printf('%8.4f %8.4f
%8.4f
%8.4f
f(a)
%8.4f
f(m)
%8.4f
f(b)'])
//evaluation
if ((fa<0 & fb<0)|(fa>0 &fb >0)) then break end;
if fa*fm == 0 then break end;
if (fa*fm > 0) a = m; end
if (fa*fm < 0) b = m; end
if (er <= ea) then break end;
if iter == maxiter then break end;
end
if ((fa<0 & fb<0)|(fa>0 &fb >0)) then printf('\n\n No Root!')
else printf('\n\nRoot is %f
with %f error
at %dth iteration',m,er,iter);
end
endfunction
2 SIMULATION
Find the root of + = with a approximation error of . and maximum number of
iteration of 100.