「旧課程(-2012年度)高等学校数学B/数値計算とコンピューター」の版間の差分

削除された内容 追加された内容
M編集の要約なし
Ef3 (トーク | 投稿記録)
<syntaxhighlight>
27 行
 
[[Python]]によるプログラム例
<syntaxhighlight lang="python">
def euclid():
left =45
right = 30
assert (left > 0 and right > 0),"not a positive number"
def exactly_divides_the_other():
if (left > right):
if (left % right == 0):
return True
elif (left < right):
if (right % left == 0):
return True
return False
while(not exactly_divides_the_other()):
if (left > right):
left = left % right
elif (right > left):
right = right % left
#Then we get a mcd. The smaller number is that.
if (left < right):
return left
elif (left > right):
return right
print euclid()
#ok that work well if I put
#(left ,right)= (45 ,30)
#(45 ,28),(30 ,28)
 
print euclid()
[[Scheme]]によるプログラム例
#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)
;;;実行例
;;15
;;> (euclid 45 3028)
;;151
;;> (euclid 4530 28)
;;12
</syntaxhighlight>
;;> (euclid 30 28)
;;2
 
===実数の算法===
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
 
defprint 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
print bisection()
#ok that work well if I put
#func (x):
# x-1 or x**2 - 1
#The result of both calculation was 1.0.
 
 
#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
 
;;;実行例
(define (bisection f a b) ;手順1。
;;> (bisection (letlambda ((ex) (expt- 10x -101)) 0 3) ;x-1の解を0〜3間で探す。
;;0.9999999999417923
(mid_point (/ (+ a b) 2))) ;手順2。中点の計算。
;;> (bisection (lambda (x) (- (expt x 2) 1)) 0 3) ;x^2-1の解を0〜3間で探す。
(cond ((or (zero? (f mid_point))
;;1.0000000000291038
(< (- e) (f mid_point) e))
</syntaxhighlight>
(exact->inexact mid_point)) ;ここまでが手順4。
((> (f mid_point) 0)
(bisection f a mid_point))
(else (bisection f mid_point b))))) ;ここまでが手順3
;;;実行例
;;> (bisection (lambda (x) (- x 1)) 0 3) ;x-1の解を0〜3間で探す。
;;0.9999999999417923
;;> (bisection (lambda (x) (- (expt x 2) 1)) 0 3) ;x^2-1の解を0〜3間で探す。
;;1.0000000000291038
 
このコードもλ(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版に準じましたが、解説修正した方が良いですね。 -->
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 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)))))))))
 
;;;実行例
(define (trapezoid_formula f a b)
;;> (trapezoid_formula (lambda (x)
(let ((n 20))
;; (let ((dx (/sqrt (- b1 a)(expt nx 2))))
;; (let loop ((i 0) (sum 0) 1)
;;0.7821162199387454
(if (= i n)
;;> (/ pi 4)
(exact->inexact sum)
;;0.7853981633974483
(loop (+ i 1)
</syntaxhighlight>
(+ sum (* (+ (f (+ a (* dx i)))
(f (+ a (* dx (+ i 1)))))
(/ dx 2)))))))))
;;;実行例
;;> (trapezoid_formula (lambda (x)
;; (sqrt (- 1 (expt x 2))))
;; 0 1)
;;0.7821162199387454
;;> (/ pi 4)
;;0.7853981633974483
 
こちらも実際の<math>\pi</math>の値と近い値が得られていることが分かる。