How do I fix my Fortran program for calculating sin(0.75)?

hello!

I’m trying to write a program to calculate sin(0.75) using the Taylor series in FORTRAN 90, which prints each iteration and terminates once the absolute difference between the calculated value and FORTRAN’S intrinsic sin function is less than 1E-6. However, I’m going out of my mind because I keep running into different issues.

Here’s my code:

program taylor implicit none integer :: k = 1 real :: y = 0.75 real :: x = 0.75 do while (abs(y - sin(0.75)) > 1E-6) y = x - ((y * x * x) / 2 * k * (2 * k + 1 )) k = k + 1 print *, y end do end program taylor

How can I edit this so it does what I specified above? thanks, appreciate any responses.

## Expert Answer

program taylor

implicit none

integer :: k = 1

real :: y = 0.75

real :: x = 0.75

real :: numerator = 0.75

integer :: sign = 1

integer :: factorial = 1

do while (abs(y – sin(0.75)) > 1E-6)

sign = sign * (-1)

k = k + 2

numerator = numerator * x * x

factorial = factorial * (k-1) * (k)

y = y + sign * (numerator/factorial)

print *, y

end do

end program taylor

I hope you like this code.

By the way your original code stalled my pc twice ðŸ˜›