Monday 6 December 2010

Exponential Series

Below is a Java program for computing the Exponential Series


at a particular x, upto a specified number of terms. The value of x and the summation required upto a particular number of terms can be assigned to the member variables variable and numberOfTerms inside the main() method.

class Factorial {
    static long factorial (byte number)
    {
      if(number == 0 || number == 1)
          return (long) 1;
      else
          return (long) number * factorial((byte) (number - 1));
    }
}

class ExponentialSeriesSummation {                
    static double exponentialSeriesSummation(double x,byte n)
    {
      double exponentialSeries = 0;
      for(byte i = 0;i < n;i++)
      {
           exponentialSeries += Math.pow(x, (int) i) / (double)                                            Factorial.factorial(i);
      }
     
return exponentialSeries;
    }
}

public class ExponentialSeries {

    public static void main(String[] args) {
      double variable = 1;
      byte numberOfTerms = 66;
      

      System.out.println("The sum of the Exponential Series at x = " + variable 
      + " upto the first " + numberOfTerms + " terms is " +
      ExponentialSeriesSummation.exponentialSeriesSummation(variable, numberOfTerms));

   }
}


You'll get the output

     The sum of the Exponential Series at x = 1.0 upto the first 66 terms is 
     2.7182818284590455  

which is the same value generated by the method exp() in the java.lang.Math class. You can add the below line just below the last in the above code to check that.

     ...
     ...
     System.out.println("Using the exp() method of the java.lang.Math class, the value      of the Exponential Function at x = " + variable + " is " + Math.exp(variable));
 


 The Exponential Function ex generated by wxMaxima

No comments:

Post a Comment