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