Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1.The following MATLAB code uses the false position method to solve for the root

ID: 3168175 • Letter: 1

Question

1.The following MATLAB code uses the false position method to solve for the roots of the input function. Please find mistakes in the code (underlined sentences). You can add notes to explain.

function [root,ea,iter]=falsepos(func,xl,xu,es,maxit,varargin)

% falsepos: root location zeroes

% [root,ea,iter]=falsepos(func,xl,xu,es,maxit,p1,p2,...):

% uses false position to find the root of func

% input:

% func = name of function

% xl, xu = lower and upper guesses

% es = desired relative error (default = 0.0001%)

% maxit = maximum allowable iterations (default = 50)

% p1,p2,... = additional parameters used by function

% output:

% root = real root

% ea = approximate relative error (%)

% iter = number of iterations

if nargin<3,error('at least 3 input arguments required'),end

test = func(xl,varargin{:})*func(xu,varargin{:});

if test<=0,error('no sign change'),end

if nargin<4|es<=0, es=0.0001;end

if nargin<5|maxit<=0, maxit=50;end

iter = 0; xr = xl;

while (1)

xrold = xr;

fl=func(xl,varargin{:});

fu=func(xu,varargin{:});

xr = xu + fu*(xl - xu)/(fl - fu);

iter = iter + 1;

if xr ~= 0,ea = abs((xr - xrold)/xr) * 100;end

test = fl*func(xr,varargin{:});

if test < 0

xu = xr;

elseif test > 0

xl = xr;

else

ea = 0;

end

if ea > es | iter < maxit,break,end

end

root = xr;

Page 1 of 3

2. Read and fill in the gaps in the corresponding MATLAB code.

function [root,ea,iter]=falsepos(func,xl,xu,es,maxit,varargin)

% falsepos: root location zeroes

% [root,ea,iter]=falsepos(func,xl,xu,es,maxit,p1,p2,...):

% uses false position to find the root of func

% input:

% func = name of function

% xl, xu = lower and upper guesses

% es = desired relative error (default = 0.0001%)

% maxit = maximum allowable iterations (default = 50)

% p1,p2,... = additional parameters used by function

% output:

% root = real root

% ea = approximate relative error (%)

% iter = number of iterations

if nargin<3,error('at least 3 input arguments required'),end

test = func(xl,varargin{:})*func(xu,varargin{:});

if test>0,error('no sign change'),end

if nargin<4|es<=0, es=0.0001;end

if nargin<5|maxit<=0, maxit=50;end

iter = 0; xr = xl;

while (1)

xrold = xr;

fl=func(xl,varargin{:});

fu=func(xu,varargin{:});

xr = ____________________________;

iter = iter + 1;

if xr ~= 0,ea = abs((xr - xrold)/xr) * 100;end

test = fl*func(xr,varargin{:});

if test < 0

xu = xr;

elseif test > 0

xl = xr;

else

ea = 0;

end

if ea ______ | iter _______,break,end

end

root = xr;

Explanation / Answer

A simmple code for false position method is given below

%%% Matlab code %%%%%

%%%Regula False Position method
disp('Regula False-position method ');
syms x %%% define variable

f= x^3-25 %%% function definition

tol=0.00001 %%% define accuracy of roots  

y(1)=0; %%% intial guess
y(2)=3; %%%% initial guess
for n=2:52 %%% max iteration to 50
f1=subs(f,y(n)); %%% calculating lower value of function
f2=subs(f,y(n-1)); %%% calculating higher value of function
y(n+1)=(y(n-1)*f1-y(n)*f2)/(f1-f2); %%% False position formula
if ( abs(y(n+1)-y(n)) < tol) %%% checking absolute error
break;
end
end

%%%% final roots is in y(end)