Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Excel 2024 In 7 Days

Authors: Alan Dinkins

1st Edition

B0CJ3X98XK, 979-8861224000

More Books

Students also viewed these Databases questions

Question

Explain the nature of human resource management.

Answered: 1 week ago

Question

Write a note on Quality circles.

Answered: 1 week ago

Question

Describe how to measure the quality of work life.

Answered: 1 week ago