2016-12-29 19 views
0

私は二次方程式または三次方程式を解くプログラムを書いています。問題は、Math.toRadiansを正しく配置しているかどうかわかりません。二次方程式と三次方程式を解くためのMath.toRadiansの配置

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; 
} 

しかし、問題はif (h<=0)である:

コードは次のようです。

+0

をソリューションがhttps://en.wikipedia.org/wiki/Cubic_function –

+0

HTTPに関する実装されているもの://www.1728?。 org/cubic2.htm –

+0

http://www.1728.org/cubic2.htm私はこの公式を1本の実根と2本の想像上の根に使用し、3本の等価な実根はうまく機能しますが、3本の実根は正しく機能しません。 –

答えて

0

私はウェブページに対してコードをテストし、いくつかのエラーが見つかりました。 最初はg/2iですが、g/2/iや(g /(2 * i)の代わりにg/2 * iと書いておきますが、いくつかのMath.toRadiansは必要ありません(ウェブページでは計算はラジアンです。 。)

を変換するIは、式を次のよう手助けするのprintlnを追加しました:

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 i = 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; 
    } 
} 
+0

ありがとう、私は明日それをテストします。私はMath.toRadiansを離陸しようとしましたしかし結果はNaNでしたが、それがあなたのために働くなら、それは私のためにもなります。(私の英語でいくつかの間違いを犯してしまった場合は申し訳ありません。 –

関連する問題