Coding must be done in MATLab 1) (7 pts) Bracketing Method (Bisection) I\'ve pla
ID: 3870605 • Letter: C
Question
Coding must be done in MATLab
1) (7 pts) Bracketing Method (Bisection)
I've placed Bisection.m, a function that attempts to iteratively apply the bisection method according to certain criteria (read the file itself for more information).
a) (2) At present, the function does not check to see if there is actually a root in the bracket before running. Modify the function such that it first checks for a sign difference, and if none is found it returns values that can easily be interpreted as “failure”. As an example, if there is no sign change, the function might return an iteration count of “0” and a root location outside of the bracket. Update the comments!
b) (3) Write another function that uses your modified version of Bisection to perform a search for MULTIPLE roots in a method inspired by combining incremental search with bisection. An acceptable function header might be:
function [rootLocs, numRoots] = IncrementalBisection(func, xL, xU, numIntervals, tolerance)
You may be able to re-use portions of your code from last homework. The goal here is to break the X bracket into intervals (like incremental search), then apply Bisection.m to each interval (checking the return values to see a root is found) – when roots are found, Bisection will get a pretty decent estimate of their location. The result, however, is faster than incremental search.
c) (2) Using that function, search for the roots of x 1010x 5 +0.4 e x0.4 in the interval [-5,4.5]. There are four roots – note that if you don't use enough intervals, you might not find them all. Report the roots to an estimated relative error of 0.01%.
Bitsection.m file:
function [ iterations, xVal, yVal ] = ...
Bisection( func, lowerBound, upperBound, stopCriteria, ...
maxIter)
% function [ iterations, xVal ] = Bisection( func, lowerBound, upperBound,
% stopCriteria, maxIterations )
% Bisection is a function that applies the "bisection" iterative
% root-finding technique for f(x) = 0. It continues until either of the
% stopping critieria are met.
% Inputs: func, a handle to the function to be evaluated
% lowerBound, the lower bound for "x"
% upperBound, the upper bound for "x"
% stopCriteria, the relative approximate error that is
% considered sufficient for the problem, NOT in
% percentages (e.g. 1% should be entered as
% 0.01)
% maxIterations, the number of iterations to proceed (note of
% course that in bisection, this is redundant
% with stopCriteria, whereas in other methods
% it might be quite important).
% Outputs: iterations, the number of iterations performed
% xVal, the final "x" value approximating the root
% yVal, the value of f(x) at xVal
% Contractions: "val" for value, "iter" for iteration, "func" for
% function, x_ and y_ for "value of x and y
% corresponding to the lower bound (l), upper bound (u)
% and midpoint (m)
% Section: Input cleaning: Begin sanity-checking inputs, throw errors
% This statement checks for insufficient inputs
if(nargin < 4 ...
|| (nargin == 4 && isempty(stopCriteria) ) ) % No stopping criteria given
error('You must specify at least one stopping criteria to avoid an infinite loop');
% This statement checks for missing maxIter
elseif(nargin == 4 ...
|| isempty(maxIter)) %From above, implictly stopCriteria is not empty
maxIter = inf;
% This statement checks for missing stopCriteria
elseif(isempty(stopCriteria)) %Stop criteria empty, MaxIter is not
stopCriteria = eps;
end
% Section: variable initialization
iter = 0;
approximateError = inf;
yL = func(lowerBound);
yU = func(upperBound);
% Section: Actual bisection method, loops while stopping criteria not yet
% met
while(iter < maxIter ...
&& approximateError > stopCriteria)
% Calculate new value for xM, yM
xM = (lowerBound + upperBound) / 2;
yM = func(xM);
% Search which section has a sign change
if(yU*yM < 0)
yL = yM;
lowerBound = xM;
elseif(yL*yM < 0)
yU = yM;
upperBound = xM;
end
newXM = (lowerBound + upperBound) / 2;
approximateError = abs( (xM-newXM) / max(abs(newXM),abs(xM)) );
iter = iter + 1;
end % Main while loop - conditions met for convergence
% Output variables
xVal = newXM;
yVal = func(xVal);
iterations = iter;
end
Explanation / Answer
%%-------------------Bisect.m--------------------------------------
function [bisect]= Bisect(xl,xu,es,imax,xr,iter,ea)
iter=0;
while ( (ea > es) | (iter<=imax))
xrold=xr;
xr=(xl-xu)/2;
iter=iter+1;
if(xr~= 0)
ea=abs((xr-xrold)/xr) *100;
end
test=f(xl)*f(xr);
if (text>0)
xl=xr;
else
ea=0;
end
end
bisect=xr;
%----------------------------------------------------------------------------------
%%-------------------------f.m---------file to define your function--------------------------
function f=f(x)
f= x^10 - 10*x^5 +0.4*x - 0.4; % write your function definition here in. for your exampel
%------------------------------------------------------------------------------------------------
(c)
To run the above program do following
Step 1. put above 2 files (f.m and Bisect.m) in working folder of you Matlab
Step 2. Set the value of all the input argument as follow. input depend on you function, so donot you my values.
"ea" should be grater than "es"
>> xl=-5; % you lower value
xu=-.4.5; % put value to make interval
ex=0.2;
imax=20;
xr=1;
iter=0;
ea=1;
Step 3: Now call the function
>> bisetc=Bisect(xl, xu, ex, imax, xr, iter, ea);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.