我有以下簡單的 python 程序來使用二分法求方程根:from numpy import exp#def fun(x): return 5.0+4.0*x-exp(x)#a=3b=10.0eps=1.0e-15#fa=fun(a)fb=fun(b)#if fa*fb>0: print("wrong interval!!!",fa,fb) exit()#iter=1while (b-a)>eps: c=(a+b)/2.0 fc=fun(c) if fc==0: print("x = ",c) exit() if fc*fa>0: a=c fa=fc else: b=c fb=fc iter+=1#print("x = ",c)print("accuracy = ",'{:.2e}'.format(b-a))print("f(",c,") =",fun(c))print(iter," iterations needed")如果我將 a 放入錯誤的間隔(例如 a=3),它會說這是錯誤的間隔,但無論如何它都會繼續給出(顯然)錯誤的結果和四行錯誤:root:別名無效:名稱 less 不能使用別名,因為它是另一個魔術命令。而且,內核死掉了(我使用的是 jupyter)。你能幫助我嗎?
添加回答
舉報
0/150
提交
取消