//solve diff-eq uing difference equations //solve equations of the form ax^2 + bx + c = y //this example solves x^2 + 2x + 4 = 0 public class diffeq { public static void main ( String args[] ) { //first find roots //X^2 + 2X + 4 = 0 // (b+- sqrt ( b^2 -4ac ))/2a double a = 1; double b = 2; double c = 4; if ( ( b*b-4*a*c )<0 ) { //complex roots double r1_real = b/(2*a); double r2_real = b/(2*a); double r1_img = Math.sqrt ( (-1)*(b*b - 4*a*c) )/(2*a); double r2_img = (-1) * (Math.sqrt ( (-1)*(b*b - 4*a*c) ))/(2*a); double R1 = Math.sqrt( r1_real*r1_real + r1_img*r1_img ); double theta = Math.atan2( r1_img, r1_real ); System.out.println ( theta ); theta = theta*180/Math.PI; System.out.println ( " y = " + R1 + "^n ( A cos (" + theta + "*n) + B sin (" + theta + "*n ) " ); }else{ //real roots double r1 = ( b + Math.sqrt( b*b - 4*a*c) )/( 2*a ); double r2 = ( b - Math.sqrt( b*b - 4*a*c) )/( 2*a ); if ( r1 == r2 ) { System.out.println ( " y = " + r1 + "x^n + " + r2 + "nx^n " ); }else{ System.out.println ( " y = " + r1 + "x^n + " + r2 + "x^n " ); } } } }