需要帮助这个Python作业 [英] Need help with this Python homework

查看:109
本文介绍了需要帮助这个Python作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

家庭作业#2:快速烘焙Pi



有几种迭代方法可以计算pi。一个特别快速的方法是迭代:



P(1)= 3.0



P(n + 1)= P(n)+ sin(P(n))



...其中P(n)是迭代'n'处的pi的近似值。给定pi的第一个估计P(1)为3.0,这个迭代收敛到pi的近似值,调整到你调用sin()时允许的精度数字。



P(1)= 3 + sin(3)= 3.1411200 ......



P(2)= 3.14112 + sin(3.14112) = 3.141592654 ...



第二次迭代后,误差非常小(大约1.8 * 10 ^( - 11))。并且,在下一次迭代中,误差降至约10 ^( - 32)。为什么它可以工作?



这个算法是一个称为定点迭代的方法的特例,用于找到形式方程的解:



x = f(x)[1]







在这种特殊情况下,我们有f(x)= x + sin(x),并且,对于任何整数n,sin(n * pi)= 0,pi的任何倍数都是该等式的解。 br />


类似[1]的等式有时(通常是?)可以通过迭代公式来解决:



x [n + 1] = f(x [n])



其中x [n]是解的第n个近似值。有一个着名的定理表明,如果导数f'(x)的绝对值小于某个数L <1,则该方法将收敛于解。 1在包含解决方案的某个时间间隔内,如果以该时间间隔内的值开头。导数越小,序列收敛得越快。在f(x)= x + sin(x)的情况下,我们得到:f'(x)= 1 + cos(x),如果x接近pi,则cos(x)接近-1,并且f'(x)接近于0,这解释了为什么该方法快速收敛。我们还可以注意到f(x),二阶导数是-sin(x),它在根处为0。这意味着你可以在相对较大的pi区间内开始,并且仍然可以获得非常快速的收敛。



我们可以看到这是牛顿方法的一个小修改,应用于方程sin(x)= 0的解。牛顿方法给出了公式:x [ n + 1] = x [n] - sin(x [n])/ cos(x [n]和,因为cos(x [n])接近-1,你可以通过替换cos得到你的公式(x [ n])在上面的公式中为-1。



1.确定牛顿方法或定点方法是否更适合计算pi(最多15位精度) )通过计算每个需要收敛的迭代次数。



2.确定牛顿方法或定点方法是否更适合计算Dottie数(0.739085 ... ),这是cos()函数的唯一实数固定点。



3.包括讨论你的分析的评论问题。



N.B.您可以从教科书中使用和修改牛顿方法的代码。您应该编写一个高阶函数来计算迭代定点迭代,如下所示。



def fixed_point_iteration(f,x = 1.0):



step = 0



而不是about_fixed_point(f,x):



x = f(x)



步+ = 1



返回x,步







def approx_fixed_point(f,x)应该返回True当且仅当f(x)是非常接近(距离<1e-15)到x。







样本运行:



>>来自数学进口罪,cos



