> x1 = ((-2)*(sqrt(q))*(th/3)) - (a1/3);
Your graphic contains a cos() as well

ARGH! Thanks...that was a really stupid mistake, a pretty much rewrote the code like 3 times trying to fix it