2019年2月25日 星期一

Julia語言 範例2-5 非線性方程式 f(x)= (4x-7) /(x-1)的根

Julia語言 範例2-5 非線性方程式  f(x)= (4x-7) /(x-1)
利用牛頓(Newton-Raphson)方法 求f(x)的根取誤差=0.001
x0=1.5 ,1.625 , 1.875 , 1.95 , 3.0 時的現象  (會出現overflow)

using Printf
#=========================================================
Julia has no do-while construct. Here is one of several
ways to implement do-while behavior.

julia> i = 0
0

julia> while true
           println(i)
           i += 1
           i % 6 == 0 && break
       end
============================================================#

MAX=100
TOL=0.001

function f(x0::Float64)
    return ((4*x0-7)/(x0-2))
end   

function ff(x0::Float64)
    return (-1/(x0-2)^2)
end   

i=1
x0=1.5
while(i<=MAX)
    x=x0-f(x0)/ff(x0)
    s=@sprintf("%2d   %10.7lf",i,x0)
    println(s)
    if(abs(x-x0)<TOL)
        s=@sprintf("Root=%10.7lf x-x0=%10.7lf\n",x,abs(x-x0))
        println(s)
        break
    end   
    i+=1
    x0=x
end
s=@sprintf("Newton-Raphson Method failed after %2d iterations!!!\n",i)
println(s)


i=1
x0=1.625
while(i<=MAX)
    x=x0-f(x0)/ff(x0)
    s=@sprintf("%2d   %10.7lf",i,x0)
    println(s)
    if(abs(x-x0)<TOL)
        s=@sprintf("Root=%10.7lf x-x0=%10.7lf\n",x,abs(x-x0))
        println(s)
        break
    end   
    i+=1
    x0=x
end
s=@sprintf("Newton-Raphson Method failed after %2d iterations!!!\n",i)
println(s)


i=1
x0=1.875
while(i<=MAX)
    x=x0-f(x0)/ff(x0)
    s=@sprintf("%2d   %10.7lf",i,x0)
    println(s)
    if(abs(x-x0)<TOL)
        s=@sprintf("Root=%10.7lf x-x0=%10.7lf\n",x,abs(x-x0))
        println(s)
        break
    end   
    i+=1
    x0=x
end
s=@sprintf("Newton-Raphson Method failed after %2d iterations!!!\n",i)
println(s)


i=1
x0=1.95
while(i<=MAX)
    x=x0-f(x0)/ff(x0)
    s=@sprintf("%2d   %10.7lf",i,x0)
    println(s)
    if(abs(x-x0)<TOL)
        s=@sprintf("Root=%10.7lf x-x0=%10.7lf\n",x,abs(x-x0))
        println(s)
        break
    end   
    i+=1
    x0=x
end
s=@sprintf("Newton-Raphson Method failed after %2d iterations!!!\n",i)
println(s)


i=1
x0=3.0
while(i<=MAX)
    x=x0-f(x0)/ff(x0)
    s=@sprintf("%2d   %10.7lf",i,x0)
    println(s)
    if(abs(x-x0)<TOL)
        s=@sprintf("Root=%10.7lf x-x0=%10.7lf\n",x,abs(x-x0))
        println(s)
        break
    end   
    i+=1
    x0=x
end
s=@sprintf("Newton-Raphson Method failed after %2d iterations!!!\n",i)
println(s)


輸出畫面
x0= 1.5  Newton-Raphson Method failed after 101 iterations!!!
x0=1.625 Root= 1.7500038 x-x0= 0.0009727
x0=1.875 Root= 1.7500038 x-x0= 0.0009727
x0=1.95  Root= 1.7500002 x-x0= 0.0001979
x0=3.0   Newton-Raphson Method failed after 101 iterations!!!
 
======================================================
 1    1.5000000
 2    2.0000000
 3          NaN
 4          NaN
 5          NaN
 6          NaN
 7          NaN
 8          NaN
 9          NaN
