Using for loop to calculate sine function (factorial issue)
Hi again, I was working on another homework assignment and I got stuck trying to figure out why my program wasn't computing the right answer. The assignment was to basically write a program that computes the Sine function (as close as you possibly can) without using Math.sin(). I believe that my for loop in the original code is not doing the factorial for the right variable. I'm pretty sure the equation is right as well. I figured out the series needed to compute sin:
Sine = (-1.0^n)(x^(2n+1)) / (2n+1)!
Here's my original code:
Code java:
/* This program is suppose to compute the Sine function without using Math.sin */
import java.io.*;
public class HW5
{
public static void main(String args[]) throws IOException
{
BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
String s;
double Rad, Sine;
int MaxE, i;
Rad = 0.0;
System.out.println("Degrees (d)? or Radians (r)? Please enter (d) or (r): ");
s = keybd.readLine();
s = s.toLowerCase();
s = s.trim();
if (s.equals("d"))
{
System.out.println("Enter a value (Degrees): ");
Rad = Double.parseDouble(keybd.readLine());
Rad = (Math.PI * Rad) / 180;
}
else if (s.equals("r"))
{
System.out.println("Enter a value (Radians): ");
Rad = Double.parseDouble(keybd.readLine());
}
System.out.println("Enter the desired maximum exponent: ");
MaxE = Integer.parseInt(keybd.readLine());
****************************************************************
i = (2 * MaxE) + 1; //This is where I think I messed and confused myself
for (int a = 0; a <= MaxE; a++)
{
i = a * i;
}
Sine = ((Math.pow(-1.0,MaxE))*(Math.pow(Rad, i)) / i!); // I put the ! to indicate where I wanted the factorial to go
System.out.println("The value of Sine is " + Sine);
}
}
My first problem was trying to figure out how to compute a factorial. I looked through the tutorials and found some code for recursive factorials but I couldn't get it to work. I ended up finding a code that I got to work but the problem is is that I'm not sure how I could incorporate it into my original code.
Factorial program:
I apologize for this being so long but I'm not sure if my logic is right or not. Also, does java have a built in factorial method or did I have to make a loop for it? Any help is appreciated.
Re: Using for loop to calculate sine function (factorial issue)
Quote:
I ended up finding a code that I got to work but the problem is is that I'm not sure how I could incorporate it into my original code.
If you don't know, you first should figure out how the code you found to calculate the factorial works. How does it calculate factorial? Knowing this will put you in a much better position to be able to reproduce its functionality in your code. What variable(s) are necessary, and what variable(s) does it produce as a result? Break the problem down, and perhaps to help you do so just isolate this functionality by writing a method that computes the factorial of a parameter and returns the result - this will let you focus primarily on this portion of the code independent of the rest.
Re: Using for loop to calculate sine function (factorial issue)
Quote:
Originally Posted by
copeg
If you don't know, you first should figure out how the code you found to calculate the factorial works. How does it calculate factorial? Knowing this will put you in a much better position to be able to reproduce its functionality in your code. What variable(s) are necessary, and what variable(s) does it produce as a result? Break the problem down, and perhaps to help you do so just isolate this functionality by writing a method that computes the factorial of a parameter and returns the result - this will let you focus primarily on this portion of the code independent of the rest.
Thanks for the suggestion. I was just about to go through that, in fact. I felt that just sitting around and waiting isn't going to help so I'm going through all my code and writing down what each part does and what I intended it to do. I should of down this first instead of asking for help right away. Hopefully this will be solved soon. Thanks again.