Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

用fortran證明Maclaurin Series是對的

以sin(x)為例

sin(x)=x-x**3/3!+x**5/5!......

寫一程式可以算出上面的級數,並與內建函數做比較

條件 : 不可用副程式及自定函數

要用if與do的組合來完成

謝謝

Update:

go to 1 and go to 5

呃.......沒教過耶~~ 冏

Update 2:

我懂了

可是

問題是

老師沒教過的東西不能用啊(哭哭)

Update 3:

對了

我記得"迴圈"不是都有什麼"do counter"的

你的程式沒有耶~

(老師要求用do)

Update 4:

老師說~要跑到多大的值是由自己的程式決定的

(SORRY我忘了說,誤差值<0.00001) -------> 這似乎是重點之一 (冏)

不好意思麻煩你了

1 Answer

Rating
  • Jack
    Lv 5
    1 decade ago
    Favorite Answer

    程式如下:

    program main

    real x, term, sum

    integer n

    1 write(*, "(/'x = ', $)")

    read *, x

    n = 0

    term = x

    sum = x

    5 n = n + 1

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

    sum = sum + term

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

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

    print *, 'sin(x) by calculation =', sum

    go to 1

    end

    執行結果:

    x = .07

    sin(x) by function call = 0.06994285

    sin(x) by calculation = 0.06994285

    x = .25

    sin(x) by function call = 0.24740396

    sin(x) by calculation = 0.24740395

    x = 1

    sin(x) by function call = 0.84147096

    sin(x) by calculation = 0.84147096

    x = 2

    sin(x) by function call = 0.9092974

    sin(x) by calculation = 0.9092974

    x = -.8

    sin(x) by function call = -0.7173561

    sin(x) by calculation = -0.7173561

    x = -1.5

    sin(x) by function call = -0.997495

    sin(x) by calculation = -0.99749506

    x = ^C

    2009-11-25 01:15:04 補充:

    go to nn 就是跳到標籤(label)為 nn 的那一行去執行。因此 go to 1 就是跳到:

    1 write(*, "(/'x = ', $)")

    這一行。go to 5 就是跳到:

    5 n = n + 1

    這一行。

    2009-11-25 08:49:52 補充:

    go to nn 就是跳到標籤(label)為 nn 的那一行去執行。因此 go to 1 就是跳到:

    1 write(*, "(/'x = ', $)")

    這一行。

    go to 5 就是跳到:

    5 n = n + 1

    這一行。

    2009-11-25 08:58:38 補充:

    ※ 不知何故,「補充」內容老是莫名其妙的消失掉,只好在這裡補充:

    go to nn 就是跳到標籤(label)為 nn 的那一行去執行。因此 go to 1 就是跳到:

    1 write(*, "(/'x = ', $)")

    這一行。

    go to 5 就是跳到:

    5 n = n + 1

    這一行。

    2009-11-25 19:31:13 補充:

    用 Do 的寫法如下:

    program main

    real x, term, sum

    integer n, i

    do i = 1, 2000

    write(*, "(/'x = ', $)")

    read *, x

    term = x

    sum = x

    do n = 1, 10000

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

    2009-11-25 19:31:47 補充:

    sum = sum + term

    if (abs(term) < 1e-7) then

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

    print *, 'sin(x) by calculation =', sum

    exit

    end if

    end do

    end do

    end

    這題用 Do 寫的缺點是:

    你無法事先預估 n 要由 1 跑到多大的值,只好給它一個「很大」的值,例如 10000。(這也是我先前的回答不用 Do 寫的原因)

    2009-11-25 19:35:02 補充:

    用 Do 的寫法如下:

    program main

    real x, term, sum

    integer n, i

    do i = 1, 2000

    write(*, "(/'x = ', $)")

    read *, x

    term = x

    sum = x

    do n = 1, 10000

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

    2009-11-25 19:35:39 補充:

    sum = sum + term

    if (abs(term) < 1e-7) then

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

    print *, 'sin(x) by calculation =', sum

    exit

    end if

    end do

    end do

    end

    這題用 Do 寫的缺點是:

    你無法事先預估 n 要由 1 跑到多大的值,只好給它一個「很大」的值,例如 10000。(這也是我先前的回答不用 Do 寫的原因)

    2009-11-25 22:12:38 補充:

    Do 迴圈實際上會跑幾次雖是由程式決定的,但

    do n = 1, 10000

    這行的 10000 這個值卻必須由你自己設定,它不可以小於 Do 迴圈實際上會跑的次數。因此保險起見,最好設一個「很大」的值。

    若是誤差值<0.00001,只要將程式的這一行

    if (abs(term) < 1e-7) then

    改成

    if (abs(term) < 1e-5) then

    就可以了。

Still have questions? Get your answers by asking now.