Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sin(x) using the infinite series

Status
Not open for further replies.

kathyla18

Programmer
Jan 26, 2010
1
US
I have to create a program that reads in a value for x in degrees and then calculates sine of x using the sine intrinsic function. Then calculates the sine of x using the infinite series equation.

I know how to write the code to calculate the sine using the intrinsic funtion, but im stuck at how to create the code for the infinite series.

Any help will be appreciated. Thanks
 
The formula for calculating sin(x) using Taylor series can be found here:

So you need to create a function with 2 arguments (x - real and n - integer) which calculates the sum of series in a loop.
The accuracy of the result is depending on n.
Here is the pseudocode (Ruby)
Code:
def taylor_sin(x, n)
  sum = 0
  for k in (0..n)
    sum = sum + (-1)**k * x**(2*k+1)/fact(2*k+1)
  end
  return sum
end
The Fortran implementation would be very similar.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top