Many applications in mathematics involve computing various values of the sin fun
ID: 3688939 • Letter: M
Question
Many applications in mathematics involve computing various values of the sin function. It can be proven that
sin(x) = x – x 3 /3! + x5 /5! - x 7 /7! +
… for all radian values of x. It is important to remember that x must be expressed in radians, not degrees ( radians = 180 degrees). Of course, this is an infinite sum, so we can’t hope to actually add all of these values up! The good news is that the later terms get so small that a partial sum can provide a very nice approximation for the value of sin(x).
You are to write a double precision function (result returned in $f0) called sin with one double precision parameter (in $f12), along with a little driver program for testing your function. Your function should use the summation formula as an approximation for the value of sin(x), using the following guide for terminating the summation: If the absolute value of the next term is less than 1.0e-15, then terminate the summation (and don’t even bother to add in that next term). Even though the summation is valid for all values of x, there is a problem with convergence when you use values of x having absolute values greater than 30 or so. You may assume that we will test your program with values within this range.
Here is a sample execution of my code:
Let's test our sin function!
Enter a (radian) value for x (or 999 to exit): 0
Our approximation for sin(0) is 0
Enter a (radian) value for x (or 999 to exit): 1
Our approximation for sin(1) is 0.8414709848078965
Enter a (radian) value for x (or 999 to exit): 10
Our approximation for sin(10) is -0.54402111088906224
Enter a (radian) value for x (or 999 to exit): -10
Our approximation for sin(-10) is 0.54402111088906224
Enter a (radian) value for x (or 999 to exit): 1.570796326794896 [/2]
Our approximation for sin(1.5707963267948959) is 1
Enter a (radian) value for x (or 999 to exit): -.5232
Our approximation for sin(-0.5232) is -0.49965461045512305
Enter a (radian) value for x (or 999 to exit): -.5235987755982988 [-/6]
Our approximation for sin(-0.52359877559829882) is -0.5
Enter a (radian) value for x (or 999 to exit): 999
Come back soon!
Explanation / Answer
Hello there ,
Kindly find below code and it's O/P.
import java.util.Scanner;
public class SineValueRadiants {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter a (radian) value for x (or 999 to exit):");
double x = scanner.nextDouble();
if(x!=999){
System.out.println("Our approximation for sin("+ x + ") is : " + sin(x));
}else{
System.out.println("Come back soon!");
break;
}
}
scanner.close();
}
public static double sin(double x){
double term = 1;
double n;
double fact = 1;
double sinx;
// Calculate the sinx with 17 terms
for (n = 1, sinx = 0; term <= 100; n = n + 2, term++) {
// Calculate factorial value of n
fact = 1;
for (double i = 1; i <= n; i++) {
fact = fact * i;
}
if (Math.abs((Math.pow(x, n) / fact)) > Math.abs((1 * Math.pow(10, -16)))) { //To terminate
// Verify if the term is an odd number to apply the right term
// sign
if (term % 2 == 1) {
sinx = sinx + Math.pow(x, n) / fact;
} else {
sinx = sinx - (Math.pow(x, n) / fact);
}
} else
break;
}
return sinx;
}
}
===O/P==
Enter a (radian) value for x (or 999 to exit):
0
Our approximation for sin(0.0) is : 0.0
Enter a (radian) value for x (or 999 to exit):
1
Our approximation for sin(1.0) is : 0.8414709848078965
Enter a (radian) value for x (or 999 to exit):
10
Our approximation for sin(10.0) is : -0.54402111088927
Enter a (radian) value for x (or 999 to exit):
-10
Our approximation for sin(-10.0) is : 0.54402111088927
Enter a (radian) value for x (or 999 to exit):
1.570796326794896
Our approximation for sin(1.570796326794896) is : 1.0000000000000002
Enter a (radian) value for x (or 999 to exit):
-.5232
Our approximation for sin(-0.5232) is : -0.49965461045512305
Enter a (radian) value for x (or 999 to exit):
-.5235987755982988
Our approximation for sin(-0.5235987755982988) is : -0.5
Enter a (radian) value for x (or 999 to exit):
999
Come back soon!
Let me know if you have any queries.
Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.