[Python] ระเบียบวิธีหาค่ารากเดี่ยวของสมการไม่เชิงเส้น


Solutions of a single non-linear equations
  1. The interval enclose the require solutions
  2. Methods
    1. Bisection method / Half-interval method
    2. Regular falsi / False position method
      • Improved refular falsi
    3. Secant method / Chord method
    4. Newton raphson method / Newton's method
    5. Iteration method / Fixed point iterative method 
  3. Sufficient condition for convergence of iteration
  4. Order and speed of convergence

def f(x):
    return x**4 - 9*(x**3) - 2*(x**2) + 120*x - 130

def f1(x):
    return 4*(x**3) - 27*(x**2) - 4*x + 120

def fine(x):
    return (((((130/x)-120)/x)+2)/x)+9

def printTable(a,b,c):
    rootRanges = []
    x_prev = 0
    y_prev = 0
    x_now = 0
    y_now = 0
    print("x\tf(x)")
    for i in range(a,b,c):
            print(i,"\t",f(i))
            if i == a :
                    x_prev = a
                    y_prev = f(a)
            else :
                    x_now = i
                    y_now = f(i)
                    if y_prev*y_now < 0 : rootRanges.append((x_prev,x_now))
                    x_prev = i
                    y_prev = f(i)
    print("The root might be in : ",rootRanges)

def bisection(a, b, e, n=100):
    for i in range(n):
        x = (a+b)/2
        if abs(f(x)) < e :
            return x
        if abs(x - a) < e :
            return x
        if f(x)*f(a) > 0 :
            a = x
        else :
            b = x
    return None

def regularFalsi(a, b, e, n=100):
    for i in range(n):
        x = (a*f(b)-b*f(a))/(f(b)-f(a))
        if abs(f(x)) < e :
            return x
        if abs(x - a) < e :
            return x
        if abs(x - b) < e :
            return x
        if f(x)*f(a) > 0 :
            a = x
        else :
            b = x
    return None

def regularFalsiImproved(a, b, e, n=100):
    for i in range(n):
        if i == 0:
            x = (a*f(b)-b*f(a))/(f(b)-f(a))
        if abs(f(x)) < e :
            return x
        if abs(x - a) < e :
            return x
        if abs(x - b) < e :
            return x
        if f(x)*f(a) > 0 :
            a = x
            x = (a*f(b)-(b*f(a)/2))/(f(b)-(f(a)/2))
        else :
            b = x
            x = ((a*f(b)/2)-b*f(a))/(f(b)/2-f(a))
    return None

def secant(x0, x1, e, d, n=100):
    f0 = f(x0)
    f1 = f(x1)
    for i in range(n):
        if abs(f1 - f0) < d :
            print("Slope too small")
            return None
        x2 = x1 - (((x1 - x0)/(f1-f0))*f1)
        f2 = f(x2)
        if abs(f2) < e :
            return x2
        f0 = f1
        f1 = f2
        x0 = x1
        x1 = x2
    print('Does not convergence')
    return None

def newtonRalphson(a, m, e, n=100):
    for i in range(n):
        x = a - (f(a) / f1(a))
        if f(x) < e :
            return x
        if abs(x - a) < e :
            return x
        if n == m:
            print('Does not convergence')
            return None
        else: 
            a = x
    return None

def fixedPointIterative(x0, e, n=100):
    x1 = fine(x0)
    for i in range(n):
        x0 = x1
        x1 = fine(x0)
        if abs(x1 - x0) < e :
            return x1
    return None
    
printTable(-10,11,1)
print('Bisection \t\t= ',bisection(6,9,0.001))
print('Regular Falsi \t\t= ',regularFalsi(6,9,0.001))
print('Regular Falsi Improved \t= ',regularFalsiImproved(6,9,0.001))
print('Secant \t\t\t= ',secant(6,9, 0.001, 0.001))
print('Newton Ralphson \t= ',newtonRalphson(9,50,0.001))
print('Fixed Point Iterative \t= ',fixedPointIterative(6, 0.001))

Previous
Next Post »