テイラー展開のプログラミング
注目度
3
閲覧数
3778
未解決
今すぐに!
このエントリーをはてなブックマークに追加
kojikoji
(0pt)
下のような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
oshinobi
(36pt)
\sin x および \log x  は、下のように Taylor 展開できますよね:

\sin x = x - \frac{1}{3!}x^3 + \frac{1}{5!}x^5 - \frac{1}{7!}x^7 + \cdots
\log x = (x-1) - \frac{1}{2}(x-1)^2 + \frac{1}{3}(x-1)^3 - \frac{1}{4}(x-1)^4 + \cdots

 \log x は、 x=1 において Taylor 展開すると上記のきれいな式になるので、x-1=y とおいているのでしょう。

さて、上記の式から考えて、sine(x) , logthm(x) は概略次のようになると思います。
(出力系は省いていますので、適宜挿入してください)

subroutine sine(x)
c begin
(略)
c
EPS = 1.0e-6
n = 1
Tn = x
ans = Tn
60 if( abs(Tn) .gt. EPS) then
n = n + 2
Tn = Tn * (-1) * x * x / ( (n-1) * n )
ans = ans + Tn
go to 60
end if
(後略)


subroutine logthm(x)
c begin
(略)
real y
y = x - 1
EPS = 1.0e-6
n = 1
Tn = y
ans = Tn
70 if( abs(Tn) .gt. EPS) then
n = n + 1
Tn = Tn * (-1) * y * (n-1) / n
ans = ans + Tn
go to 70
end if
(後略)


なお、sine(x) の方は、x にあまり大きな数が入ってくると、計算がオーバーフローしたりなかなか
収束しなかったりしますので、 logthm(x) の前でやっているように入力する値の範囲を抑えたりする
などの工夫が必要かも知れません。

その他、細かな改善点は残っているかと思いますが、とりあえずこんなところでどうでしょう。


2拍手 |
2010-09-04 12:29:05
一言投稿 (Q&Aに関して、思ったことなどをつぶやいてみよう!)
一言投稿はまだありません

そのままでしばらくお待ちください


しばらくたっても変わらない場合はキャンセルしてください

キャンセル

以下の内容で回答を投稿します

よろしいですか?

回答内容

回答の投稿が完了しました

こちらからご確認ください