下のようなe^x,sinx,logxのテーラー展開のプログラミングをFORTRUNで書く課題が出ました。
program EplTyl
c
real x
integer which
c
c begin
c EPS = 1.0e-5
write(6,*) 'Taylor Expansion of mathematical function'
write(6,*) 'Which function do you select ?'
write(6,*) ' 1. Exponential function'
write(6,*) ' 2. Sine function'
write(6,*) ' 3. Logarithmic function'
write(6,*) ' ===> '
read (5,*) which
if((which .lt. 1) .or. (which .gt. 3)) then
write(6,*) ' Number must be 1,2 or 3 !!'
else
write(6,*) 'Enter real number : '
read (5,*) x
if(which .eq. 1) call expotn(x)
if(which .eq. 2) call sine(x)
if(which .eq. 3) then
100 if((x .le. 0) .or. (x .ge. 2)) then
write(6,*) 'Enter x again (0<x<2) : '
read (5,*) x
go to 100
end if
call logthm(x)
end if
end if
end
c
subroutine expotn (x)
c
real Tn, ans, err
integer n
c
EPS = 1.0e-6
n = 0
Tn = 1.0
ans = Tn
write(6,10) x
10 format('EXP(', f10.5, ') ')
50 if( abs(Tn) .gt. EPS) then
n = n + 1
Tn = Tn * x / n
ans = ans + Tn
err = ans - exp(x)
write(6,11) n, ans, err
11 format(I3,' exp(X) = ',f12.7,' err = ',f15.10)
go to 50
end if
write(6,*) ' Exp( ',x,' )= ',ans
return
end
c
46
subroutine sine (x)
c begin
c { implement yourself } 説明:プログラムをこの部分へ追加する.
return
end
c
subroutine logthm(x)
real y
c begin
y = x - 1
c { implement yourself }
return
end
これで、sinxとlogxのテーラー展開をそれぞれ書きこむのですが・・・値がうまく行きません。
私の間違っているプログラムを書きますので、間違いと訂正のほうをどうかよろしくお願いします。
subroutine sine (x)
c begin
real Tn, ans, err
integer n,nstop
c
EPS = 1.0e-6
n = 0
Tn = 1.0
nSTOP=50
ans = Tn
write(6,20) x
20 format('EXP(', f10.5, ') ')
60 if( abs(Tn) .gt. EPS) then
n = n + 1
Tn = Tn * -x^2 /(2*n + 1)
ans = ans + Tn
err = ans - sin(x)
write(6,11) n, ans, err
11 format(I3,' exp(X) = ',f12.7,' err = ',f15.10)
go to 60
end if
write(6,*) ' Exp( ',x,' )= ',ans
return
end
subroutine logthm(x)
real y
c begin
y = x - 1
EPS = 1.0e-6
n = 0
Tn = 1.0
ans = Tn
write(6,30) x
30 format('EXP(', f10.5, ') ')
70 if( abs(Tn) .gt. EPS. ANS. LD. n) then
n = n + 1
Tn = Tn * -(n-1)*y/n
ans = ans + Tn
err = ans - log(x)
write(6,11) n, ans, err
11 format(I3,' exp(X) = ',f12.7,' err = ',f15.10)
go to 70
end if
write(6,*) ' Exp( ',x,' )= ',ans
return
end
あと、logxだけyが設けられていてかなり混乱しています。
どうか、このプログラムを教えてください。
2010-06-16 17:05:23