「旧課程(-2012年度)高等学校数学B/数値計算とコンピューター」の版間の差分
削除された内容 追加された内容
M編集の要約なし |
<syntaxhighlight> |
||
27 行
[[Python]]によるプログラム例
<syntaxhighlight lang="python">
print euclid()
#ok that work well if I put
#(left ,right)= (45 ,30)
#(45 ,28),(30 ,28)
</syntaxhighlight>
[[Scheme]]によるプログラム例
<syntaxhighlight lang="Scheme">
(define (euclid m n)
(let ((r (modulo m n)))
(if (zero? r) ;ここまでが導出過程の1
n ;ここが導出過程の3
(euclid n r)))) ;ここが導出過程の2
;;;実行例
;;> (euclid 45 30)
;;> (euclid 45 </syntaxhighlight>
===実数の算法===
82 ⟶ 83行目:
[[Python]]によるプログラム例
<syntaxhighlight lang="python">
def bisection():
#Could be a too simple function. But seems ok,
#it just is an example.
def func(x):
return x**2-1
#2 magic variables
left = 0.0
right = 3.0
assert (type(left) == float
and type(right) == float),"That is not real numbers"
mid = (left +right)/2
while not (-1.0e-10 <func(mid) < -1.0e-10):
mid = (right + left)/2
if (func(mid) < 0):
left = mid
elif (func(mid) > 0 ):
right = mid
#That could hardly be true ... .
else:
return mid
return mid
#ok that work well if I put
#func (x):
# x-1 or x**2 - 1
#The result of both calculation was 1.0.
</syntaxhighlight>
このコードはf(x)=x-1、または、f(x)=<math>x^2-1</math>のときに試された。結果はどちらもx=1.0となり、正しい値を返している。
[[Scheme]]によるコード例
<syntaxhighlight lang="Scheme">
(define (bisection f a b) ;手順1。
(let ((e (expt 10 -10))
(mid_point (/ (+ a b) 2))) ;手順2。中点の計算。
(cond ((or (zero? (f mid_point))
(< (- e) (f mid_point) e))
(exact->inexact mid_point)) ;ここまでが手順4。
((> (f mid_point) 0)
(bisection f a mid_point))
(else (bisection f mid_point b))))) ;ここまでが手順3
;;;実行例
;;>
;;0.9999999999417923
;;> (bisection (lambda (x) (- (expt x 2) 1)) 0 3) ;x^2-1の解を0〜3間で探す。
;;1.0000000000291038
</syntaxhighlight>
このコードもλ(x)=x-1、または、λ(x)=<math>x^2-1</math>のときに試された。[[Python]]版と違うが、結果はどちらもx=1.0に極めて近い値を返している。計算精度の違い、である。
141 ⟶ 140行目:
ここで、台形の面積<math>s _i</math>は
:<math>
s _i = \frac12 \{ f(x _i)+f(x _{i+1}) \} \cdot (x _{i+1}-x _i )
</math>
で書かれることを考慮すると、求める面積Sは、
153 ⟶ 152行目:
<!-- 実際は、4分の1円の面積を求め、それとpi/4の数値の比較を行っているだけ、だと思う。 -->
<!-- 後者のSchemeコードはPython版に準じましたが、解説修正した方が良いですね。 -->
<syntaxhighlight lang="python">
from math import sqrt,pi
def trapezoid_formula():
def func(x):
return sqrt(1-x**2)
sum = 0.0
a = 0.0
b = 1.0
assert (type (sum) ==float and
type (a) ==float and
type (b) ==float), "not a real number"
N = 20
dx = (b-a)/N
for i in range(N):
#Section of trapezoid ... .
sum += (func(a+dx * i)+ func(a+dx*(i+1)) ) * dx /2
return sum
print trapezoid_formula()
print pi/4
#ok that work well
#the result reads
#0.782116219939 for trapezoid_formula()
#0.785398163397 for pi/4
</syntaxhighlight>
実際の<math>\pi</math>の値と近い値が得られていることが分かる。
[[Scheme]]によるプログラム例
<syntaxhighlight lang="Scheme">
(define (trapezoid_formula f a b)
(let ((n 20))
(let ((dx (/ (- b a) n)))
(let loop ((i 0) (sum 0))
(if (= i n)
(exact->inexact sum)
(loop (+ i 1)
(+ sum (* (+ (f (+ a (* dx i)))
(f (+ a (* dx (+ i 1)))))
(/ dx 2)))))))))
;;;実行例
;;> (trapezoid_formula (lambda (x)
;;
;;
;;0.7821162199387454
;;> (/ pi 4)
;;0.7853981633974483
</syntaxhighlight>
こちらも実際の<math>\pi</math>の値と近い値が得られていることが分かる。
|