patternjavaMinor
Approximating sines and cosines using up to five terms of the Taylor series
Viewed 0 times
thefivetaylorsinescosinesusingapproximatingandseriesterms
Problem
I have one programming question:
The sine and cosine of \$x\$ can be computed as follows:
\$\sin(x) = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + \frac{x^9}{9!} - \dots\$
\$\cos(x) = 1 - \frac{x^2}{2!} + \frac{x^4}{4!} - \frac{x^6}{6!} + \frac{x^8}{8!} - \dots\$
Your task is to compute the sine and cosine for given values of \$x\$ (where \$x\$ is in radians) using the above series up to 5 terms.
Input Format
First line will contain \$N\$, the number of test cases. Next \$N\$ lines will contain the input values of \$x\$:
\$1 \le N \le 50\$
\$0 \lt x \lt 10\$
Each value of \$x\$ can contain up to 2 places of decimal in radians.
Output Format
2 \$N\$ lines, corresponding to the \$N\$ input values of \$x\$. For each input, you will output 2 lines. First line will be the sine and the second line will be the cosine of \$x\$. An error margin of \$\pm\$0.001 will be tolerated while evaluating the answers. Please round off your answer to 3 decimal places.
Sample Input
Sample Output [sic]
and what I have tried:
Please review my code. In particular, I am concerned about performance,
The sine and cosine of \$x\$ can be computed as follows:
\$\sin(x) = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + \frac{x^9}{9!} - \dots\$
\$\cos(x) = 1 - \frac{x^2}{2!} + \frac{x^4}{4!} - \frac{x^6}{6!} + \frac{x^8}{8!} - \dots\$
Your task is to compute the sine and cosine for given values of \$x\$ (where \$x\$ is in radians) using the above series up to 5 terms.
Input Format
First line will contain \$N\$, the number of test cases. Next \$N\$ lines will contain the input values of \$x\$:
\$1 \le N \le 50\$
\$0 \lt x \lt 10\$
Each value of \$x\$ can contain up to 2 places of decimal in radians.
Output Format
2 \$N\$ lines, corresponding to the \$N\$ input values of \$x\$. For each input, you will output 2 lines. First line will be the sine and the second line will be the cosine of \$x\$. An error margin of \$\pm\$0.001 will be tolerated while evaluating the answers. Please round off your answer to 3 decimal places.
Sample Input
5
2.83
3.24
0.99
2.74
5.04Sample Output [sic]
0.309
-0.943
-0.089
-0.963
0.836
0.549
0.392
-0.914
0.195
2.746and what I have tried:
public class Trigonometric_Ratios
{
int factorial(int number)
{
int result = 1;
for (int i = 1; i 0)
cosx+=Math.pow(-1,j)*Math.pow(x,c-1)/factorial(c-1);
}
sinx=Math.round( sinx * 1000.0 ) / 1000.0;
cosx=-cosx;
cosx=Math.round( cosx * 1000.0 ) / 1000.0;
System.out.println(sinx+" \n"+(cosx));
}
public static void main(String [] args)
{
int n;
Trigonometric_Ratios tr=new Trigonometric_Ratios();
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
double a[]=new double[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextDouble();
}
for(int i=0;i<n;i++)
{
tr.calc(a[i]);
}
}
}Please review my code. In particular, I am concerned about performance,
Solution
Instead of computing
Math.pow() from scratch, multiply the previous numerator by x2. (I suspect that Math.pow() uses logarithms so that it can handle the general case.) Also, instead of computing the factorial from scratch, multiply the previous denominator by the next two numbers.Context
StackExchange Code Review Q#31577, answer score: 2
Revisions (0)
No revisions yet.