MIT公开课: Python 笔记6 二分法,牛顿-拉夫森方法,列表
Lecture5: Bisection methods , Newton/Raphson, introduction to lists二分法,牛顿,拉复生方法,列表
Bisection methods 二分法
注意:
# bug: when x < 1, sqrt(x) > x = high eg.x=0.25 sqrt(x) = 0.5
# fix bug: high = max(x, 1.0)
def squareRootBi(x, epsilon):"""Assumes x >= 0 and epsilon > 0Return y s.t. y*y is within epsilon of x"""assert x >= 0, 'x must be non-negative, not' + str(x)assert epsilon > 0, 'epsilon must be positive, not' + str(epsilon)low = 0# bug: when x < 1, sqrt(x) > x = high eg.x=0.25 sqrt(x) = 0.5# fix bug: high = max(x, 1.0)high = xguess = (low + high) / 2.0ctr = 1while abs(guess ** 2 - x) > epsilon and ctr <= 100:# print 'low:', low, 'high:', high, 'guess:', guessif guess**2 < x:low = guesselse:high = guessguess = (low + high) / 2.0ctr += 1assert ctr <= 100, 'Iteration count exceeded'print 'Bi method. Num. iterations:', ctr, 'Estimate:', guessreturn guess
Newton Raphson 牛顿-拉夫森方法
Bisection methods 二分法:
\Delta(guess) = guess ^ 2 -x
\Delta(guess) = 0
Newton 求导
guess点的导数 = guess 点的斜率
\Delta'(guess)= 2guess_i = k
k = {\Delta(guess_i) \over guess_{i+1} - guess_i}
\Delta(guess_i) = guess_i ^2- x
由上面3个式子得到:
guess_{i+1} = guess_i - {\Delta(guess_i) \over 2guess_i}
例子:
\Delta(3) = 3 ^2- 16 = -7
guess_{i+1} = 3 - {\Delta(3) \over 2\times3} = 4.1666
def squareRootNR(x, epsilon):"""Assumes x >= 0 and epsilon > 0Return y s.t. y*y is within epsilon of x"""assert x >= 0, 'x must be non-negative, not' + str(x)assert epsilon > 0, 'epsilon must be positive, not' + str(epsilon)x = float(x)guess = x / 2.0guess = 0.001diff = guess ** 2 - xctr = 1while abs(diff) > epsilon and ctr <= 100:# print 'Error:', diff, 'guess:', guessguess = guess - diff/(2.0*guess)diff = guess ** 2 - xctr += 1assert ctr <= 100, 'Iteration count exceeded'print 'NR method. Num. iterations:', ctr, 'Estimate:', guessreturn guess
Lists 列表
3wschool Python 列表
non-scalar types 非基本类型
- Tuple 元组 (immutable 不可改变的)
- String 字符串(immutable 不可改变的)
- List 列表 (mutable 可改变的)
>>> Techs = ['MIT', 'Cal Tech']
>>> print Techs
['MIT', 'Cal Tech']
>>> Ivys = ['Harvard', 'Yale', 'Brown']
>>> print Ivys
['Harvard', 'Yale', 'Brown']
>>> Univs = []
>>> Univs.append(Techs)
>>> print Univs
[['MIT', 'Cal Tech']]
>>> Univs.append(Ivys)
>>> print Univs
[['MIT', 'Cal Tech'], ['Harvard', 'Yale', 'Brown']]
for e in Univs:print efor c in e: print c['MIT', 'Cal Tech']
MIT
Cal Tech
['Harvard', 'Yale', 'Brown']
Harvard
Yale
Brown
>>> Univs = Techs + Ivys
>>> print Univs
['MIT', 'Cal Tech', 'Harvard', 'Yale', 'Brown']
>>> Ivys.remove('Harvard')
>>> print Ivys
['Yale', 'Brown']
>>> Ivys[1] = -1
>>> print Ivys
['Yale', -1]
MIT公开课: Python 笔记6 二分法,牛顿-拉夫森方法,列表相关推荐
- Python:实现newton raphson牛顿-拉夫森算法(附完整源码)
Python:实现newton raphson牛顿-拉夫森算法 import math as m def calc_derivative(f, a, h=0.001):return (f(a + h) ...
- 【Math ML】Newton-Raphson.python 牛顿拉夫森方法
牛顿·拉弗森方法 牛顿·拉弗森方法 牛顿·拉弗森方法原理与举例 python 手写牛顿拉弗森 python 调用牛顿拉弗森函数一步求解 牛顿·拉弗森方法 牛顿拉弗森方法 用于求解近似方程式的根,f(0 ...
- 牛顿—拉夫森迭代、加速牛顿—拉夫森迭代和哈利法改进牛顿——拉夫森迭代
目录 一.牛顿-拉夫森迭代 1.1牛顿-拉夫森定理 1.2牛顿-拉夫森迭代的matlab实现 1.3牛顿-拉夫森迭代的python实现 二.加速-牛顿拉夫森迭代 2.1牛顿-拉夫森迭代的加速收敛 2. ...
- 数值分析之牛顿拉夫森迭代(牛顿法)
@数值分析之非线性方程求解 文章目录 一.牛顿拉夫森迭代 1.1 牛顿拉夫森法 1.1.1 牛顿-拉夫森定理: 1.1.2 matlab版算法: 二.题目及实现代码 2.1 题目 2.2 输入输出格式 ...
- C++newton raphson method牛顿拉夫森法的实现算法(附完整源码)
C++newton raphson method牛顿拉夫森法的实现算法 C++newton raphson method牛顿拉夫森法的实现算法完整源码(定义,实现,main函数测试) C++newto ...
- R语言 牛顿-拉夫森迭代法求方程组
牛顿-拉夫森迭代法: xk+1=xk−[f′(x)]−1f(x)x_{k+1}=x_{k}-{[f'(x)]^{-1}}f(x)xk+1=xk−[f′(x)]−1f(x) 其中,f′(x)f'(x ...
- MATLAB求解非线性方程组(牛顿拉夫逊方法)
文章目录 MATLAB求解非线性方程组(牛顿拉夫逊方法) Equation.m 函数 牛顿拉夫逊方法迭代求解 MATLAB求解非线性方程组(牛顿拉夫逊方法) Equation.m 函数 这个函数用来写 ...
- David Silver强化学习公开课自学笔记——Lec2马尔科夫决策过程
本笔记摘自知乎博主旺财的搬砖历险记和叶强,仅用于自学 0.数学规范 大写字母表示随机变量:S,A,RS,A,RS,A,R等 小写字母表示具体的值:s,a,rs,a,rs,a,r等 空心字母表示统计运算 ...
- 牛顿拉夫森法 matlab,【原创】牛顿-拉夫森迭代求非线性方程组
function [xf,fval,iter]=newtmullt(e,x0,es,maxit,varargin) % written by tubehu % www.MATLABsk ...
最新文章
- Vmware ESX server CPU掩码导致的挂起
- 小谈应聘.NET程序员失败的几点硬伤
- 几则常用的BASIS技巧整理
- LeetCode 287. 寻找重复数(BitMap)
- python小球弹弹弹_python实现小球弹跳效果
- 学习笔记之51单片机键盘篇(非编码键盘与编码键盘、非编码键盘的扫描方式、独立键盘、矩阵键盘)
- AT SELECTION-SCREEN
- centos7 设备 mariadb-10
- 雪花算法生成一个id
- STM32移植FATFS+USB+FLASH+PDFLIB库总结
- 无线收发模块——NRF24L01
- 前端jq实现视频跟图片一起混播
- windows无法连接到无线网络怎么办?
- 弹性公网IP、私有IP、浮动IP、虚拟IP之间有何区别?
- 分享124个ASP源码,总有一款适合您
- Python爬虫之自动下载图片
- 进程的概念与基本介绍
- Fabric官方教程(release 2.2)翻译及总结——链码的生命周期管理
- C#解析ip.ws.126.net的IP查询地区接口数据(使用正则表达式匹配获取所需数据)
- 软件测试学习笔记——性能理论知识