Help with Fortran: Calculate Cos(x) from Equation

In summary: This means that in your current code, you are skipping over numbers and not calculating the correct value of the factorial, which leads to incorrect answers for certain degrees. To fix this, you can change your loop to count by 1 and use a larger data type, like real*8, to store larger factorial values.
  • #1
NicolasPan
21
2
Hello I was assigned the following problem:Make a Fortran program which will be able to read a degree[0-360] checking validity range(not type) and it will be able to calculate and print the cos(x) from the following equation:
cos(x)=1-x^2/2! + x^4/4!-x^6/6!+x^8/8!-...,where x is in radiants.As a convergence criteria assume 10^(-5) using the absolute error between two successive repeats(I suppose it means do's).For the calculation of the ! the greatest possible kind of integer should be used.Finally the total number of repeats should be printed on screen.I have been trying really hard to make it run properly but I still have some questions,which I would be greatful would you be able to answer them.1)How can I measure the diffrence of two succesive repeats?
2)The command iostat will work only for the read it corresponds?
Fortran:
program askhsh6_ergasia2
implicit none
integer(kind=4)::i,fact,fact2
real,parameter::pi=3.14159265
real::degree,radiants,cosradiants,s
print*,'This program reads and calculates an angle`s co-sinus'
print*,'Please input the degrees of the angle'
read*,degree
do while(degree<0 .or. degree>360)
read*,degree
  print*,'Error input degree'
  cycle
  end do
  radiants=(degree*pi/180)
  fact=1
  fact2=1
  s=0
do i=2,100,2
      fact=fact*i
      fact2=fact2*(i+2)
      cosradiants=(-(radiants)**i/fact)+(((radiants)**(i+2))/fact2)
  s=s+cosradiants

end do
  print*,s+1.

end program
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Your factorials are jumping over some numbers. Also, these numbers will get fairly large, so you may have some numerical problems.

For convergence, you should probably test if cosradiants lt 10^-5 and break out of the loop if true.
 
  • #3
NicolasPan said:
1)How can I measure the diffrence of two succesive repeats?
Fortran:
cosradiants = 0
do i=2,100,2
      fact=fact*i
      fact2=fact2*(i+2)
      oldcosradiants = cosradiants
      cosradiants=(-(radiants)**i/fact)+(((radiants)**(i+2))/fact2)
      difference = cosradiants - oldcosradiants
      s=s+cosradiants
end do
2)The command iostat will work only for the read it corresponds?
You mean you want to do something like this:
Fortran:
READ(*,*,IOSTAT=io) x
Then the value of io corresponds to x, each read will generate a new IOSTAT for that read that you will have to give to some variable.

Hope this helps.edit: of course you need to check your original code, as Khashishi mentioned, your factorials are wrong.
 
Last edited by a moderator:
  • #4
No, you don't need to calculate the difference in cosradiants. cosradiants is already the difference between pairwise terms in the series, since you are adding them all together into s.
 
  • #5
A four-byte Fortran REAL data type has a max. magnitude of about 10-38 to 1038. This means that only about 33! can be represented in single-precision REAL data types. If you want to go higher than 33!, you'll need to use a DOUBLE PRECISION data type.
 
  • #6
Hello everyone ! Thank you very much for your help I really appreciate that! Could you please explain why my factorials are wrong? It's strange since I get correct answers only for some degrees e.x 45 and wrong for many others such as 90 degrees.Thanks in advance
 
  • #7
You are skipping many numbers since your loop counts by 2. Also, you are using integer*4 which is much too small to store factorials larger than 12! or so. You should probably use real*8.
 
  • #8
NicolasPan said:
Hello everyone ! Thank you very much for your help I really appreciate that! Could you please explain why my factorials are wrong? It's strange since I get correct answers only for some degrees e.x 45 and wrong for many others such as 90 degrees.Thanks in advance
Even though you're using 2!, 4!, 6!, 8!, etc. 8! is still 1×2×3×4×5×6×7×8, so you can't skip over numbers in calculating the correct value of the factorial.

8! ≠ 6! × 8

8! = 6! × 7 × 8
 

1. How do I use Fortran to calculate the cosine of a given value?

To calculate the cosine of a value in Fortran, you can use the intrinsic function "cos(x)" where x is the given value. This function takes the input value in radians and returns the cosine value as a double precision number.

2. Can Fortran calculate the cosine of an angle in degrees?

Yes, Fortran can calculate the cosine of an angle in degrees by converting the input value from degrees to radians using the intrinsic function "rad(x)", where x is the input angle in degrees. Then, you can use the "cos(x)" function to calculate the cosine value.

3. How do I import the necessary libraries for Fortran to perform cosine calculations?

Fortran has built-in support for mathematical functions like cosine, so you do not need to import any external libraries. However, you can include the "use, intrinsic :: iso_fortran_env" statement at the beginning of your code to ensure compatibility with different Fortran compilers.

4. Can I calculate the cosine of multiple values at once in Fortran?

Yes, you can use arrays in Fortran to calculate the cosine of multiple values at once. Simply declare an array to store the input values, use a loop to iterate through the array and calculate the cosine value for each element using the "cos(x)" function, and store the results in another array.

5. How do I handle errors in Fortran when calculating the cosine of a value?

In Fortran, you can use the "error stop" statement to handle errors when calculating the cosine of a value. This statement will stop the program and display an error message if there is an issue with the input value or the calculation process. You can also use the "if" statement to check for specific conditions and handle errors accordingly.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top