寫fortran程式計算

求cosx=1-x^2/2!+x^4/4!+x^6/6!+...之值,x=2.0 (誤差須在10^-6之內)

Update:

看這題cosx=1-x^2/2!+x^4/4!-x^6/6!+...之值,x=2.0

1 Answer

Rating
  • Jack
    Lv 5
    9 years ago
    Favorite Answer

    程式如下: program main

    real x

    x = 2.0

    print *, 'cos(x) by function call =', cos(x)

    print *, 'cos(x) by calculation =', cos_cal(x)

    end

    !

    real function cos_cal(x)

    real x, term, sum

    integer n

    n = 0

    term = 1.

    sum = 1.

    1 n = n + 1

    term = -term * x * x / (float(2*n-1) * float(2*n))

    sum = sum + term

    if (abs(term) >= 1e-7) go to 1

    cos_cal = sum

    return

    end執行結果: cos(x) by function call = -0.41614684

    cos(x) by calculation = -0.4161468

Still have questions? Get your answers by asking now.