Question: In Matlab, programs may be written and saved in files with a suffix . m called M - files. There are two types of M

In Matlab, programs may be written and saved in files with a suffix .m called M-files. There are two
types of M-file programs: functions and scripts.
The following function program does n iterations of the bisection method.
function [xe mybisect (f,a,b,n)
% function [xe]= mybisect (f,a,b,n)
% Does n iterations of the bisection method for a function f
% Inputs : f-- a function
%a,b-- left and right edges of the interval
%n-- the number of bisections to do.
% Outputs : x-- the estimated solution of f(x)=0
% e -- an upper bound on the error
% evaluate at the ends and make sure there is a sign change
c=f(a);
d=f(b);
if c**d>0.0
error ('Function has same sign at both endpoints. ')
end
disp('{:x,y')
for i=1 : n
% find the middle and evaluate there
x=a+b2;
y=f(x);
disp([x,y])
ify=0.0% solved
,a=x;
b=x;
break
% jumps out of the f
% decide which half
ifc**y0
b=x;
else
a=x;
if y==0.0% solved the equation exactly
a=x;
b=x;
break
% jumps out of the for loop end
% decide which half to keep, so that the signs at the ends differ
if c**y0
b=x;
else
a=x;
end
end
% set the best estimate for x and the error bound
x=a+b2;
e=b-a2;
end
Your turn
Modify mybisect in above to create mybisectwhile to solve until the absolute error is bounded
by a given tolerance. Use a while loop to do this. Run your program on the function
f(x)=2x3+3x-1 with starting interval 0,1 and a tolerance of10-8.
How many steps does the program use to achieve this tolerance? (You can count the
steps by adding 1 to a counting variable i in the loop of the program.)
How big is the final residual f(x)?
Turn in your program and a brief summary of the results.
The commands in the shell aremybisectwhile(f,a,b,tol,n)
Here is the sample function with "while" loop.
function x= mynewtontol , tol
 In Matlab, programs may be written and saved in files with

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!