Create a Matlab Function file and a script file that runs the function. When run
ID: 2267421 • Letter: C
Question
Create a Matlab Function file and a script file that runs the function. When running the script, the answer's should display on the screen.
Post a clear picture of the hand written work if the problem asks for hand written work.
Comment the MatLab code so I can understand it. I should be able to just copy and paste the code and have it run.
The work and answer should match the answer posted.
Post the code and written work.
Create the " falsePsotion M-file" using the bisect M-File as an example
Bisect M-File...
function [root,ea,iter]=bisect(func,xl,xu,print,es,maxit,varargin)
% bisect: root location zeroes
% [root,ea,iter]=bisect(func,xl,xu,es,maxit,p1,p2,...):
% uses bisection method to find the root of func
%
% input:
% func = name of function
% xl, xu = lower and upper guesses
% print = a Boolean variable which causes printed output if true
% (default print=false)
% es = desired relative error (default = 1e-6)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by func
% 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||isempty(print), print=false;end
if nargin<5||isempty(es), es=1e-6;end
if nargin<6||isempty(maxit), maxit=50;end
iter = 0;
fl=func(xl,varargin{:});
fu=func(xu,varargin{:});
while (1)
xr = (xl + xu)/2;
iter = iter + 1;
fr=func(xr,varargin{:});
ea = abs(fr);
test = fl*fr;
if test < 0
xu = xr;
fu=fr;
elseif test > 0
xl = xr;
fl=fr;
else
ea = 0;
end
if print
fprintf('%d %9.6f %9.6f %9.6f %9.6f %9.6f %9.6f ',iter,xl,xu,fl,fu,xr,fr)
end
if ea <= es || iter >= maxit,break,end
end
root = xr;
Explanation / Answer
% Bisection Method
False Position Method