Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objectives: *learn Java syntax *display to the terminal *for loops *static metho

ID: 3816305 • Letter: O

Question

Objectives:

*learn Java syntax

*display to the terminal

*for loops

*static methods with parameters and returns

For this homework, you are going to implement approximations for ex and sin(x). Each of these functions can be approximated with Taylor Series, as follows:

Restrictions

This program cannot use the exp() or sin() method in the Math class, nor use the Math.E constant.

Do not use any if statements in your solution.

Calculation Specification:

The ! operation is known as Factorial. n! is the product of all the positive integers <= n, where n is positive. In otherwords

n! = 1 * 2 * 3 * ... * n

5! = 1 * 2 * 3 * 4 * 5

To calculation ex, you must use a loop and calculate the sum above for the first 15 terms. To obtain maximum numerical range and improve efficiency, you will want to progressively compute each term: don't calculate the 4th term by raising x to the 4th power and dividing by 4! Instead, determine the 4th term based on the value you already have for the 3rd term.   In other words, determine how the 4th term differs mathematically from the 3rd term, and perform only those additional operations.

To calculate sin(x), you must use a loop to calculate the sum above for the first 15 terms. Again, progressively compute each term.

Note that the unit for x in sin(x) is in radians, not degrees. Here is a table showing degree to radian equivalencies:

You can read more about radians here http://www.purplemath.com/modules/radians.htm

Display

Your program should create a display similar to this:

Note: We know that the sin of and 2 are both zero. What you see here are accuracy issues with floating point number representation/calculation in computers. You can read more about that here https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

Other Requirements

Your design must use procedural decomposition.

Use class constants where appropriate.

The program must start with main().

Your file must have a program comment at the top. This description, at a minimum, is one to two lines explaining what the program does.

Include your name and date in the file comments.

Each method must include a block comment that describes

what the method does

a description of parameters (if there are any)

what is returned (if anything is returned)

In addition to the block comments, also include algorithm comments, that help to explain your algorithms.

Use good style, such as good variable and method names, class constants, appropriate indentation, etc.

Grading

/15 program runs, and good decomposition
/5 proper comments, style

Good Luck!

degrees radians 90 /2 180 270 3/2 360 2

Explanation / Answer


/* program is written to LearnJava the aspects of java here we are calculating e^x and sin(x) without using predefined libraries */
public class LearnJava{

   public static void main(String ar[]){
       int numberOfTerm= 15;
       double x = 1.0;
       //printf("e^x = %f", exponential(numberOfTerm, x));
       System.out.println("x           e^x");
       System.out.println("=           ===");
       System.out.println("e^" +numberOfTerm +"=       " +exponential(numberOfTerm, x));
       System.out.println("x           sin(x)");
       System.out.println("=           ====");
       System.out.println(numberOfTerm +"PI/2        " +sin(numberOfTerm));

   }
  
   public static double exponential(int numberOfTerm, double x)
   {
       double sum = 1.0; // initialize sum of series
       int j=1;
       for (int i = numberOfTerm - 1; i > 0; --i )
       {
           sum = 1 + x * sum / i;
           System.out.println("e^" +(j++) +"=       " +sum );
       }
     
       return sum;
   }
  
   public static double sin(int n) {
// angle to radians
       double rad = n*1./180.*Math.PI;
       int j=1;
       // the first element of the taylor series
       double sum = rad;
       // add them up until a certain precision (eg. 10)
       for (int i = 1; i <= n; i++) {
           if (i % 2 == 0) {
               sum += Math.pow(rad, 2*i+1) / factorial(2 * i + 1);
               System.out.println((j++) +"PI/2       " +sum );
           }
           else {
               sum -= Math.pow(rad, 2*i+1) / factorial(2 * i + 1);
               System.out.println((j++) +"PI/2       " +sum );
           }
       }
       return sum;
   }
  
   public static double factorial(double n) {
       if (n <= 1) // base case
           return 1;
       else
           return n * factorial(n - 1);
   }

}