Use Matlab Please!!! One method for finding a root of a function is the bisectio
ID: 3844933 • Letter: U
Question
Use Matlab Please!!!
One method for finding a root of a function is the bisection method. Here is a nice writeup on the bisection method: http://www.mathwarehouse.com/calculus/continuity/continuity-bisection-method.php (Links to an external site.)Links to an external site.
Write a function bisect( ) that finds the root of a polynomial ax3+bx2+cx+d over an interval [A,B]. The function takes as input the coefficients a, b, c, d, and the interval limits A and B. The function returns as output the value of the root over the interval [A,B], if it exists. The function also returns as output a message (flag) that informs the user if no roots exist over the interval [A,B]. It should be clear that you cannot use any of MATLAB's built-in root finding functions to do this problem. Use the bisection method.
a = -1;
b = 1;
c = -1;
d = 10;
A = 0;
B = 5;
[root,flag] = bisect(a,b,c,d,A,B)
Explanation / Answer
function [root,flag] = bisect(a,b,c,d,A,B)
fun = @(a,b,c,d,x) a*(x^3) + b*(x^2) + c*x + d;
if fun(a,b,c,d,A) * fun(a,b,c,d,B) > 0
flag = 0;
root = 0;
return;
end
if fun(a,b,c,d,A) > fun(a,b,c,d,B)
t = A;
A = B;
B = t;
end
root = (A + B)/2;
while abs(fun(a,b,c,d,root)) > 0.001
if fun(a,b,c,d,root) > 0
B = root;
else
A = root;
end
root = (A + B)/2;
end
flag = 1;
end
flag 0 is failure flag
flag 1 is success
I hope this helped you. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.