Truncated Sin Series in FORTRAN

Click For Summary

Discussion Overview

The discussion revolves around a FORTRAN program intended to calculate a truncated value of the sine function using its series expansion. Participants are exploring issues related to the accuracy of the sine approximation, particularly at angles greater than 20 degrees, and are seeking to identify and correct errors in the implementation of the series expansion and factorial calculation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses confusion and frustration regarding discrepancies between the program's output and the intrinsic sine function, particularly at larger angles.
  • Another participant questions the truncation of the series and suggests that a significantly truncated series will lead to larger errors at higher angles.
  • A participant identifies a potential issue with the factorial calculation, noting that only odd numbers are being multiplied, which does not yield the full factorial.
  • Suggestions are made to modify the factorial calculation to account for both odd and even numbers, with conditional logic proposed to handle the first iteration correctly.
  • A participant considers implementing a condition to exit the loop based on the accuracy of the sine approximation compared to the intrinsic function.
  • There is a discussion about updating the factorial calculation in two steps to ensure accuracy, with one participant reflecting on their oversight regarding the factorial issue.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to correct the program, as multiple suggestions for addressing the factorial calculation and series truncation are presented. The discussion remains unresolved regarding the optimal implementation.

Contextual Notes

Limitations include the potential misunderstanding of how the series truncation affects accuracy, as well as the incomplete handling of factorial calculations in the current implementation.

mattmac.nuke
Messages
22
Reaction score
0
I am very much confused and frustrated at this point and would just like to understand what I'm doing wrong... This program is supposed to calculate a truncated value of sine using it's series expansion beginning at i = 1. At values under 20 degrees it compares almost exactly with the intrinsic sine function, but after that it's as much as .06-.23 off! This is my first course in programming in general, and I've been very good at Fortran thus far, but there's something I'm missing in this case... This is the program I have been trying to fix:

program series
implicit none
real :: n_factorial, sin_x, x, v1, sum_1, n_1
integer :: n, i

write (*,*) 'enter n'
read (*,*) n
write (*,*) 'enter x in degrees'
read (*,*) x

n_factorial = 1
sin_x = 0
x = ((x*3.1415926535898)/180)
v1 = sin(x)


DO i = 1, n

n_1 = (2*i) - 1

n_factorial = n_factorial * n_1

sum_1 = (((-1)**(i-1)) * ((x**((2*i) - 1))/(n_factorial)))

sin_x = sin_x + sum_1

END DO

write (*,*) sin_x
write (*,*) ' '
write (*,*) v1
end program series


This is my first time posting on this site, and posting in general, I would infinitely appreciate the help in figuring out what's wrong!
 
Technology news on Phys.org
You didn't mention how truncated the series is. What value are you inputting for n? If the series is significantly truncated, then the error will get larger as the angle gets larger.
 
Hi Matt, the problem is with your factorial calculation. You're only multiplying the odd numbers so you're not getting the full factorial.

You could fix it by modifying the line "n_factorial = n_factorial * n_1" to something like,
n_factorial = n_factorial * n_1 * (n_1-1)

Unfortunately however that will fail on the first pass when n_1 is 1, so you'll need something like

if (i>1) n_factorial = n_factorial * n_1 * (n_1-1)
 
Thank you Uart! I suspected that might have been the problem, but I was entirely blanking on how to correct it. In my textbook they only had one example of a factorial function calculator, but I was stumped on how to transpose that code into one which handles the cases where 'i' is multiplied by a number, or when a number is subtracted from it.

And to Rcgldr, originally my idea was to allow the user to input various integer values for n, but I think now I'll add a condition something like, IF (abs(sin(x)-sin_x) < 1E-6) EXIT. I think it should run until the condition is satisfied...
 
mattmac.nuke said:
I was stumped on how to transpose that code into one which handles the cases where 'i' is multiplied by a number, or when a number is subtracted from it.
An alternative would be to update n_factorial twice, once before and once after it's used:

n_factorial = n_factorial * n_1

sum_1 = (((-1)**(i-1)) * ((x**((2*i) - 1))/(n_factorial)))

sin_x = sin_x + sum_1

n_factorial = n_factorial * (n_1 +1 )

And to Rcgldr, originally my idea was to allow the user to input various integer values for n.
You're currrent method is fine, I was just wondering what values you were trying for n. Sorry I missed the n_factorial issue, it was late and I wasn't paying attention to the rest of the code.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 26 ·
Replies
26
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K