2019年1月19日 星期六

例題2-6 已知方程式 e^x + x^-2 + 2 cosx -6 利用正割法 找出f(x)=0的根

例題2-6
已知方程式 e^x + x^-2 + 2 cosx -6
利用正割法(secant method) 採用誤差0.001 找出f(x)=0的根



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

def f_cal(x):
    # we are taking equation as e^x + x^-2 + 2 cosx -6
    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))
            print("(x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1)) = ",end='')
            print( round(x0,6))
            # 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))
            print("(x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1)) = ",end='')
            print( round(xm,6))
            print("")
    # 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)


==========先用二分法測試 根的區間===============
from math import *

#define   f(x) = (pow(x,3)-4.0*x+2.0)
#====================================
def f_cal(x) :
    temp= exp(x)+1/pow(x,2) + 2*cos(x) - 6.0
    #print(temp)
    return temp

a=0.1
b=2.1
x=a
i=1
print(" i     x          f(x)\n")

while x<=b:
    s = "{%5d} {%8.2f} {%2.5f}" %(i,x,f_cal(x))
    print(s)
    i+=1
    x=x+0.1

======== RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX2-6-0.py ===========
 i     x          f(x)

{    1} {    0.10} {97.09518}
{    2} {    0.20} {22.18154}
{    3} {    0.30} {8.37164}
{    4} {    0.40} {3.58395}
{    5} {    0.50} {1.40389}
{    6} {    0.60} {0.25057}
{    7} {    0.70} {-0.41575}
{    8} {    0.80} {-0.81855}
{    9} {    0.90} {-1.06261}
{   10} {    1.00} {-1.20111}
{   11} {    1.10} {-1.26220}
{   12} {    1.20} {-1.26072}
{   13} {    1.30} {-1.20399}
{   14} {    1.40} {-1.09466}
{   15} {    1.50} {-0.93239}
{   16} {    1.60} {-0.71474}
{   17} {    1.70} {-0.43772}
{   18} {    1.80} {-0.09611}
{   19} {    1.90} {0.31632}
{   20} {    2.00} {0.80676}
>>> 


0.6- 0.7 與 1.8 - 1.9 方程式有根

輸出結果

========= RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX2-6.py ============
(x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1)) = 1.824439
(x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1)) = 1.824979

(x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1)) = 1.824979
(x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1)) = 1.824977

Root of the given equation= { 1.82498}
No. of iterations = {    3}
>>> 

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


x1=0.6
x2=0.8
E = 0.0001

secant(x1, x2, E)

======== RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX2-6.py ============
(x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1)) = 0.624415
(x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1)) = 0.632109

(x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1)) = 0.632109
(x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1)) = 0.631822

(x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1)) = 0.631822
(x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1)) = 0.631816

Root of the given equation= { 0.63182}
No. of iterations = {    4}

>>> 

沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...