2019年2月25日 星期一

Julia語言 範例2-3 使用Newton-Raphson理則 解 x^3 + 4x^2 -10 =0 非線性方程式解

Julia語言 範例2-3 使用Newton-Raphson理則
解 x^3 + 4x^2 -10 =0 非線性方程式解
使用Newton-Raphson理則找出位於(1.0 ,2.0)之間的根
取誤差=0.001


虛擬碼

Algorithm NewtonRoot

    E0 : 初始化最小誤差 EPS,初始點 xo

    E1 :  x = x0

    E2 :  x0 = x - f(x) / f'(x)

    E3 :  if abs(x-x0) < eps,演算法結束,傳回 x

    E4 :  goto E1

End Algorithm

==========================================
using Printf

MAX=100
TOL=0.001
function   fx(x::Float64)
    return  (x^3 + 4*x^2 -10)
end

function   ffx(x::Float64)
    return  (3*x^2 + 8x)
end


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


輸出畫面
 i     Root of f(x)
 1    1.5000000
 2    1.3733333
 3    1.3652620
Root= 1.3652300 x-x0= 0.0000320

Newton-Raphson Method failed after  3 iterations!!!

沒有留言:

張貼留言

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> &...