20250429
This commit is contained in:
20
159-1.py
Normal file
20
159-1.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
def fx(x):
|
||||||
|
return x**4-3*x+1
|
||||||
|
|
||||||
|
|
||||||
|
def SolveByDivTwo(x1,x2,err):
|
||||||
|
while abs(x2-x1) >= err:
|
||||||
|
x = (x1+x2)/2
|
||||||
|
if fx(x) * fx(x1) < 0:
|
||||||
|
x2 = x
|
||||||
|
else:
|
||||||
|
x1 = x
|
||||||
|
return (x1+x2)/2
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
x1 = 0.3
|
||||||
|
x2 = 0.4
|
||||||
|
err = 0.5e-5
|
||||||
|
root = SolveByDivTwo(x1, x2, err)
|
||||||
|
print(f"Root: {root:.5f}")
|
||||||
43
159-3.py
Normal file
43
159-3.py
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
def fd1(x):
|
||||||
|
return 1+1/x**2
|
||||||
|
|
||||||
|
def fd2(x):
|
||||||
|
return 1/(x-1)**0.5
|
||||||
|
|
||||||
|
def Renew(x,fd,err):
|
||||||
|
count=0
|
||||||
|
i = 0
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
xk = fd(x)
|
||||||
|
if abs(i) < (xk-x):
|
||||||
|
count += 1
|
||||||
|
else:
|
||||||
|
count = 0
|
||||||
|
if count > 10:
|
||||||
|
print("不收敛")
|
||||||
|
break
|
||||||
|
if abs(xk-x) < err:
|
||||||
|
return xk
|
||||||
|
i = xk-x
|
||||||
|
x = xk
|
||||||
|
except Exception as e:
|
||||||
|
print(f"发生错误: {e}")
|
||||||
|
return None
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
x0 = 1.5
|
||||||
|
|
||||||
|
result = Renew(x0, fd1, 1e-5)
|
||||||
|
if result is not None:
|
||||||
|
print(f"1式收敛 解为: {result:.5f}")
|
||||||
|
else:
|
||||||
|
print("1式不收敛")
|
||||||
|
|
||||||
|
result = Renew(x0, fd2, 1e-5)
|
||||||
|
if result is not None:
|
||||||
|
print(f"2式收敛 解为: {result:.5f}")
|
||||||
|
else:
|
||||||
|
print("2式不收敛")
|
||||||
Reference in New Issue
Block a user