Kök Bulma

Yazar

Taygun Bulmus

Yayınlanma Tarihi

26 Ağustos 2024

Artan Arama Metodu

import numpy as np

import matplotlib.pyplot as plt

def kok_bulma_artan_arama(fonksiyon,aralikKucuk,aralikBuyuk,adimAraligi):
    '''[Numerical Methods in Engineering with Python 3]
    '''
    x1 = aralikKucuk
    f1 = fonksiyon(aralikKucuk)
    x2 = aralikKucuk + adimAraligi
    f2 = fonksiyon(x2)
    while np.sign(f1) == np.sign(f2):
        if x1 >= aralikBuyuk: 
            return None,None
        x1 = x2
        f1 = f2
        x2 = x1 + adimAraligi
        f2 = f(x2)
    return x1,x2

# Ornek

def f(x):
    return x**3 - 10.0*x**2 + 5.0

x1 = -2.0
x2 = 11.0
dx= 1e-3
for i in range(4):
    print(kok_bulma_artan_arama(f,x1,x2,dx))
    x1,x2 = kok_bulma_artan_arama(f,x1,x2,dx)
    
x = (x1 + x2)/2.0
print('x =', '{:6.6f}'.format(x))

print('f(x1) =', '{:6.6f}'.format(f(x1)))
print('f(x2)    =', '{:6.6f}'.format(f(x2)))

xCok = np.linspace(0,1,100)

xCok= np.linspace(-5,11,100)

plt.plot(xCok,f(xCok))
plt.grid()
plt.ylim(-3,3)
plt.show()
(-0.6850000000001099, -0.6840000000001099)
(-0.6850000000001099, -0.6840000000001099)
(-0.6850000000001099, -0.6840000000001099)
(-0.6850000000001099, -0.6840000000001099)
x = -0.684500
f(x1) = -0.013669
f(x2)    = 0.001426

İkiye Bölme Yöntemi

import numpy as np

def kok_bulma_ikiye_bolme(fonksiyon, aralikKucuk, aralikBuyuk, tolerans):
    '''https://pythonnumericalmethods.berkeley.edu/notebooks/chapter19.03-Bisection-Method.html
    '''
    
    # aralikKucuk ve aralikBuyuk kök mü?
    if np.sign(fonksiyon(aralikKucuk)) == np.sign(fonksiyon(aralikBuyuk)):
        raise Exception("The scalars a and b do not bound a root")
        
    # Aralığın orta noktasını bul
    m = (aralikKucuk + aralikBuyuk)/2
    
    # Eğer orta nokta kök ise, kök orta noktadır.
    if np.abs(fonksiyon(m)) < tolerans:
        return m
    # Eğer kök aralik küçük ve orta nokta arasındaysa, aralığı küçült
    elif np.sign(fonksiyon(aralikKucuk)) == np.sign(fonksiyon(m)):
        return kok_bulma_ikiye_bolme(fonksiyon, m, aralikBuyuk, tolerans)
    # Eğer kök aralik büyük ve orta nokta arasındaysa, aralığı küçült
    elif np.sign(fonksiyon(aralikBuyuk)) == np.sign(fonksiyon(m)):
        return kok_bulma_ikiye_bolme(fonksiyon, aralikKucuk, m, tolerans)

# Ornek
def f(x):
    return x**3 - 10.0*x**2 + 5.0

a = 0.0
b = 1.0
tol = 1e-4

x = kok_bulma_ikiye_bolme(f, a, b, tol)
print('x =', '{:6.6f}'.format(x))

print('f(x-tol) =', '{:6.6f}'.format(f(x-tol)))
print('f(x) =', '{:6.6f}'.format(f(x)))
x = 0.734604
f(x-tol) = 0.001302
f(x) = -0.000005

Newton-Raphson Yöntemi

import numpy as np

def f(x): 
    return x**2 - 2

def f_prime(x):
    return 2*x
newton_raphson = 1.4 - (f(1.4))/(f_prime(1.4))

print("newton_raphson =", newton_raphson)
print("sqrt(2) =", np.sqrt(2))
newton_raphson = 1.4142857142857144
sqrt(2) = 1.4142135623730951
import numpy as np

def kok_bulma_newton_raphson(fonksiyon, turevFonksiyon, baslangicNoktasi, tolerans):
    '''https://pythonnumericalmethods.berkeley.edu/notebooks/chapter19.04-Newton-Raphson-Method.html
    '''
    # Başlangıç noktası kök mü?
    if np.abs(fonksiyon(baslangicNoktasi)) < tolerans:
        return baslangicNoktasi
    # Başlangıç noktası kök değilse, yeni bir başlangıç noktası bul
    else:
        return kok_bulma_newton_raphson(fonksiyon, 
                                        turevFonksiyon, 
                                        baslangicNoktasi - fonksiyon(baslangicNoktasi)/turevFonksiyon(baslangicNoktasi), 
                                        tolerans)

def f(x): 
    return x**2 - 2

def f_prime(x):
    return 2*x

estimate = kok_bulma_newton_raphson(f, f_prime, 1.5, 1e-6)
print("estimate =", estimate)
print("sqrt(2) =", np.sqrt(2))
estimate = 1.4142135623746899
sqrt(2) = 1.4142135623730951

Hazır paketler

import scipy as sp
import numpy as np

def f(x):
    return x**3 - 10.0*x**2 + 5.0


# https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html
#fullOutput= sp.optimize.fsolve(f, [0.0], full_output=True)
#fullOutput= sp.optimize.fsolve(f, [0.0], full_output=True, xtol=1e-19)
#fullOutput= sp.optimize.fsolve(f, [0.1], full_output=True)

#fullOutput= sp.optimize.fsolve(f, [18.0], full_output=True)
#fullOutput= sp.optimize.fsolve(f, [-1.0], full_output=True)

fullOutput= sp.optimize.fsolve(f, [-1.0, 1.0, 8.0], full_output=True)


print(type(fullOutput))

for it in fullOutput:
    print(it)
<class 'tuple'>
[-0.68409457  0.73460351  9.94949106]
{'nfev': 18, 'fjac': array([[-0.98811706, -0.15230149,  0.02071054],
       [ 0.15245084, -0.98829389,  0.00582501],
       [-0.01958095, -0.00891313, -0.99976854]]), 'r': array([-14.01078658,   1.51524046,   5.58342376,  13.95659158,
         0.76897666, -98.82622489]), 'qtf': array([ 4.09473227e-09, -1.20212711e-08,  1.68011267e-09]), 'fvec': array([ 3.24629212e-12, -6.14264195e-12,  9.09494702e-13])}
1
The solution converged.