Scientific Computing of math homework
Math 382 Homework 7 [corrected] Total 60 Points
1. Write a python function cobweb(f, x0, n, xmin, xmax, ymin, ymax) to draw a cobweb plot of fixed point iteration, where
f name of function to plot. x0 starting point value of x. n number of fixed point iterations.
xmin minimum value for x-axis on plot xmax maximum value for x-axis on plot ymin minimum value for y-axis on plot ymax maximum value for y-axis on plot
This is a reminder of what a typical cobweb plot looks like (not for the function you are working with though, so your cobweb will look different).
Demonstrate that your cobweb plot works with the function call
cobweb(cos, 1.0, 200,0, 1.5, 0, 1)
2. Explore the convergence of the logistic map using your cobweb plot.
Run the function you wrote in problem 1 using the function f(x) = rx(1 − x) by defining the following three functions in python:
f(x) r*x*(1-x)
g(x) f(f(x))
h(x) f(f(f(x))
In particular, explore what happens by plotting the cobwebs for values of r as it varies from 3.8 to 3.85. You may need to make 20 to 30 plots over this region and compare them as r is varied to see what is happening. First look at F, then G, then H. What do you think is happening, and why do you think it is happening? Is there some range of values it is occurring over? You will be graded on how you justify your answer.
1
3. The Fourier series of a function f(x) on an interval [0,P] is given by
F(x) ∼ a0 2
+ n∑
k=1
[ an cos
( 2πkx
P
) + bn sin
( 2πkx
P
)] (1)
where
a0 = 2
P
∫ P 0
f(x)dx (2)
ak = 2
P
∫ P 0
f(x) cos
( 2πkx
P
) dx, k ≥ 1 (3)
bk = 2
P
∫ P 0
f(x) sin
( 2πkx
P
) dx (4)
(a) Write a python function fourier coefficients(f, P, n) that will return two lists a and b containing the coefficients [a0,a1,a2, . . . ,an ] and [0,b1,b2, . . . ,bn] respectively.
You may use the following code to find the integral
∫ xmax xmin
f(x) dx:
def trapez_integration(f, xmin,xmax, n=1000):
h=(float(xmax)-float(xmin))/float(n)
xvals = [xmin+i*h for i in range(n+1)]
fvals = [f(x) for x in xvals]
integral=0.5*h*sum( [p+q for p,q in zip(fvals[1:], fvals[:-1])] )
return integral
(b) Using the function f(x) = 1 − x and P = 1, plot the Fourier series for F(x) on the interval [−1, 2] using n = 5, 10, 25 and 100. What do you observe as n becomes larger? The squiggles that remain near the discontinuities are called Gibb’s phenomenon.
2