2019年2月25日 星期一

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

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

F(X)= =1/5^x - x  改寫成
g(x)= 1/ (5^x) 與 g(x)= x
f(0.4) * f(0.5) < 0 所以取 x0=0.45



using Printf
#=======================================================
 * ex2-9.jl is used for solving nonlinear equation
 * based on Fixed-Point Algorithm g(x)=x with initial
 * approximation P0.
=========================================================#
MAX=50
TOL=0.0001

function g(x0::Float64)
    return (1/(5^x))
end   

i=1
x0=0.45
x=0.0
while(i<=MAX)
    x=g(x0)
    s=@sprintf("%-2d  %10.7lf",i-1,x0)
    println(s)
    if(abs(x-x0) < TOL)
        s=@sprintf("The Root=%10.7lf  x-x0=%10.7lf",x,abs(x-x0))
        println(s)
        break
    end   
    i+=1
    x0=x
end
s=@sprintf("Fixed-point failed after %d iteration.\n",i)
println(s)


輸出結果
0    0.4500000
1    1.0000000
2    0.2000000
3    0.7247797
4    0.3114589
5    0.6057586
6    0.3772185
7    0.5449236
8    0.4160205
9    0.5119342
10   0.4387058
11   0.4935803
12   0.4518582
13   0.4832420
14   0.4594395
15   0.4773815
16   0.4637935
17   0.4740479
18   0.4662885
19   0.4721482
20   0.4677164
21   0.4710644
22   0.4685329
23   0.4704457
24   0.4689997
25   0.4700925
26   0.4692664
27   0.4698907
28   0.4694188
29   0.4697755
30   0.4695059
31   0.4697096
32   0.4695556
33   0.4696720
The Root= 0.4695841  x-x0= 0.0000880
Fixed-point failed after 34 iteration.

沒有留言:

張貼留言

2024年4月24日 星期三 Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --2

 2024年4月24日 星期三 Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --2 AngularJS 實例 <!DOCTYPE html> <html> <head> &...