# FORTRAN 計算COSH(X) 如何寫 急件

http://images.plurk.com/5676515_9c9a9a36eb788e0792...

Write a FORTRAN program to calculate the hyperbolic cosine of any angle x

using the following form:

   

Finish the calculation when any term of the above equation becomes less than

1.0E-7.

program ooooooooooooo

real x,xx,current,accum,final,ax

integer n

write(*,*)'input any x,cal cosh(X)'

xx=x*x

final=1.0

n=1

accum=1.0

current=1.0

final=1.0

ax=1.0

2 個已更新項目:

1 if(abs(current) .gT. (1.0E-7))then

AX=AX*XX

accum=accum*(2*n)*(2*n-1)

current=ax/accum

final=fianl+current

endif

write(*,*)'fianl=',final

stop

end

3 個已更新項目:

4 個已更新項目:

### 1 個解答

• Jack
Lv 5
1 0 年前
最佳解答

程式如下： program main

real n, x, term, sum

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

n = 0.

term = 1.

sum = 1.

do

term = term * x * x / ((n+1.) * (n+2.))

sum = sum + term

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

n = n+2.

end do

write (*, *) 'cosh(x) by calculation = ', sum

write (*, *) 'cosh(x) by function call =', cosh(x)

go to 1

end執行結果：x = 0

cosh(x) by calculation = 1.

cosh(x) by function call = 1.x = 1

cosh(x) by calculation = 1.5430806

cosh(x) by function call = 1.5430807x = 2

cosh(x) by calculation = 3.762196

cosh(x) by function call = 3.7621956x = 3

cosh(x) by calculation = 10.067662

cosh(x) by function call = 10.067662x = -2.5

cosh(x) by calculation = 6.132289

cosh(x) by function call = 6.1322894x = -10

cosh(x) by calculation = 11013.233

cosh(x) by function call = 11013.233x = ^C

2010-11-06 00:21:21 補充：

你的程式改成這樣就行了：

program ooooooooooooo

real x, xx, current, accum, final, ax

integer n

write(*,*)'input any x, cal cosh(x)'

xx=x*x

final=1.0

n=1

accum=1.0

current=1.0

ax=1.0

2010-11-06 00:21:35 補充：

do while (abs(current) .gt. 1.0E-7)

AX=AX*XX

accum=accum*n*(n+1)

current=ax/accum

final=final+current

n=n+2

end do

write(*,*) 'final=', final

stop

end

2010-11-06 02:18:19 補充：

我跑出來都是正確的。

請再檢查看看有沒有什麼地方沒改到（比方說，你有個 final 打成 fianl 了，諸如此類的）。