>>> print(fixed_point_iteration(lambda x:sin(x)+ x,3.0)



(3.141592653589793,3)#固定点pi:



>>> print(newton_find_zero(lambda x:sin(x),lambda x:cos(x),3.0)



(3.141592653589793,3)#Newtons's pi







>>> print( fixed_point_iteration(lambda x:cos(x),1.0)



(0.7390851332151611,86)#固定点dottie



>>> print(newton_find_zero(lambda x:cos(x) - x,lambda x:-sin(x)-1,1.0)



(0.7390851332151606,7)#牛顿的dottie



我的尝试:



我不知道从哪里开始,因为我不确定这个问题是什么让我这么做

解决方案

仔细阅读问题。

它很好地解释了这个方法,然后问你问题:

Quote:

1。通过计算每个需要收敛的迭代次数来确定牛顿方法或定点方法是否更适合计算pi(最多15位精度)。



2。确定牛顿方法或定点方法是否更适合计算Dottie数(0.739085 ...),这是cos()函数的唯一实数固定点。



3.包括讨论您对此问题的分析的评论。



NB您可以从教科书中使用和修改牛顿方法的代码。您应该编写一个高阶函数来计算迭代定点迭代,如下所示。

如果您仍然不知道从哪里开始,那么您需要与您的导师交谈,而不是我们。


Homework #2: Quick Baked Pi

There are several iterative methods to calculate pi. A particularly fast method is the iteration:

P(1) = 3.0

P(n + 1) = P(n) + sin(P(n))

...where P(n) is the approximation of pi at iteration 'n'. Given a first estimate P(1) of pi as '3.0', this iteration converges to an approximation of pi correct to as many digits of precision that you are allowed when you call sin().

P(1) = 3 + sin(3) = 3.1411200...

P(2) = 3.14112 + sin(3.14112) = 3.141592654...

After the second iteration, the error is very small (about 1.8*10^(-11)). And, on the next iteration, the error drops to about 10^(-32). Why does it work?

This algorithm is a particular case of a method called fixed point iteration, and is used to find solutions to equations of the form:

x = f(x) [1]



In this particular case, we have f(x) = x + sin(x), and, as sin(n*pi) = 0 for any integer n, any multiple of pi is a solution of that equation.

An equation like [1] can sometimes (often?) be solved by iterating the formula:

x[n+1] = f(x[n])

where x[n] is the nth approximation of the solution. There is a famous theorem which states that the method will converge to the solution if the absolute value of the derivative f'(x) is less than some number L < 1 in some interval containing the solution, and if you start with a value in that interval. The smaller the derivative is, the faster the sequence will converge. In the case of f(x) = x + sin(x), we have: f'(x) = 1 + cos(x) and, if x is close to pi, cos(x) is close to -1, and f'(x) is close to 0, which explains why the method converges rapidly. We can also notice that f"(x), the second derivative, is -sin(x), which is 0 at the root. This means that you can start in a relatively large interval about pi, and still get a very fast convergence.

We can see this as a slight modification of Newton's method applied to the solution of the equation sin(x) = 0. Newton's method gives the formula: x[n+1] = x[n] - sin(x[n])/cos(x[n] and, as cos(x[n]) is close to -1, you get your formula by replacing cos(x[n]) by -1 in the above formula.

1. Determine if Newton's method or the fixed point method is better for computing pi (up to 15 digits of accuracy) by counting how many iterations each needs to converge.

2. Determine if Newton's method or the fixed point method is better for computing the Dottie number (0.739085...), which is the unique, real number fixed point of the cos() function.

3. Include comments that discuss your analysis of this problem.

N.B. You can use and modify the code for Newton's method from the textbook. You should write a higer-order function to compute an iterative fixed point iteration as follows.

def fixed_point_iteration(f, x=1.0):

step = 0

while not approx_fixed_point(f, x):

x = f(x)

step += 1

return x, step



def approx_fixed_point(f, x) should returns True if and only if f(x) is very close (distance < 1e-15) to x.



Sample Run:

>> from math import sin,cos

>>> print(fixed_point_iteration(lambda x: sin(x) + x, 3.0)

(3.141592653589793, 3) # Fixed point pi:

>>> print(newton_find_zero(lambda x: sin(x) , lambda x: cos(x), 3.0)

(3.141592653589793, 3) # Newtons's pi



>>> print(fixed_point_iteration(lambda x: cos(x), 1.0)

(0.7390851332151611, 86) # Fixed point dottie

>>> print(newton_find_zero(lambda x: cos(x) - x , lambda x: -sin(x)-1, 1.0)

(0.7390851332151606, 7) # Newtons's dottie

What I have tried:

I am not sure where to begin as I am not sure what the question is askig me to do

解决方案

Read the question carefully.
It's explains the method very well, and then asks you questions:

Quote:

1. Determine if Newton's method or the fixed point method is better for computing pi (up to 15 digits of accuracy) by counting how many iterations each needs to converge.

2. Determine if Newton's method or the fixed point method is better for computing the Dottie number (0.739085...), which is the unique, real number fixed point of the cos() function.

3. Include comments that discuss your analysis of this problem.

N.B. You can use and modify the code for Newton's method from the textbook. You should write a higer-order function to compute an iterative fixed point iteration as follows.

If you still don't understand where to start then you need to talk to your tutor, not us.


这篇关于需要帮助这个Python作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