2019年1月19日 星期六

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

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

'''
 定點回路法
/* 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
print("==============================")
print('x\t\t     x0')
while(i<=MAX):
    x=g(x0)
    print("{%-2d}\t\t {%10.7f}" %(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))



======== RESTART: F:\2018-09勤益科大數值分析\數值分析\PYTHON\ex2-9-2.py ===========
==============================
x      x0
{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.

>>> 

沒有留言:

張貼留言

Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3

  Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3 AngularJS 實例 <!DOCTYPE html> <html> <head> <meta charse...