Week 3: Introduction to LYX, Fourier Approximations

$30.00

Category: You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (4 votes)

The Assignment
We will fit two functions, exp(x) and cos(cos(x)) over the interval [0,2π) using the fourier series
a0 +


n=1
{an cos(nx) +bn sin(nx)} (1)
As you know from earlier courses, the coefficients an and bn are given by
a0 =
1

Z 2π
0
f(x)dx
an =
1
π
Z 2π
0
f(x) cos(nx)dx
bn =
1
π
Z 2π
0
f(x)sin(nx)dx
1. Define Python functions for the two functions above, that take a vector (or scalar) input, and return a
vector (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.
2. 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.
3. For each of the two functions, make two different plots using “semilogy” and “loglog” and plot the
magnitude 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


1
Note: Plot the coefficients for f1(x) = exp(x) in Figures 3 and 4, and the coefficients for f2(x) =
cos(cos x) in Figures 5 and 6.
(a) If you did Q1 correctly, the bn coefficients in the second case should be nearly zero. Why does
this happen?
(b) In the first case, the coefficients do not decay as quickly as the coefficients for the second case.
Why not?
(c) Why does loglog plot in Figure 4 look linear, wheras the semilog plot in Figure 5 looks linear?
4. 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
a0 +
25

n=1
an cosnxi +
25

n=1
bn sinnxi ≈ f(xi) (2)
Turn this into a matrix problem:


1 cosx1 sinx1 … cos 25×1 sin 25×1
1 cosx2 sinx2 … cos 25×2 sin 25×2
… … … … … …
1 cosx400 sinx400 … cos 25×400 sin 25×400




a0
a1
b1

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. Since you are not yet familiar with Python vector coding, here is
the way to build up x, b and A:
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.
5. Use lstsq to solve this problem (type lstsq? to see the help page). 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.
6. 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?)
2
7. 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?
8. Write a report on this assignment in LYX and submit the same, The code should be included by using
LYX Code environment (this takes your keystrokes as is and prints them in a fixed width font that is
suitable for program listings.)
Note: To include your program listing, first select the code in your editor. Then go to LYX and enter
the LYX code environment. Select Edit->paste special->plain text. The text will be inserted as is,
with indents etc.
We will go into the theory of Least Squares Estimation in the next week.
3