利用正割理則 (Scant Method) 解 x3 + x - 1 非線性方程式解 Python語言
Input : equation = x3 + x - 1
x1 = 0, x2 = 1, E = 0.0001
Output : Root of the given equation = 0.682326
No. of iteration=5
# C++ Program to find root of an equations using secant method
from math import *
def f_cal(x):
# we are taking equation as x^3+x-1
f = pow(x,3) + x - 1.0
return f
def secant(x1,x2,E):
n = 0.0
xm=0.0
x0=0.0
c=0.0
if (f_cal(x1) * f_cal(x2) > 0):
print( "Can not find a root in the given inteval")
else:
# calculate the intermediate value
x0= (x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1))
# check if x0 is root of equation or not
c = f_cal(x1) * f_cal(x0)
# update the value of interval
x1 = x2
x2 = x0
# update number of iteration
n=n+1
#if x0 is the root of equation then break the loop
if (c == 0):
exit()
xm = (x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1))
# repeat the loop until the convergence
while (abs(xm - x0) >= E): #// repeat the loop
# calculate the intermediate value
x0= (x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1))
# check if x0 is root of equation or not
c = f_cal(x1) * f_cal(x0)
# update the value of interval
x1 = x2
x2 = x0
# update number of iteration
n=n+1
#if x0 is the root of equation then break the loop
if (c == 0):
break
xm = (x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1))
# repeat the loop until the convergence
print("Root of the given equation= {%8.5f}" %(x0))
print("No. of iterations = {%5d}" %(n))
#initializing the values
x1 = 0.0
x2 = 1.0
E = 0.0001
secant(x1, x2, E)
======= RESTART: H:/2018-09勤益科大數值分析/數值分析/PYTHON/EX2-7.py ==========
Root of the given equation= { 0.68233}
No. of iterations = { 5}
>>>
Input : equation =
x1 = 1.8, x2 = 2.0, E = 0.0001 Output : Root of the given equation = 0.682326 No. of iteration=5
# C++ Program to find root of an equations using secant method
from math import *
def f_cal(x):
# we are taking equation as x^3+x-1
f = exp(x)+1/pow(x,2) + 2*cos(x) - 6.0
return f
def secant(x1,x2,E):
n = 0.0
xm=0.0
x0=0.0
c=0.0
if (f_cal(x1) * f_cal(x2) > 0):
print( "Can not find a root in the given inteval")
else:
# calculate the intermediate value
x0= (x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1))
# check if x0 is root of equation or not
c = f_cal(x1) * f_cal(x0)
# update the value of interval
x1 = x2
x2 = x0
# update number of iteration
n=n+1
#if x0 is the root of equation then break the loop
if (c == 0):
exit()
xm = (x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1))
# repeat the loop until the convergence
while (abs(xm - x0) >= E): #// repeat the loop
# calculate the intermediate value
x0= (x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1))
# check if x0 is root of equation or not
c = f_cal(x1) * f_cal(x0)
# update the value of interval
x1 = x2
x2 = x0
# update number of iteration
n=n+1
#if x0 is the root of equation then break the loop
if (c == 0):
break
xm = (x1 * f_cal(x2) - x2 * f_cal(x1)) / (f_cal(x2) - f_cal(x1))
# repeat the loop until the convergence
print("Root of the given equation= {%8.5f}" %(x0))
print("No. of iterations = {%5d}" %(n))
#initializing the values
x1 = 1.8
x2 = 2.0
E = 0.0001
secant(x1, x2, E)
沒有留言:
張貼留言