Question
In Python, There are several iterative methods to calculate pi. A particularly fast method is the iteration: P(1) = 3.0 P(n + 1) = P(n)
In Python, 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))
This algorithm is a particular case of a method called Fixed Point Iteration
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.
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.
Write a higher-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
Any Help Would Be Greatly Appreciated, Thanks.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started