'''
定點回路法
/* ex2-8.py is used for solving nonlinear equation
* based on Fixed-Point Algorithm g(x)=x with initial
* approximation P0.
*/
例題 EX2-8 定點回路法 求非線性方程式 f(x)=math.exp(x) - 3x^2 = 0
y= x
y= (esp(x)/3) ^ 0.5
與
y= x
y= - (esp(x)/3) ^ 0.5
'''
import math
MAX=50
TOL=0.001
def g(x):
temp=( pow ( ( math.exp(x)/3) ,0.5)+0.0)
return temp
def g1(x):
temp=-(pow ( ( math.exp(x)/3) ,0.5)+0.0)
return temp
i=1
# f(0)= 0.5575 ,
# f(0)=-0.5575
x0=0.0
print("==============================")
print('x\t\t x0')
while(i<=MAX):
x=g(x0)
print("{%-2d}\t\t {%10.7f}" %(i-1,x0))
if(abs(x-x0) < TOL):
print("The Root={%10.7f} x-x0={%10.7f}\n" %(x,abs(x-x0)))
break
i=i+1
x0=x
print("Fixed-point failed after {%d} iteration.\n"%(i))
i=1
# f(0)= 0.5575 ,
# f(0)=-0.5575
x0=0.0
print("==============================")
print('x\t\t x0')
while(i<=MAX):
x=g1(x0)
print("{%-2d}\t\t {%10.7f}" %(i-1,x0))
if(abs(x-x0) < TOL):
print("The Root={%10.7f} x-x0={%10.7f}\n" %(x,abs(x-x0)))
break
i=i+1
x0=x
print("Fixed-point failed after {%d} iteration.\n"%(i))
========= RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/ex2-8.py ============
==============================
x x0
{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}
The Root={ 0.9094786} x-x0={ 0.0006340}
Fixed-point failed after {9} iteration.
==============================
x x0
{0 } { 0.0000000}
{1 } {-0.5773503}
{2 } {-0.4325829}
{3 } {-0.4650559}
{4 } {-0.4575660}
{5 } {-0.4592828}
The Root={-0.4588887} x-x0={ 0.0003941}
Fixed-point failed after {6} iteration.
>>>
沒有留言:
張貼留言