2019年1月19日 星期六

利用 定點迴路 解 非線性方程式 f(x) = x^3 + 4x2 -10 =0 (往復式收斂)

P2-45 定點迴路中往復式收斂

# Python Program to find root of an equations using secant method
# 利用 定點迴路   解 非線性方程式 f(x) = x^3 + 4x2 -10 =0
# g(x)= x
# g(x)= 0.5 (10-x^3) ^ 0.5
# f(x) = x^3 + 4x2 -10

from math import *

MAX=50
TOL=0.0001

def g(x):
    temp1= ( 10 - pow(x,3))
    temp= 0.5*  pow( temp1 , 0.5)
    return temp

def f(x):
    temp= pow(x,3) + 4* pow(x,2) -10 
    return temp


i=0    #0,1 被使用 從2開始
x0=1.3
x1=1.4
q0=f(x0)
q1=f(x1)

print("  i        xi            f(x)\n")
print("====================================\n")
print("{%-2d}   {%10.6f}   {%10.6f}\n" %(0,x0,q0))
print("{%-2d}   {%10.6f}   {%10.6f}\n" %(1,x1,q1))
print("============================")
while(i<=MAX):
    x=g(x0)
    print("{%-2d}   {%10.6f}   {%10.6f}\n" %(i,x0,x))
    if(abs(x-x0) < TOL):
        print("====================================================\n")
        print("The Root={%10.6f} f({%10.6f})={%10.6f}\n" %(x,x,f(x)) )
        break
    else:
        i=i+1
        x0=x

if(i>MAX):
    print("Secant Method faileds!!!\n")



==== RESTART: F:\2018-09勤益科大數值分析\數值分析\PYTHON\EX2-12.py ==========
  i        xi            f(x)
====================================

{0 }   {  1.300000}   { -1.043000}          f(1.3)

{1 }   {  1.400000}   {  0.584000}           f(1.4)

============================
{0 }   {  1.300000}   {  1.396693}

{1 }   {  1.396693}   {  1.348648}

{2 }   {  1.348648}   {  1.373591}

{3 }   {  1.373591}   {  1.360916}

{4 }   {  1.360916}   {  1.367430}

{5 }   {  1.367430}   {  1.364102}

{6 }   {  1.364102}   {  1.365807}

{7 }   {  1.365807}   {  1.364934}

{8 }   {  1.364934}   {  1.365381}

{9 }   {  1.365381}   {  1.365153}

{10}   {  1.365153}   {  1.365270}

{11}   {  1.365270}   {  1.365210}

====================================================

The Root={  1.365210} f({  1.365210})={  1.365240}


>>> 

沒有留言:

張貼留言

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

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