2019年1月19日 星期六

例題2-5 使用Newton-Raphson理則解 f(x) = (4x-7) / (x-2) =0 非線性方程式解

例題2-5
'''
 使用Newton-Raphson理則解 f(x) = (4x-7) / (x-2)  =0 非線性方程式解
/* ex2-4.c is used for solving nonlinear equation f(x)=0
 * based on Newton-Raphson Method with initial approximation
 * p0.
 */
 使用Newton-Raphson理則解 f(x) = (4x-7) / (x-2) = 0 非線性方程式解
'''



from math import *
import sys

MAX=100
TOL=0.001

def f(x):
    tmp=0.0+ ((4*x-7)/(x-2))
    return tmp


def ff(x):
    tmp=0.0 - (1/ pow( (x-2),2))
    return tmp

i=1
x0=1.5   #1.5 , 1.625 , 1.875 , 1.95 , 3.0 
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))

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

i=1
x0=1.5

x=0.0

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

Traceback (most recent call last):
  File "F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX2-5.py", line 29, in <module>
    x=x0-f(x0)/ff(x0)
  File "F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX2-5.py", line 16, in f
    tmp=0.0+ ((4*x-7)/(x-2))
ZeroDivisionError: float division by zero
>>> 


i=1
x0=1.625

x=0.0

========= RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX2-5.py =============
{ 0}   { 1.6250000}

{ 1}   { 1.8125000}

{ 2}   { 1.7656250}

{ 3}   { 1.7509766}

Root= 1.7500038  ,  x-x0= 0.0009727

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


>>> 

i=1
x0=1.875

x=0.0
========= RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX2-5.py =============
{ 0}   { 1.8750000}

{ 1}   { 1.8125000}

{ 2}   { 1.7656250}

{ 3}   { 1.7509766}

Root= 1.7500038  ,  x-x0= 0.0009727

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


>>> 


i=1
x0=1.95

x=0.0
========= RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX2-5.py =============
{ 0}   { 1.9500000}

{ 1}   { 1.9100000}

{ 2}   { 1.8524000}

{ 3}   { 1.7919430}

{ 4}   { 1.7570369}

{ 5}   { 1.7501981}

Root= 1.7500002  ,  x-x0= 0.0001979

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


>>> 


i=1
x0=3.0

x=0.0
========= RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX2-5.py =============
{ 0}   { 3.0000000}

{ 1}   { 8.0000000}

{ 2}   {158.0000000}

{ 3}   {97658.0000000}

{ 4}   {38146972658.0000229}

{ 5}   {5820766091346748375040.0000000}

{ 6}   {135525271560688433474159118908309231747727360.0000000}

{ 7}   {73468396926393384679514105682249657134065937588368498667350306457576200536943192421957632.0000000}

Traceback (most recent call last):
  File "F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX2-5.py", line 29, in <module>
    x=x0-f(x0)/ff(x0)
  File "F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX2-5.py", line 21, in ff
    tmp=0.0 - (1/ pow( (x-2),2))
OverflowError: math range error

>>> 

沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...