Matlab Coding finding zeros without using fzero or roots function

824 Views Asked by At

So i am a completely new at Matlab. I'm basically suppose to develop a function in Matlab that finds the zeros of a cubic polynomial. real and complex. I'm pasting below what I have so far.

I started off by using the bisection method and it's not working for a basic polynomial i know the roots to.

function rts = cubicxx(C)
a = C(1);
b = C(2);
c = C(3);
d = C(4);
start = 1e20;
last = -1e20;
rts = [8000 ; 8000; 8000];
rcount = 0;
f = @(x) (a).*x^3 + (b).*x^2 + (c).*x + d ;

%for loop 
 %   x(i)=
  %  i = i+1;
%end

 %for rts='a' : 'c', rts, maybe something i will use later


tol = 10^(-6)
while (rcount < 4) %while true Starting off with the Bisection method.
    p = (start + last)/2;
    if f(p) == 0
        rcount = rcount + 1;
        rts (rcount) = p; 
        break; end
    if p-start <tol 
        break; end
    if f(start)*f(p)> 0
        start = p;
    else
        last = p;
    end
end
 end

the "rcount" is suppose to help me print the roots. Anyone's help would be greatly appreciated.