第1关:给定方程给定单调上升区间方程的根
函数f(x)=x2−10x+3在区间[8,12]单调上升且与x轴相交,求解该近似解,要求精度0.0001,即|f(x)|<=0.0001。
def f(x):
return x**2-10*x+3
#Student Start
def findroot(a,b,eps):
while abs(a-b)>0.00001:
if f(a+0.000001)*f(b)<0:
a+=0.000001
if f(a)*f(b-0.000001)<0:
b-=0.000001
return (a+b)/2
#Student End
print("%.4f"%findroot(8,12,0.0001))
第2关:给定方程给定单调下降区间方程的根
函数f(x)=x2−10x+3在区间[-2, 2]单调下降且与x轴相交,求解该近似解,要求精度0.0001 (|f(x)|<=0.0001)。
def f(x):
return x**2-10*x+3
#Student Start
def findroot(a,b,eps):
while abs(a-b)>0.00001:
if f(a+0.000001)*f(b)<0:
a+=0.000001
if f(a)*f(b-0.000001)<0:
b-=0.000001
return (a+b)/2
#Student End
print("%.4f"%findroot(-2,2,0.0001))
第3关:给定方程给定区间内所有方程的根(可能多个)
函数f(x)=x2−10x+3在区间[-2, 12]与x轴有两个交点,求解这两个近似解,要求精度0.0001,即|f(x)|<=0.0001。
def f(x):
return x**2-10*x+3
#Student Start
def findroot(a,b,eps):
while abs(a-b)>0.00001:
if f(a+0.000001)*f(b)<0:
a+=0.000001
if f(a)*f(b-0.000001)<0:
b-=0.000001
return (a+b)/2
#Student End
print("%.4f"%findroot(-2,2,0.0001))
print("%.4f"%findroot(8,12,0.0001))
求求三连。。。