Taylor_series_expansion_for_cosine

an anonymous user · September 16, 2020 · Fortran
real function fac(n)
    integer n, p
    
    p = 1
    do i = 1, n
        p = p * i
    end do
    
    fac = p
    end
    
program Taylor_series_expansion_for_cosine
    
    REAL x, term, result
    INTEGER n
    
    term = 1.0
    result = 1.0
    x = 0.5
    n = 1
    
    DO WHILE(ABS(term) > 0.0000001)
        term = x**(2*n) * (-1)**n / (fac(2*n))
        result = result + term
        n = n + 1
        WRITE(*,26) result 
        26 format(f9.7)
    ENDDO
    
    WRITE(*,*) "result"
    WRITE(*,31) result
    31 format(f9.7)

STOP
END

Comments

Please sign up or log in to contribute to the discussion.