#-*-encoding:utf-8-*- #插值法求解问题 #http://zhidao.baidu.com/question/24991328?si=2 def getResult(r): tmp1 = 59 * ((1 + r) **(-1)) tmp2 = 59 * ((1 + r) **(-2)) tmp3 = 59 * ((1 + r) **(-3)) tmp4 = 59 * ((1 + r) **(-4)) tmp5 = (59 + 1250) * ((1 + r) **(-5)) tmp6 = -1000 x = sum([tmp1, tmp2, tmp3, tmp4, tmp5, tmp6]) return(x) def getNext(a, b): #其中a为区间下限、b为区间上限 m = (b + a) / 2 tmpa = getResult(a) tmpb = getResult(b) tmpm = getResult(m) tmp1, tmp2 = 0, 0 if tmpa * tmpb > 0: tmp1 = max(a, b) tmp2 = tmp1 + abs(b-a) else: if tmpa * tmpm > 0: tmp1 = m tmp2 = b else: tmp1 = a tmp2 = m #print(a,m,b) #print(tmpa,tmpm,tmpb) return(tmp1, tmp2) def write(a,b): f = open('data.txt', 'a') f.write(str(a) + '/t' + str(b) + "/n") f.close() if __name__ == '__main__': little, big = 0.01, 0.20 i = 1 while i < 50: little, big = getNext(little, big) write(little, big) i += 1 该解法的理论基础是初等微积分中的罗尔定理