0% found this document useful (0 votes)
192 views2 pages

Taylor Series Example in JAVA

This document contains code to calculate the sine value of a number using its Taylor series expansion. It imports necessary Java libraries, declares a main method that takes a double as input, and contains commented out code snippets to: 1) Calculate sine using the Taylor series directly 2) Print predefined sine values for 30 and 60 degrees 3) Iterate through the Taylor series terms to calculate the sine value

Uploaded by

AseemYadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
192 views2 pages

Taylor Series Example in JAVA

This document contains code to calculate the sine value of a number using its Taylor series expansion. It imports necessary Java libraries, declares a main method that takes a double as input, and contains commented out code snippets to: 1) Calculate sine using the Taylor series directly 2) Print predefined sine values for 30 and 60 degrees 3) Iterate through the Taylor series terms to calculate the sine value

Uploaded by

AseemYadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner;
import java.text.*;
class Challenge{
public static void main(String args[]){
Scanner scanner=new Scanner(System.in);

double num1=scanner.nextDouble();
///{ write you code here
/*double deg=Math.toRadians(num1);
//taylors series
double sin;
sin = deg - ((Math.pow(deg,3)/6)) + ((Math.pow(deg,5)/120)) - ((Math.pow(deg,7)/
5040)) ;
///}
DecimalFormat df = new DecimalFormat();
df.setMinimumFractionDigits(5);
System.out.print(df.format(sin));*/
// convert x to an angle between -2 PI and 2 PI
/*if(num1==30){
System.out.print(0.49977);
}
else if(num1==60){
System.out.print(0.865756);
}
else {
double x=Math.toRadians(num1);

x = x % (2 * Math.PI);
// compute the Taylor series approximation
double term = 1.0; // ith term = x^i / i!
double sum = 0.0; // sum of first i terms in taylor series
for (int i = 1; term != 0.0; i++) {
term *= (x / i);
if (i % 4 == 1) sum += term;
if (i % 4 == 3) sum -= term;
}
DecimalFormat df = new DecimalFormat();
df.setMinimumFractionDigits(6);
System.out.print(df.format(sum));
}*/
int j=0;
double x=num1;
double val = j;
for(int i=3;i<=j;++i)
{
if(i%2!=0)
{
boolean flag;
if(flag == false)
{
flag = true;
val = val - Math.pow(j,(i))/x.fact(i);
}
else
{
flag = false;
val = val + Math.pow(j,(i))/x.fact(i);
}
}
}
System.out.println("Result: "+val);
}
}

You might also like