We will fit two functions, exp(x) and cos(cos(x)) over the interval [0,2π) using the fourier series
a (1)
n=1
As you know from earlier courses, the coefficients an and bn are given by
adx
an dx
bn dx
- Define Python functions for the two functions above, that take a vector (or scalar) input, and return avector (or scalar) value. Plot the functions over the interval [−2π,4π) in Figure 1 and 2 respectively. Determine whether the functions are periodic. What function do you expect to be generated by the fourier series? Compute and plot those functions as well in the respective figures.
Note: Since exp(x) grows rapidly, use semilogy for that plot.
Note: Add grids and labels to figures.
- Obtain the first 51 coefficients for the two functions above. Note: The built in integrator in Python integrates a scalar function from a to b. So you will have to compute the coefficients in a for loop. Also note that you will have to create two new functions to be integrated, namely u(x,k) = f(x)cos(kx) and v(x,k) = f(x)sin(kx). To integrate these, use the option in quad to pass extra arguments to the function being integrated:
rtnval=quad(u,0,2*pi,args=(k))
What this does is it accepts a function u(x,…); the integration is over x, but the k values is passed to the function by quad as the second argument. So you will have to define the two functions as having k as their second (not first) argument.
- For each of the two functions, make two different plots using “semilogy” and “loglog” and plot themagnitude of the coefficients vs n. The values should be plotted with red circles. Note that the answer should be a vector of the form
a0
a1
b1
…
a25 b25
Note: Plot the coefficients for f1(x) = exp(x) in Figures 3 and 4, and the coefficients for f2(x) = cos(cosx) in Figures 5 and 6.
- If you did Q1 correctly, the bn coefficients in the second case should be nearly zero. Why does this happen?
- In the first case, the coefficients do not decay as quickly as the coefficients for the second case.Why not?
- Why does loglog plot in Figure 4 look linear, wheras the semilog plot in Figure 5 looks linear?
- We instead do a “Least Squares approach” to the problem. Define a vector x going from 0 to 2π in 400 steps (remember linspace). Evaluate the function f(x) at those x values and call it b. Now this function is to be approximated by Eq. (1). So for each xi we want
25 25a0+ ∑ an cosnxi + ∑ bn sinnxi ≈ f(xi) | (2) | ||||
Turn this into a matrix problem: | n=1 | n=1 | |||
1 cosx1 sinx1 1 cosx2 sinx2 … … …1 cosx400 sinx400 | ……… … | a01cos25x2 sin25x2 …b1 = … … cos25x400 sin25x400 a25 b25 | f(x1) f(x2) … | | |
f(x400) |
Create the matrix on the left side and call it A. We want to solve
Ac = b
where c are the fourier coefficients.
Note: The matrix can be constructed using zeros((400,51)) and then filling in the columns in a for loop. Do not use a double for loop. Here is the way to build up x, b and A without using nested loop:
x=linspace(0,2*pi,401) x=x[:-1] # drop last term to have a proper periodic integral b=f(x) # f has been written to take a vector A=zeros((400,51)) # allocate space for A
A[:,0]=1 # col 1 is all ones for k in range(1,26):
A[:,2*k-1]=cos(k*x) # cos(kx) column
A[:,2*k]=sin(k*x) # sin(kx) column
#endfor
c1=lstsq(A,b)[0] # the ’[0]’ is to pull out the # best fit vector. lstsq returns a list.
- Use lstsq to solve this problem. You execute c=lstsq(A,b)[0]
What this does is to find the “best fit” numbers that will satisfy Eq. (2) at exactly the points we have evaluated f(x). Obtain the coefficients for both the given functions. Plot them with green circles in the corresponding plots.
- Compare the answers got by least squares and by the direct integration. Do they agree? Should they?How much deviation is there (find the absolute difference between the two sets of coefficients and find the largest deviation. How will you do this using vectors?)
- Compute Ac from the estimated values of c. These should be the function values at xi. Plot them (with green circles) in Figures 1 and 2 respectively for the two functions. Why is there so much deviation in Figure 1 but nearly perfect agreement in Figure 2?
- Write a report on this assignment in latex and submit the same along with your code.
Reviews
There are no reviews yet.