2018年12月29日 星期六

利用正割理則 (Scant Method) 解 x^3 + x - 1 非線性方程式解 Python語言

利用正割理則 (Scant Method) 解 x3 + x - 1 非線性方程式解 Python語言
Input : equation = x3 + x - 1 
        x1 = 0, x2 = 1, E = 0.0001
Output : Root of the given equation = 0.682326
         No. of iteration=5



# C++ Program to find root of an equations using secant method
from math import *


def f_cal(x):
    # we are taking equation as x^3+x-1
    f = pow(x,3) + x - 1.0
    return f

def secant(x1,x2,E):
    n = 0.0
    xm=0.0
    x0=0.0
    c=0.0
 
    if (f_cal(x1) * f_cal(x2) > 0):
        print( "Can not find a root in the given inteval")
    else:
        # calculate the intermediate value
        x0= (x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1))
        # check if x0 is root of equation or not
        c = f_cal(x1) * f_cal(x0)
        # update the value of interval
        x1 = x2
        x2 = x0
        # update number of iteration
        n=n+1
         
        #if x0 is the root of equation then break the loop
        if (c == 0):
            exit()
        xm = (x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1))
# repeat the loop until the convergence

        while (abs(xm - x0) >= E): #// repeat the loop
            # calculate the intermediate value
            x0= (x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1))
            # check if x0 is root of equation or not
            c = f_cal(x1) * f_cal(x0)
            # update the value of interval
            x1 = x2
            x2 = x0
            # update number of iteration
            n=n+1
         
            #if x0 is the root of equation then break the loop
            if (c == 0):
                break

            xm = (x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1))
    # repeat the loop until the convergence

 
        print("Root of the given equation= {%8.5f}" %(x0))
        print("No. of iterations = {%5d}" %(n))


#initializing the values
x1 = 0.0
x2 = 1.0
E = 0.0001
secant(x1, x2, E)



======= RESTART: H:/2018-09勤益科大數值分析/數值分析/PYTHON/EX2-7.py ==========
Root of the given equation= { 0.68233}
No. of iterations = {    5}
>>>


Input : equation = 
plot | exp(x) + 1/x^2 + 2 cos(x) - 6
        x1 = 1.8, x2 = 2.0, E = 0.0001
Output : Root of the given equation = 0.682326
         No. of iteration=5





# C++ Program to find root of an equations using secant method
from math import *


def f_cal(x):
    # we are taking equation as x^3+x-1
    f = exp(x)+1/pow(x,2) + 2*cos(x) - 6.0
    return f

def secant(x1,x2,E):
    n = 0.0
    xm=0.0
    x0=0.0
    c=0.0
   
    if (f_cal(x1) * f_cal(x2) > 0):
        print( "Can not find a root in the given inteval")
    else:
        # calculate the intermediate value
        x0= (x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1))
        # check if x0 is root of equation or not
        c = f_cal(x1) * f_cal(x0)
        # update the value of interval
        x1 = x2
        x2 = x0
        # update number of iteration
        n=n+1
           
        #if x0 is the root of equation then break the loop
        if (c == 0):
            exit()
        xm = (x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1))
# repeat the loop until the convergence

        while (abs(xm - x0) >= E): #// repeat the loop
            # calculate the intermediate value
            x0= (x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1))
            # check if x0 is root of equation or not
            c = f_cal(x1) * f_cal(x0)
            # update the value of interval
            x1 = x2
            x2 = x0
            # update number of iteration
            n=n+1
           
            #if x0 is the root of equation then break the loop
            if (c == 0):
                break

            xm = (x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1))
    # repeat the loop until the convergence

   
        print("Root of the given equation= {%8.5f}" %(x0))
        print("No. of iterations = {%5d}" %(n))


#initializing the values
x1 = 1.8
x2 = 2.0
E = 0.0001
secant(x1, x2, E)

沒有留言:

張貼留言

2024產專班 作業2

 2024產專班 作業2   1. 系統圖       ESP32+MFRC522 組成RFID Reader 可以將RFID卡片的UID 透過 MQTT協定    上傳(發行 主題 (:topic) alex9ufo/2024/RFID/RFID_UID  ,, Payload...