I need a MATLAB code for the following to compare mine to, I can\'t get it to wo
ID: 3195010 • Letter: I
Question
I need a MATLAB code for the following to compare mine to, I can't get it to work properly. Solving the problem by hand wont help me, MATLAB only please.
You buy a $35,000 vehicle for nothing down at $8500 per year for 7 years. Use the bisect function from Fig. 5.7 to determine the interest rate that you are paying. Employ initial guesses for the interest rate of 0.01 and 0.3 and a stopping criterion of 0 00005. The formula relating present worth P, ant-al payments A number of years n, and interest rate / isExplanation / Answer
code for bisect {already given in book fig 5.7}
function [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,varargin)
% bisect: root location zeroes
% [root,fx,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
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by func
% output:
% root = real root
% fx = function value at 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(es), es=0.0001;end
if nargin<5|isempty(maxit), maxit=50;end
iter = 0; xr = xl; ea = 100;
while (1)
xrold = xr;
xr = (xl + xu)/2;
iter = iter + 1;
if xr ~= 0,ea = abs((xr - xrold)/xr) * 100;end
test = func(xl,varargin{:})*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; fx = func(xr, varargin{:});
Now run below code in command window
fi = @(i) 35000 * i*(1+i)^7/((1+i)^7-1)-8500;
>> [i fx ea iter]=bisect(fi,0.01,0.3)
i =
0.1535
hence i = 0.1535
Please rate
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.