Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This question is a matlab code question. We are asked to both write the code and explain what we are doing in that line. The
This question is a matlab code question. We are asked to both write the code and explain what we are doing in that line.
The Bisection Idea
The idea of the bisection method is very simple. We assume that we are given two values A and B and that the function FX is positive at one of these values say at A and negative at the other. When this occurs, we say that AB is a changeofsign interval for the function. So we know that assuming F is continuous that there must be one at least one root in the interval.
Consider the point
C A B
If FC we are done. This is pretty unlikely Otherwise, depending on the sign of FC we know that the root lies in AC or CB In any case, our changeofsign interval is now half as large as before. Repeat this process with the new change of sign interval, until the interval is "sufficiently small", say no larger than epsilon. Declare victory.
We are guaranteed to converge. We can even compute the maximum number of steps this will take. We know in advance how well we will approximate the root X These are very powerful facts, which make bisection a robust algorithm that is it is very hard to defeat it
Exercise :
a If we know the start points A and B and the interval size tolerance epsilon we can predict beforehand the number of steps the bisection code will probably take. What is this formula?
b Name a function which has a root in the interval but for which bisection could not be used.
Here is a Matlab function that carries out the bisection algorithm for our cosmx function. In Matlab, returned values are separated from arguments, so the variables x and itCount are written on the left of an equal sign.
function xitCount bisectcosmx a b
xitCount bisectcosmx a b uses bisection to find a
root of cosmx between a and b to tolerance of e
aleft end point of interval
bright end point of interval
cosmxa and cosmxb should be of opposite signs
x is the approximate root found
itCount is the number of iterations required.
your name and the date
EPSILON e;
fa cosmxa;
fb cosmxb;
for itCount : fill in using the formula from Exercise
x ba;
fx cosmxx;
The following statement prints the progress of the algorithm
dispstrcata numstra fa numstrfa
x numstrx fx numstrfx
b numstrb fb numstrfb
if fx
return; found the solution exactly!
elseif abs b x EPSILON
return; satisfied the convergence criterion
end
if signfa signfx
b x;
fb fx;
else
a x;
fa fx;
end
end
errorbisectcosmx failed with too many iterations!
Remarks about the code: Compare the code and the algorithm. The code is similar to the algorithm, but there are some significant differences. For example, the algorithm generates sequences of endpoints and and midpoints The code keeps track of only the most recent endpoint and midpoint values. This is a typical strategy in writing codeuse variables to represent only the most recent values of a sequence when the full sequence does not need to be retained.
A second difference is in the looping. There is an error exit in the case that the convergence criterion is never satisfied. If the function ever exits with this error, it is because either your estimate of the maximum number of iterations is too small or because there is a bug in your code.
Note the symbol to represent equality in a test. Use of a single sign causes a syntax error in Matlab. In languages such as C no syntax error is produced but the statement is wrong nonetheless and can cause serious errors that are very hard to find.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started