0

I have been working on a script in Java to solve cubic equations. I've been following along with Mathematics and Physics for Programmers to teach my self about, well maths and physics.

As far as I can test it is working correctly, but I have not been able to find an equation with a discriminant of zero to test that case.

The equation that the code is based on is as follows:

eq

If the discriminant is > 0:

disc1

if the discriminant = 0 (I do not know an equation to test this):

disc2

if the discriminant < 0:

disc3

After finding t transform it by:

transform

I'd be very grateful if someone can help me work out how to test this is working correctly.

For anyone interested my code is here: https://github.com/sarcoma/Java-Algorithms/blob/master/src/com/oacc/maths/SolveCubic.java

1 Answers1

1

I figured I'd expand on my comment. If you have a polynomial $f(x) = a_{n}x^n + \cdots + a_{0}$ and its roots are $r_{1}, \dots, r_{n}$ then the discriminant $D$ is equal to $$ D = a_{n}^{2n - 2} \prod_{i < j} (r_{i} - r_{j})^2.$$

From this definition, it is immediate that $f$ has discriminant 0 if and only if it has a repeated root.

So if you want a cubic with discriminant zero, try something like $(x - 4)^2(x + 5)$ such as John Gowers suggested in the comments.

Oiler
  • 3,413
  • Thanks for the answer, this is all going over my head, I can understand the form Ax^3 + Bx^2 + Cx + D = 0 which I'm simplifying to x^3 + ax^2 + bx + c = 0 I'm trying to find A, B, C and D that I can punch in to my function, I'm not sure how would translate: (x−4)2(x+5). – OrderAndChaos Apr 22 '17 at 21:02
  • First, note that we aren't setting a polynomial equal to zero anywhere (though this is certainly necessary for determining the roots). Now if you want to write $(x - 4)^2 (x +5)$ in the form $Ax^3 + Bx^2 + Cx + D$, you have to expand the polynomial-- $(x - 4)^2 = x^2 - 8x + 16$ so $(x -4)^2(x + 5) = (x^2 - 8x + 16)(x +5)$. I'll leave the rest of the algebra to you. – Oiler Apr 22 '17 at 21:07
  • Thanks so much, when I manage that you'll get your tick :) – OrderAndChaos Apr 22 '17 at 21:09
  • $x^{3} - 3 x^{2} - 24 x + 80$ has Δ = 0 – OrderAndChaos Apr 22 '17 at 21:41
  • And to top it off the code works too! Thanks so much for your help. Do you know what I'd need to google to find out more about: $a(x−b)^{2}(x−c)$, no worries if not. – OrderAndChaos Apr 22 '17 at 21:51
  • @Sarcoma that's kind of vague. What exactly do you want to know more about? – Oiler Apr 22 '17 at 22:04
  • Ha, no problem, it's just that John Gowers mentioned "this is a characterization of cubics with discriminant 0", I wondered if there was something I could research to find out more. But as long as it does what I need, it's fine. – OrderAndChaos Apr 22 '17 at 22:24
  • Well my answer outlines exactly why that's the case-- the discriminant is zero if and only if a polynomial has a repeated root. So if a cubic has discriminant zero, it will look something like $a(x-b)^2)(x-c)$ where $b$ isn't necessarily distinct from $c$. – Oiler Apr 22 '17 at 23:11