okay, so I've modifies my code a bit.
function rts = cubicSS(C)
a = C(1);
b = C(2);
c = C(3);
d = C(4);
rts = [90000 ; 90000 ; 90000];
f = @(x) a.*x^3 + b.*x^2 + c.*x + d;
a1=1;
while f(a1) <= 0 && a1 < inf
a1=2*a1;
end
b1=1;
while f(-b1)>=0 && b1 < inf
b1=2*b1;
end
rcount = 0;
tol = 1e-6;
a1=1;
while f(a1) <= 0 && a1 < inf
a1=2*a1;
end
b1=1;
while f(-b1)>=0 && b1 < inf
b1=2*b1;
end
while 1
p = (a1+b1)/2;
if f(p) == 0
rcount = rcount + 1;
rts(rcount) = p;
break; end
if p-a1 <tol
break; end
if f(a1)*f(p)> 0
a1=p;
else
b1=p;
end
end
end
However, whenever I run this code, I don't get any roots back. right now I'm setting C =[ 1 -3 3 -1] just to test it out. My plan is that after i find one root, I will use deconv function to do long division and then use the quadratic formula on my newly found polynomial .
wanted to know what bugs or errors could possibly be in my code
Since you set your left endpoint as a positive and found its position by taking $f(-b1)$, you start out in the range $[1,2]$ instead of the range $[-1,2]$ like you want. After finding the absolute value of $b1$, make sure it is negative, since that is how your programme works.
Aside from that, you are setting your tolerance based on the distance from $p$ to $a1$, which will eventually terminate, but you never set the value of the root for the "tolerant" version.
I didn't see anything else wrong here.
NOTE, though, that your example cubic will require complex roots.