111
This commit is contained in:
3
159-2.py
3
159-2.py
@@ -1,6 +1,7 @@
|
|||||||
import math
|
import math
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
#把原函数换成题干的形式########################
|
||||||
def fx(x):
|
def fx(x):
|
||||||
return math.exp(x)-math.sin(x)
|
return math.exp(x)-math.sin(x)
|
||||||
|
|
||||||
@@ -25,7 +26,7 @@ def DrawGraph(a, b, stepper):
|
|||||||
plt.show()
|
plt.show()
|
||||||
return x, y
|
return x, y
|
||||||
|
|
||||||
|
#把范围改成题干的形式#######################
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
a = -2*math.pi
|
a = -2*math.pi
|
||||||
b = math.pi
|
b = math.pi
|
||||||
|
|||||||
7
159-5.py
7
159-5.py
@@ -1,6 +1,6 @@
|
|||||||
import math
|
import math
|
||||||
|
|
||||||
|
#原函数和导数改成题干的形式#####################
|
||||||
def f1(x):
|
def f1(x):
|
||||||
return x**2 + 10*math.cos(x)
|
return x**2 + 10*math.cos(x)
|
||||||
|
|
||||||
@@ -17,12 +17,14 @@ def df2(x):
|
|||||||
# 牛顿方法求解方程
|
# 牛顿方法求解方程
|
||||||
def NewtonSolve(fx, dfx, x0, err, N0):
|
def NewtonSolve(fx, dfx, x0, err, N0):
|
||||||
count = 0
|
count = 0
|
||||||
|
print(f"k={count}, x0={x0}")
|
||||||
x1 = x0 + 1 + err
|
x1 = x0 + 1 + err
|
||||||
while abs(x1 - x0) > err or abs(fx(x1)) > err: # 添加条件修正根误差太大的问题
|
while abs(x1 - x0) > err or abs(fx(x1)) > err: # 添加条件修正根误差太大的问题
|
||||||
if abs(dfx(x1)) < 1e-10:
|
if abs(dfx(x1)) < 1e-10:
|
||||||
return None, 0
|
return None, 0
|
||||||
x1 = x0 - fx(x0) / dfx(x0)
|
x1 = x0 - fx(x0) / dfx(x0)
|
||||||
count += 1
|
count += 1
|
||||||
|
print(f"k={count}, x{count}={x1},x1-x0={abs(x1-x0)}")
|
||||||
if count > N0:
|
if count > N0:
|
||||||
return None, -1
|
return None, -1
|
||||||
x0 = x1
|
x0 = x1
|
||||||
@@ -41,6 +43,7 @@ if __name__ == "__main__":
|
|||||||
err = 1e-5
|
err = 1e-5
|
||||||
N0 = 100
|
N0 = 100
|
||||||
|
|
||||||
|
#把初始值换成题干形式###############
|
||||||
x0 = 1.6
|
x0 = 1.6
|
||||||
result,status = NewtonSolve(f1, df1, x0, err,N0)
|
result,status = NewtonSolve(f1, df1, x0, err,N0)
|
||||||
if status == 1:
|
if status == 1:
|
||||||
@@ -51,7 +54,7 @@ if __name__ == "__main__":
|
|||||||
print("f1导数为0,无法收敛")
|
print("f1导数为0,无法收敛")
|
||||||
|
|
||||||
|
|
||||||
x0 = FindRootZone(f2, -5, 5, 0.01)
|
x0 = FindRootZone(f2, -5, 5, 0.01) # 查找f2的根区间与步长
|
||||||
result, status = NewtonSolve(f2, df2, x0, err,N0)
|
result, status = NewtonSolve(f2, df2, x0, err,N0)
|
||||||
if status == 1:
|
if status == 1:
|
||||||
print(f"f2收敛 解为: {result}")
|
print(f"f2收敛 解为: {result}")
|
||||||
|
|||||||
6
159-6.py
6
159-6.py
@@ -1,6 +1,6 @@
|
|||||||
import math
|
import math
|
||||||
|
|
||||||
|
#把函数改成题干的形式#####################
|
||||||
def f(x):
|
def f(x):
|
||||||
return x**2 - 30
|
return x**2 - 30
|
||||||
|
|
||||||
@@ -10,12 +10,14 @@ def df(x):
|
|||||||
# 牛顿方法求解方程
|
# 牛顿方法求解方程
|
||||||
def NewtonSolve(fx, dfx, x0, err, N0):
|
def NewtonSolve(fx, dfx, x0, err, N0):
|
||||||
count = 0
|
count = 0
|
||||||
|
print(f"k={count}, x0={x0}")
|
||||||
x1 = x0 + 1 + err
|
x1 = x0 + 1 + err
|
||||||
while abs(x1 - x0) > err or abs(fx(x1)) > err: # 添加条件修正根误差太大的问题
|
while abs(x1 - x0) > err or abs(fx(x1)) > err: # 添加条件修正根误差太大的问题
|
||||||
if abs(dfx(x1)) < 1e-10:
|
if abs(dfx(x1)) < 1e-10:
|
||||||
return None, 0
|
return None, 0
|
||||||
x1 = x0 - fx(x0) / dfx(x0)
|
x1 = x0 - fx(x0) / dfx(x0)
|
||||||
count += 1
|
count += 1
|
||||||
|
print(f"k={count}, x{count}={x1},x1-x0={abs(x1-x0)}")
|
||||||
if count > N0:
|
if count > N0:
|
||||||
return None, -1
|
return None, -1
|
||||||
x0 = x1
|
x0 = x1
|
||||||
@@ -34,6 +36,6 @@ if __name__ == "__main__":
|
|||||||
err = 1e-4
|
err = 1e-4
|
||||||
N0 = 100
|
N0 = 100
|
||||||
|
|
||||||
x0 = FindRootZone(f, 5, 6, 0.01)
|
x0 = FindRootZone(f, 5, 6, 0.1) # 查找f的根的区间与步长,改成题干对应的范围
|
||||||
result,status = NewtonSolve(f, df, x0, err,N0)
|
result,status = NewtonSolve(f, df, x0, err,N0)
|
||||||
print(f"sqrt(30) = {result:.3f}")
|
print(f"sqrt(30) = {result:.3f}")
|
||||||
@@ -17,11 +17,11 @@ def SecantSolve(fx, x0, x1, err=1e-10, N0=100):
|
|||||||
x1 = x2
|
x1 = x2
|
||||||
return x2,1
|
return x2,1
|
||||||
|
|
||||||
|
#把精度要求改成题干要求的##########################
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
err = 1e-5
|
err = 1e-5
|
||||||
N0 = 100
|
N0 = 100
|
||||||
|
##把初始函数和初始值改成题干要求的##########################
|
||||||
x0 = 0.3
|
x0 = 0.3
|
||||||
x1 = 0.4
|
x1 = 0.4
|
||||||
fx = lambda x: x**4 - 3*x + 1
|
fx = lambda x: x**4 - 3*x + 1
|
||||||
|
|||||||
@@ -45,12 +45,12 @@ def MullerSolve(fx,x0,x1,x2,err1,err2,N):
|
|||||||
f2 = f3
|
f2 = f3
|
||||||
q = h1
|
q = h1
|
||||||
|
|
||||||
|
#精度要求#########################(152页)
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
err1 = 1e-5
|
err1 = 1e-5
|
||||||
err2 = 1e-5
|
err2 = 1e-5
|
||||||
N = 100
|
N = 100
|
||||||
|
##把初始函数和初始值改成题干要求的##########################
|
||||||
x0 = 0.3
|
x0 = 0.3
|
||||||
x1 = 0.5
|
x1 = 0.5
|
||||||
x2 = 0.4
|
x2 = 0.4
|
||||||
|
|||||||
Reference in New Issue
Block a user