How to Calculate the Taylor Series Approximation for the Cosine Function


Taylor Expansion for the Cosine Function upto the first five terms. Notice that the terms have even powers as cos itself is odd.
In the previous post, we learned how to calculate the sine of a function using the Taylor approximation. Now, let's tweak this program a little bit to generate an approximation for the cosine function. Let us revise how to construct a program for Taylor Series.
  •  We change the degrees input to radians, just as we did in the previous program.
  • We set an initial value of 1 to the sum of the series and define the first term, t= 1.
  • We calculate the remaining terms to include new powers (angle**2) and factorials (i*(i-1)) with each term and add a (-) sign to the term so that the signs alter between '+ ' and '-' with each term.
  • We display the result and compare it with the value given by the intrinsic function.
Let us now see the difference between the program for the sine function and the cos function.
  • The first term in this program is 1 instead of angle in the previous one. Similarly, the initial sum1 value is 1 instead of angle.
  • The do loop for the Taylor series begins with 2 instead of 3. This is because cosine is an even function.
  • The rest of the program is the same for both sine and cosine functions.
Now for the FORTRAN program, 

!To find the cosine value using Taylor expansion
program Taylor_cos
    implicit none
    real x,t,angle,sum1,sum2,cont
    integer np,n,i
   
    !To read the angle in degrees
    30 write (*,*) "Give the number whose cos value you wish to compute (in degrees)"
    read (*,*) x
    write(*,*) "Calculating the cosine of ",x
   
    !To convert angle to to Radians
    angle=0.0174532925*x
    write (*,*) "The angle in Radians is= ",angle

    !To include n terms
    write (*,*) "Give the value of n (the no. of terms in the Taylor series)"
    read (*,*) n
    write (*,*) "The given value of n (the no. of terms in the Taylor series) is ",n

    !To sum up Taylor series
    t=1
    sum1=1
    np=(2*(n-1))+1
    do i=2,np,2
        t=(((-t)*(angle**2))/(i*(i-1)))
        sum1=sum1+t
    end do
    write (*,10)  "By Taylor series, cos(",x,")= ",sum1
    10 format (a,f7.3,a,f10.5)

    !To calculate using the intrinsic function
    sum2=cos(angle)
    write(*,20) "By using the intrinsic function, cos(",x,")= ",sum2
    20 format (a,f7.3,a,f10.5)
    if ((abs(sum1-sum2))<0.00001) then
        write(*,*) "The result matches the value given by the intrinsic function"
        else
        write(*,*)"The value found does not match the value given by the intrinsic function"
    end if

    !To read more input angles: Optional
    write(*,*) "Press 1 to continue, press 2 to exit"
    read (*,*) cont
    if (cont==1) then
      go to 30
      else
      stop
    end if
     
end program Taylor_cos
     


Sample Output for Various Angles in Degrees
Some more changes to make it a better program:
  • In this program, we have added format statements to the results.This helps because the we do not need accuracy beyond a certain limit. For example, cos(90)=0 . We do not wish to complicate results by displaying something like 0.4034x10^(-9) for normal calculations.
  • Also, you can set a default value for n=10 or larger. The more the number of terms used, the closer the answer to that given by the intrinsic function. We have kept the choice of n flexible to be read at the time of execution.
    Notice how results get better by increasing the value of n

4 comments:

  1. please can u write the fortran code for taylor series approximation using a function

    ReplyDelete
  2. Search the internet for two of the two equivalent queries from the following functions:
    Sin 
    Cosh 
    Exp 
    Ln 
    Tan 
    2. Then find the series resulting from the sum of the two
    3. Then write a program to solve this series using conditional rotors
    4. Then write a program to limit this series using unconditional rotors


    Pleas solve this my exam

    ReplyDelete
  3. How to calculate sum of the Taylor series of sec inverse of x by fortran programming

    ReplyDelete