Answered step by step
Verified Expert Solution
Link Copied!

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 F(X) is positive at one of these values (say at A) and negative at the other. (When this occurs, we say that [A,B] is a change-of-sign 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 )/2
If F(C)=0 we are done. (This is pretty unlikely). Otherwise, depending on the sign of F(C), we know that the root lies in [A,C] or [C,B]. In any case, our change-of-sign 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 3:
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 [-1,1], 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 [x,itCount]= bisect_cosmx( a, b)
%[x,itCount]= bisect_cosmx( a, b) uses bisection to find a
% root of cosmx between a and b to tolerance of 1.0e-10
% a=left end point of interval
% b=right end point of interval
% cosmx(a) and cosmx(b) should be of opposite signs
% x is the approximate root found
% itCount is the number of iterations required.
% your name and the date
EPSILON =1.0e-10;
fa = cosmx(a);
fb = cosmx(b);
for itCount =1:(???)% fill in using the formula from Exercise 3
x =(b+a)/2;
fx = cosmx(x);
% The following statement prints the progress of the algorithm
disp(strcat('a=', num2str(a),', fa=', num2str(fa),...
', x=', num2str(x),', fx=', num2str(fx),...
', b=', num2str(b),', fb=', num2str(fb)))
if ( fx ==0)
return; % found the solution exactly!
elseif ( abs ( b - x )< EPSILON )
return; % satisfied the convergence criterion
end
if ( sign(fa)* sign(fx)<=0)
b = x;
fb = fx;
else
a = x;
fa = fx;
end
end
error('bisect_cosmx 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 code--use 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

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions