Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am working on the following Matlab Homework Problem and wanted to compare my answers. I have supplied the problem, Code used and the partial

I am working on the following Matlab Homework Problem and wanted to compare my answers. I have supplied the problem, Code used and the partial answer. Please show all work in matlab format.
Thanks
9.Modify the user-defined function Bisection, so that the table is not generated and the outputs are the approximate root and the number of iterations needed for convergence. All other parameters, including default values, are to remain unchanged. Save this function as Bisection_2. Execute Bisection_2 to find the root of in the interval .
Partial Solution:
>> f = @(x)(sin(x)+2*cos(x)-1);
>>[c,k]= Bisection_1(f,5,6)
c =5.6397
k =14
MatLab Code used:
function c = Bisection2(f, a, b, kmax, tol)
%
% Bisection2 uses the bisection method to approximate a root of f(x)=0
% in the interval [a,b].
%
% c = Bisection2(f, a, b, kmax, tol), where
%
% f is an anonymous function representing f(x),
% a and b are the endpoints of interval [a,b],
% kmax is the maximum number of iterations (default 20),
% tol is the scalar tolerance for convergence (default 1e-4),
%
% c is the approximate root of f(x)=0.
%
if nargin <5|| isempty(tol), tol =1e-4; end
if nargin <4|| isempty(kmax), kmax =20; end
if f(a)*f(b)>0
c = 'failure';
return
end
disp(' k a b c (b-a)/2')
for k =1:kmax
c =(a+b)/2; % Find the first midpoint
if f(c)==0% Stop if a root has been found
return
end
fprintf('%3i %11.6f%11.6f%11.6f%11.6f
',k,a,b,c,(b-a)/2)
if (b-a)/2< tol % Stop if length of interval is within tolerance
return
end
if f(b)*f(c)>0% Check sign changes
b = c; % Adjust the endpoint of interval
else
a = c;
end
end

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

More Books

Students also viewed these Databases questions