Commit 81a24dbc authored by Kittipong Maneewong's avatar Kittipong Maneewong

ส่งงาน week12

parent 2f8ed6b1
...@@ -27,13 +27,17 @@ def bracket(f, **kwargs): ...@@ -27,13 +27,17 @@ def bracket(f, **kwargs):
xi -= h xi -= h
print('Solution xi',xi,f(xi)) print('Solution xi',xi,f(xi))
bracket(f,a=-2,b=2,h=0.01) bracket(f,a=-2,b=2,h=0.01)
#Output Solution xi 1.0000000000000022 0.0
#Golden Section Search #Golden Section Search
import math import math
from math import log as ln
R = (-1 + 5**0.5)/2 R = (-1 + 5**0.5)/2
def n(a=-2,b=2,ebs=10**-9) : eps=10**-9
def n(a=-2,b=2,eps=10**-9) :
from math import log as ln from math import log as ln
return int(-ln(R)*ln(eps/abs(b-a))) return int(ceil(-2.078087)*ln(eps/abs(b-a)))
def search(f,a,b,tol=1.0e-9): def search(f,a,b,tol=1.0e-9):
nIter = n(a,b) nIter = n(a,b)
0.618033989 0.618033989
...@@ -55,3 +59,4 @@ def search(f,a,b,tol=1.0e-9): ...@@ -55,3 +59,4 @@ def search(f,a,b,tol=1.0e-9):
if f1 < f2: return x1,f1 if f1 < f2: return x1,f1
return x2,f2 return x2,f2
search(f,-2,2) search(f,-2,2)
#Output (0.9999999940181203, 0.0)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment