Hello, Can someone please help me write MATLAB code that uses the Bisection Meth
ID: 3643033 • Letter: H
Question
Hello,Can someone please help me write MATLAB code that uses the Bisection Method to solve
f(x) = 0 (where f(x) = 6x^3 - 13x^2 + x + 2) on the interval [-1, 1]?
Cheers
Explanation / Answer
function c = bisect(a, b, delta) fa = f(a); %% compute initial values of f(a) and f(b) fb = f(b); Implements the bisection method %% %% Input: a the left endpoint of the interval %% b the right endpoint of the interval %% delta the tolerance/accuracy we desire %% %% Output: c the approxmiation to the root of f fprintf('initial interval: a=%d, b=%d, fa=%d, fb=%d ',a,b,fa,fb) while ( abs(b - a) > 2*delta ) %% While the size of the interval is %% larger than the tolerance c = (b + a)/2; %% Set c to be the midpoint of the interval fc = f(c); %% Calculate the value of f at c if sign(fc) ~= sign(fb) %% If f(c) and f(b) are of different sign, then %% f must have a zero between c and b (by the %% Intermediate Value Theorem). a = c; fa = fc; else %% This is the case where f(a) and f(c) are of %% different sign. %% b = c; fb = fc; end %% Repeat the algorithm on the new interval fprintf(' a=%d, b=%d, fa=%d, fb=%d ',a,b,fa,fb) end function fx = f(x) fx = 6*x^3 - 13*x^2 + x + 2; %% Enter your function here. return; ---------------------------------------------------------------------------- Save the file as bisect.m Example Run : >>bisect(-1,1,0.00001); Please note that there are 2 roots between -1 and 1. Bisection method will find only one root. Change the interval [-1,0] and [0,1] to get the roots.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.