2018年12月29日 星期六

例題 EX2-9 定點回路法 求非線性方程式 f(x)=1/5^x - x = 0 Python語言

 例題 EX2-9 定點回路法 求非線性方程式 f(x)=1/5^x - x = 0  Python語言



'''
 定點回路法
/* ex2-9.c is used for solving nonlinear equation
 * based on Fixed-Point Algorithm g(x)=x with initial
 * approximation P0.
 */

 例題 EX2-9 定點回路法 求非線性方程式 f(x)=1/5^x - x = 0  Python語言

    y= x
    y=1/5^x
'''
MAX=50
TOL=0.0001

def g(x):
    temp=(1/pow(5,x))+0.0
    return temp


i=1
# f(0.4)= 0.1253 , f(0.5)=-0.0527  f(0.4) * f(0.5) < 0
#所以 設定x0=0.45  在0.4與 0.5 之間有 f(x)=0的解

x0=0.45
while(i<=MAX):
    x=g(x0)
    print("{%-2d}  {%10.7f}\n" %(i-1,x0))
    if(abs(x-x0) < TOL):
        print("The Root={%10.7f}  x-x0={%10.7f}\n" %(x,abs(x-x0)))
        break
    i=i+1
    x0=x

print("Fixed-point failed after {%d} iteration.\n"%(i))


========================
輸出畫面
========================
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
======= RESTART: H:/2018-09勤益科大數值分析/數值分析/PYTHON/ex2-9-2.py ==========
{0 }  { 0.4500000}

{1 }  { 0.4846894}

{2 }  { 0.4583705}

{3 }  { 0.4782035}

{4 }  { 0.4631803}

{5 }  { 0.4745160}

{6 }  { 0.4659374}

{7 }  { 0.4724151}

{8 }  { 0.4675155}

{9 }  { 0.4712167}

{10}  { 0.4684181}

{11}  { 0.4705327}

{12}  { 0.4689340}

{13}  { 0.4701421}

{14}  { 0.4692289}

{15}  { 0.4699191}

{16}  { 0.4693974}

{17}  { 0.4697917}

{18}  { 0.4694936}

{19}  { 0.4697189}

{20}  { 0.4695486}

{21}  { 0.4696773}

The Root={ 0.4695801}  x-x0={ 0.0000973}

Fixed-point failed after {22} iteration.

>>>

沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...