2019年1月19日 星期六

範例2-3 使用Newton-Raphson理則解pow(x,3)+4*pow(x,2)-10 =0 非線性方程式解

範例2-3
'''
 使用Newton-Raphson理則解pow(x,3)+4*pow(x,2)-10 =0 非線性方程式解
/* Used for solving nonlinear equation f(x)=0
 * based on Newton-Raphson Method with initial approximation
  */
 使用Newton-Raphson理則解pow(x,3)+4*pow(x,2)-10 =0  非線性方程式解
'''
from math import *
import sys

MAX=100
TOL=0.001

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


def ff(x):
    tmp=0.0+3*pow(x,2)+8*x
    return tmp

i=1
x0=1.5
x=0.0

while(i<=MAX):
    x=x0-f(x0)/ff(x0)
    print("{%2d}   {%10.7f}\n" %(i-1,x0))
 
    if (abs(x-x0) < TOL):
        print("Root=%10.7lf , x-x0=%10.7lf\n" %(x,fabs(x-x0)))
        break
    i=i+1
    x0=x

print("Newton-Raphson Method failed after {%2d} \iterations!!!\n" %(i))


輸出結果
========= RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX2-3.py ==============
{ 0}   { 1.5000000}

{ 1}   { 1.3733333}

{ 2}   { 1.3652620}

Root= 1.3652300 , x-x0= 0.0000320

Newton-Raphson Method failed after { 3} \iterations!!!

>>> 

 f(x) = pow(x,3)+4*pow(x,2)-10
        =(1.3652300)^3 + 3* (1.3652300)^2 -10 -->0


沒有留言:

張貼留言

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

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