Truncated Sin Series in FORTRAN

In summary, the user is trying to calculate the truncated value of the sine function, but is having difficulty doing so. They are using a program that begins its series expansion at value 1, but the error gets larger as the angle gets larger.
  • #1
mattmac.nuke
22
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
  • #2
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.
 
  • #3
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)
 
  • #4
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...
 
  • #5
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.
 

1. What is a truncated sin series in FORTRAN?

A truncated sin series in FORTRAN is a mathematical series that approximates the value of the sine function by using a finite number of terms. It is commonly used in scientific and engineering applications to simplify complex calculations.

2. How is a truncated sin series calculated in FORTRAN?

In FORTRAN, a truncated sin series can be calculated using a loop to add up the terms of the series. Each term is calculated using the sine function and its corresponding coefficients, which are determined by the number of terms desired in the series.

3. What are the advantages of using a truncated sin series in FORTRAN?

One advantage of using a truncated sin series in FORTRAN is that it can provide a more accurate approximation of the sine function compared to simpler methods. It also allows for more precise calculations and can be easily adapted for different applications.

4. Are there any limitations to using a truncated sin series in FORTRAN?

Yes, there are limitations to using a truncated sin series in FORTRAN. The accuracy of the approximation depends on the number of terms used in the series, so it may not be suitable for all calculations. Additionally, it may not be the most efficient method for certain applications.

5. How can I improve the accuracy of a truncated sin series in FORTRAN?

To improve the accuracy of a truncated sin series in FORTRAN, you can increase the number of terms used in the series. However, this may also increase the computation time. Alternatively, you can use more advanced techniques such as Taylor series or Chebyshev series to achieve a more precise approximation.

Similar threads

  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
605
  • Programming and Computer Science
Replies
12
Views
960
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
2
Replies
53
Views
3K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top