8 Implement a MATLAB function bisection.m of the form function [r, h] = bisectio
ID: 3195226 • Letter: 8
Question
8 Implement a MATLAB function bisection.m of the form function [r, h] = bisection(a, b, f, p, t) % a: Beginning of interval [a, b] %b: End of interval [a, b] % f: function handle y = f(x, p) % p: parameters to pass through to f % t: User-provided tolerance for interval width At each step J = 1 to n, carefully choose m as in geometric mlean bisection (watch out for zeroes!). Replace a, b] by the smallest interval with endpoints chosen from a, m, b whiclh keeps the root bracketed. Repeat until a f value exactly vanishes, b-aExplanation / Answer
error('Wrong number of input arguments.');
elseif(nargin==3)
if(f(a)*f(b) >= 0)
disp(' The function has positive value at the end points of interval .');
disp('The root can't be found using bisection method, use some other method.');
root = 'Root can't be found using bisection method' ;
return;
else
fprintf('Iteration a b m f(a) f(b) f(m) ');
fprintf('============== ====== === ======= =========== );
m=(a+b)/2;
if (abs(f(m)) <= 1e-6)
root =m;
return;
else
Iter = 0;
while (abs(f(m) ) >= 1e -6)
Iter = Iter+1;
m = (a+b)/2;
if (f(a) *f(m) >0)
a =m ;
else
b = m;
end
fprintf(' % 3d' ,Iter);
fprintf(' %20.4f' ,a);
fprintf(' %12.4f' ,b);
fprintf(' %12.4f' ,m);
fprintf(' %14.4f' ,f(a);
fprintf(' % 16.4f' ,f(b));
fprintf(' % 16.4f' ,f(m));
fprintf(' ');
end
root = m;
end
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.