10          NaN
11          NaN
12          NaN
13          NaN
14          NaN
15          NaN
16          NaN
17          NaN
18          NaN
19          NaN
20          NaN
21          NaN
22          NaN
23          NaN
24          NaN
25          NaN
26          NaN
27          NaN
28          NaN
29          NaN
30          NaN
31          NaN
32          NaN
33          NaN
34          NaN
35          NaN
36          NaN
37          NaN
38          NaN
39          NaN
40          NaN
41          NaN
42          NaN
43          NaN
44          NaN
45          NaN
46          NaN
47          NaN
48          NaN
49          NaN
50          NaN
51          NaN
52          NaN
53          NaN
54          NaN
55          NaN
56          NaN
57          NaN
58          NaN
59          NaN
60          NaN
61          NaN
62          NaN
63          NaN
64          NaN
65          NaN
66          NaN
67          NaN
68          NaN
69          NaN
70          NaN
71          NaN
72          NaN
73          NaN
74          NaN
75          NaN
76          NaN
77          NaN
78          NaN
79          NaN
80          NaN
81          NaN
82          NaN
83          NaN
84          NaN
85          NaN
86          NaN
87          NaN
88          NaN
89          NaN
90          NaN
91          NaN
92          NaN
93          NaN
94          NaN
95          NaN
96          NaN
97          NaN
98          NaN
99          NaN
100          NaN
Newton-Raphson Method failed after 101 iterations!!!

 1    1.6250000
 2    1.8125000
 3    1.7656250
 4    1.7509766
Root= 1.7500038 x-x0= 0.0009727

Newton-Raphson Method failed after  4 iterations!!!

 1    1.8750000
 2    1.8125000
 3    1.7656250
 4    1.7509766
Root= 1.7500038 x-x0= 0.0009727

Newton-Raphson Method failed after  4 iterations!!!

 1    1.9500000
 2    1.9100000
 3    1.8524000
 4    1.7919430
 5    1.7570369
 6    1.7501981
Root= 1.7500002 x-x0= 0.0001979

Newton-Raphson Method failed after  6 iterations!!!

 1    3.0000000
 2    8.0000000
 3   158.0000000
 4   97658.0000000
 5   38146972658.0000229
 6   5820766091346748375040.0000000
 7   135525271560688433474159118908309231747727360.0000000
 8   73468396926393384679514105682249657134065937588368498667350306457576200536943192421957632.0000000
 9   21590421387736356954845175477318195813934831457681903459410459099972442985296985166524414010377109304944487332172036675687278085164774782269901823598293450947135537747747397435392.0000000
10          Inf
11          NaN
12          NaN
13          NaN
14          NaN
15          NaN
16          NaN
17          NaN
18          NaN
19          NaN
20          NaN
21          NaN
22          NaN
23          NaN
24          NaN
25          NaN
26          NaN
27          NaN
28          NaN
29          NaN
30          NaN
31          NaN
32          NaN
33          NaN
34          NaN
35          NaN
36          NaN
37          NaN
38          NaN
39          NaN
40          NaN
41          NaN
42          NaN
43          NaN
44          NaN
45          NaN
46          NaN
47          NaN
48          NaN
49          NaN
50          NaN
51          NaN
52          NaN
53          NaN
54          NaN
55          NaN
56          NaN
57          NaN
58          NaN
59          NaN
60          NaN
61          NaN
62          NaN
63          NaN
64          NaN
65          NaN
66          NaN
67          NaN
68          NaN
69          NaN
70          NaN
71          NaN
72          NaN
73          NaN
74          NaN
75          NaN
76          NaN
77          NaN
78          NaN
79          NaN
80          NaN
81          NaN
82          NaN
83          NaN
84          NaN
85          NaN
86          NaN
87          NaN
88          NaN
89          NaN
90          NaN
91          NaN
92          NaN
93          NaN
94          NaN
95          NaN
96          NaN
97          NaN
98          NaN
99          NaN
100          NaN
Newton-Raphson Method failed after 101 iterations!!!

沒有留言:

張貼留言

2024產專班 作業2

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