java - Placement of Math.toRadians for solving quadratic and cubic equations -
i writing program solves either quadratic or cubic equations. thing don't know if placing math.toradians
correctly.
the code following:
public double[] getraices(double a,double b, double c, double d) throws complexexception { if (a==0){ double discriminante=math.pow(c,2)+((-4)*b*d); if(discriminante>=0){ this.raices[0]=(c*(-1)+math.sqrt(discriminante))/(2*b); this.raices[1]=(c*(-1)-math.sqrt(discriminante))/(2*b); }else{ throw new complexexception("no hay solucion real"); } } else{ double f=((3*c/a)-(math.pow(b,2)/math.pow(a,2)))/3; double g=((2*math.pow(b,3)/math.pow(a,3))-(9*b*c/math.pow(a,2))+(27*d/a))/27; double h=(math.pow(g,2)/4)+(math.pow(f,3)/27); if(f+g+h==0){ raices [0]=math.cbrt(d/a)*(-1); raices [1]=math.cbrt(d/a)*(-1); raices [2]=math.cbrt(d/a)*(-1); }else{ if(h<=0){ double i=math.sqrt((math.pow(g,2)/4)-h); double j=math.cbrt(i); double k=math.acos(math.toradians(-1*(g/2*i))); system.out.println(" "+k+" "); double l=j*(0-1); double m=math.toradians(math.cos(math.toradians(k/3))); system.out.println(" "+m+" "); double n=math.sqrt(3)*math.sin(math.toradians(k/3)); system.out.println(" "+n+" "); double p=(b/(3*a)*(0-1)); raices [0]=2*j*math.cos(math.toradians(k/3))-(b/(3*a)); raices [1]=(l*(m+n))+p; raices [2]=(l*(m-n))+p; }else{ double r=((0-1)*(g/2))+math.sqrt(h); double s=math.cbrt(r); double t=((0-1)*(g/2))-math.sqrt(h); double u=math.cbrt(t); throw new complexexception("2 de las raices son imaginarias pero una raiz es real: "+math.floor(raices [0]=(s+u)-(b/(3*a)))); } } } return raices; }
but problem in if (h<=0)
.
i tested code against web page , found several errors. first g /2i, wrote g/2*i instead of g/2/i or (g/(2*i). , several math.toradians not necessary (webpage said calculations in radians, no need convert).
i added println following formula :
package test; public class cubic { private double[] raices = new double[3]; public static void main(string[] args) throws complexexception { double[] raices = new cubic().getraices(2, -4, -22, 24); system.out.println(raices[0] + "," + raices[1] + "," + raices[2]); } public double[] getraices(double a, double b, double c, double d) throws complexexception { if (a == 0) { double discriminante = math.pow(c, 2) + ((-4) * b * d); if (discriminante >= 0) { this.raices[0] = (c * (-1) + math.sqrt(discriminante)) / (2 * b); this.raices[1] = (c * (-1) - math.sqrt(discriminante)) / (2 * b); } else { throw new complexexception("no hay solucion real"); } } else { double f = ((3 * c / a) - (math.pow(b, 2) / math.pow(a, 2))) / 3; system.out.println("f=" + f); double g = ((2 * math.pow(b, 3) / math.pow(a, 3)) - (9 * b * c / math.pow(a, 2)) + (27 * d / a)) / 27; system.out.println("g=" + g); double h = (math.pow(g, 2) / 4) + (math.pow(f, 3) / 27); system.out.println("h=" + h); if (f + g + h == 0) { raices[0] = math.cbrt(d / a) * (-1); raices[1] = math.cbrt(d / a) * (-1); raices[2] = math.cbrt(d / a) * (-1); } else { if (h <= 0) { double = math.sqrt((math.pow(g, 2) / 4) - h); double j = math.cbrt(i); double k = math.acos(-1 * (g / 2 / i)); system.out.println("k=" + k + " "); double l = j * (0 - 1); system.out.println("l=" + l + " "); double m = math.cos(k / 3); system.out.println("m= " + m + " "); double n = math.sqrt(3) * math.sin(k / 3); system.out.println("n= " + n + " "); double p = (b / (3 * a) * (0 - 1)); system.out.println("p= " + p + " "); raices[0] = 2 * j * math.cos(k / 3) - (b / (3 * a)); raices[1] = (l * (m + n)) + p; raices[2] = (l * (m - n)) + p; } else { double r = ((0 - 1) * (g / 2)) + math.sqrt(h); double s = math.cbrt(r); double t = ((0 - 1) * (g / 2)) - math.sqrt(h); double u = math.cbrt(t); throw new complexexception( "2 de las raices son imaginarias pero una raiz es real: " + math.floor(raices[0] = (s + u) - (b / (3 * a)))); } } } return raices; } }
Comments
Post a Comment