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

削除された内容 追加された内容
Ef3 (トーク | 投稿記録)
→‎2分法: Schemeでも実行結果を表示するようにした。
タグ: 2017年版ソースエディター
Ef3 (トーク | 投稿記録)
→‎2分法: Pythonも再帰化。受入検査と再帰を分離。受入検査に cllable(func) と left <= right を追加。ラムダ式の評価回数を最小化。
タグ: 2017年版ソースエディター
81 行
 
;[[Python]]による[https://paiza.io/projects/mslsT2vksLfwnt8HqWmn-A?language=python3 コード例]:<syntaxhighlight lang="python">
def bisection(func, left: float, right: float) -> float:
# Acceptance inspection
assert (callable(func)),"The func is not callable."
assert (type(left) == float),"The left side is not a real number."
assert (type(right) == float),"The right side is not a real number."
mid =assert (left +<= right)/2,"The left is bigger than the right."
while not (abs(func(mid)) < +1.0e-10):
# Implementation of core algorithms
mid = (right + left)/2
def core(f, low: float, ifhigh: (func(midfloat) <-> 0)float:
x = (low + lefthigh) =/ mid2
eliffx = f(func(mid) > 0 x):
if (abs(fx) < right = mid+1.0e-10):
return x
#That could hardly be true ... .
if fx < 0.0:
low = x
else:
returnhigh mid= x
return midcore(f, low, high)
return core(func, left, right)
 
print(bisection(lambda x: x-1, 0.0, 3.0))
100 ⟶ 106行目:
</syntaxhighlight>
;実行結果:<syntaxhighlight lang=text>
0.9999999999417923
1.0000000000291038
</syntaxhighlight>
123 ⟶ 129行目:
1.0000000000291038
</syntaxhighlight>
:このコードも<math>\lambda(x)=x-1</math>、または、<math>\lambda(x)=x^2-1</math>のときに試された。[[Python]]版と同じ結果であと一致している。
 
==== 台形公式====