Objectives: learn Java syntax .display to the terminal .for loops static methods
ID: 3699932 • 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 e and sinx Each of these functions can be approximated with Taylor Series, as follows: 21 31 41 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 I operation is known as Eactorial. n is the product of all the positive integers n, where n is positive. in otherwords -123. -1234 To calculation e, 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: dont 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 sinfx), 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 sinfx) is in radians, not degrees. Here is a table showing degree to radian equivalencies:Explanation / Answer
//Program to calculate e^x without using math library. The program uses the following split logic to calculate the exponential value : e^x = 1+(x/1)(1+ (x/2)(1+ (x/3)(......)) //
import java.io.*;
import java.util.*;
class EXP {
public static void main( String[] args){
Scanner s = new Scanner(System.in);
double x = s.nextDouble();
int n=15;
System.out.println("x e^x");
System.out.println(x+" "+exponential(n,x));
}
static double exponential(int n, double x){
double sum =1.0;
for(int i=n-1; i>0;--i)
sum = 1+x*sum/i;
return sum;
}
}
// Program to calculate value to sin(x) by series without using math library.sin(x)=x-x^3/3!+x^5/5!+.....
//Terms are positive for numbers such that number%4==1 and negative for numbers such that number%4==3 and for the rest it is 0.
public class sin{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
double x = s.nextDouble();
x = x%(2*Math.PI);
double term=1.0;
double sum =0.0;
for(int i=1;i<=15;i++){
term*=(x/i);
if(i%4==1)sum+=term;
if(i%4==3)sum-=term;
}
System.out.println("x sin(x)");
System.out.println(x+" "+sum);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.