'''
定點回路法
/* ex2-8.jl is used for solving nonlinear equation
* based on Fixed-Point Algorithm g(x)=x with initial
* approximation P0.
*/
定點回路法 求非線性方程式 f(x)=math.exp(x) - 3x^2 = 0
可以改寫成
y= x
y= (esp(x)/3) ^ 0.5
與
y= x
y= - (esp(x)/3) ^ 0.5
using Printf
#=======================================================
* ex2-8.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 g1(x0::Float64)
return ( (exp(x)/3)^0.5)
end
function g2(x0::Float64)
return (-(exp(x)/3)^0.5)
end
i=1
x0=0.0
x=0.0
while(i<=MAX)
x=g1(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)
i=1
x0=0.0
x=0.0
while(i<=MAX)
x=g2(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.0000000 1 0.5773503 2 0.7705652 3 0.8487220 4 0.8825453 5 0.8975975 6 0.9043784 7 0.9074499 8 0.9088446 9 0.9094786 10 0.9097669 11 0.9098981 The Root= 0.9099578 x-x0= 0.0000597 Fixed-point failed after 12 iteration. 0 0.0000000 1 -0.5773503 2 -0.4325829 3 -0.4650559 4 -0.4575660 5 -0.4592828 6 -0.4588887 The Root=-0.4589791 x-x0= 0.0000904 Fixed-point failed after 7 iteration.
沒有留言:
張貼留言