《用 Python 学微积分》笔记 2
2016-04-23 02:19
435 查看
《用 Python 学微积分》原文见参考资料 1。
13、大 O 记法
比较两个函数时,我们会想知道,随着输入值 x 的增长或减小,两个函数的输出值增长或减小的速度究竟谁快谁慢。通过绘制函数图,我们可以获得一些客观的感受。
比较 x!、ex、x3 和 log(x) 的变化趋势。
View Code
如下定义牛顿迭代法:
Sympy 可以帮助我们求解方程:
参考资料:
[1] https://ryancheunggit.gitbooks.io/calculus-with-python/content/
13、大 O 记法
比较两个函数时,我们会想知道,随着输入值 x 的增长或减小,两个函数的输出值增长或减小的速度究竟谁快谁慢。通过绘制函数图,我们可以获得一些客观的感受。
比较 x!、ex、x3 和 log(x) 的变化趋势。
import numpy as np import matplotlib.pyplot as plt f = lambda x: x**2-2*x-4 l1 = lambda x: 2*x-8 l2 = lambda x: 6*x-20 x = np.linspace(0,5,100) plt.plot(x,f(x),'black') plt.plot(x[30:80],l1(x[30:80]),'blue', linestyle = '--') plt.plot(x[66:],l2(x[66:]),'blue', linestyle = '--') l = plt.axhline(y=0,xmin=0,xmax=1,color = 'black') l = plt.axvline(x=2,ymin=2.0/18,ymax=6.0/18, linestyle = '--') l = plt.axvline(x=4,ymin=6.0/18,ymax=10.0/18, linestyle = '--') plt.text(1.9,0.5,r"$x_0$", fontsize = 18) plt.text(3.9,-1.5,r"$x_1$", fontsize = 18) plt.text(3.1,1.3,r"$x_2$", fontsize = 18) plt.plot(2,0,marker = 'o', color = 'r' ) plt.plot(2,-4,marker = 'o', color = 'r' ) plt.plot(4,0,marker = 'o', color = 'r' ) plt.plot(4,4,marker = 'o', color = 'r' ) plt.plot(10.0/3,0,marker = 'o', color = 'r' ) plt.show()
View Code
如下定义牛顿迭代法:
def NewTon(f, s = 1, maxiter = 100, prt_step = False): for i in range(maxiter): # 相较于f.evalf(subs={x:s}),subs()是更好的将值带入并计算的方法。 s = s - f.subs(x,s)/f.diff().subs(x,s) if prt_step == True: print "After {0} iteration, the solution is updated to {1}".format(i+1,s) return s from sympy.abc import x f = x**2-2*x-4 print NewTon(f, s = 2, maxiter = 4, prt_step = True) # After 1 iteration, the solution is updated to 4 # After 2 iteration, the solution is updated to 10/3 # After 3 iteration, the solution is updated to 68/21 # After 4 iteration, the solution is updated to 3194/987 # 3194/987
Sympy 可以帮助我们求解方程:
import sympy from sympy.abc import x f = x**2-2*x-4 print sympy.solve(f,x) # result is:[1 + sqrt(5), -sqrt(5) + 1]
参考资料:
[1] https://ryancheunggit.gitbooks.io/calculus-with-python/content/
相关文章推荐
- python爬虫开发(6)—爬虫入门--Requests爬虫(session)
- python爬虫开发(6)—爬虫入门--Requests爬虫(cookie)
- Python爬虫----爬虫入门(5)---Requests基础
- python的collection系列-双向队列和单向队列
- 一键搞定python连接mysql驱动有关问题(windows版本)
- Python 探针的实现原理
- Python 中的 else详解
- Python的 循环器 itertools
- python super方法
- python 单例
- python爬虫开发–实战百度招聘(json包的处理)
- Python2.7爬虫练习爬百度百科python词条
- (转载)Python学习–数据分析路线
- Python Challenge
- python中的实例方法、静态方法、类方法、类变量和实例变量
- python的collection系列-可命名元组(namedtuple)
- python中的函数
- python之pandas的使用:数据的导入技巧
- python爬虫
- Python中Matplotlib绘图