Lab 5: Fourier Series in Maple Sample Solution
As per usual, we will start by clearing Maple's memory and setting up plotting:
Start by grabbing code from the Supplement
Since I will be needed to compute Fourier cosine series later on in this lab activity, I am going to copy those functions from the lab supplement to the begining of this worksheet.
| > |
assume(n,integer);assume(m,integer); |
| > |
c := (x,n) -> cos((2*Pi*n*x)/L); |
 |
(1.1.1) |
| > |
A:=proc(expr,var,n)
simplify(int(expr*c(var,n),var=0..L)/int(c(var,n)*c(var,n),var=0..L));
end proc; |
| > |
cosineFS:=proc(expr,var,n)
A(expr,var,0)+Sum(A(expr,var,m)*c(var,m),m=1..n);
end proc; |
| > |
cosineFP:=proc(expr,var,n)
A(expr,var,0)+add(A(expr,var,m)*c(var,m),m=1..n);
end proc; |
Part 1] Setting up the Fourier sine series routines
a. We are asked to develop routines for computing the Fourier sine series. These routines are very similar to the Fourier cosine series routines from the supplement, but tweaked in the appropriate fashion (notice there is no n=0 term in the Fourier sine series, or rather it goes to 0.):
| > |
s := (x,n) -> sin((2*Pi*n*x)/L); |
 |
(1.2.1) |
| > |
B:=proc(expr,var,n)
simplify(int(expr*s(var,n),var=0..L)/int(s(var,n)*s(var,n),var=0..L));
end proc; |
| > |
sineFS:=proc(expr,var,n)
Sum(B(expr,var,m)*s(var,m),m=1..n);
end proc; |
 |
(1.2.3) |
| > |
sineFP:=proc(expr,var,n)
add(B(expr,var,m)*s(var,m),m=1..n);
end proc; |
 |
(1.2.4) |
b. We are now asked to apply these Fourier cosine and sine series functions to solving for the Fourier series of f(x) = x - L/2.
 |
(1.2.5) |
I'll plot up the function to see what it looks like. Note that I am setting L = 1 now and that will hold into the future unless I change it. Notice I use a colon after setting L = 1 instead of a semi-colon, this suppresses the output of a line informing me I have set L = 1.
| > |
L:=1:plot(f(x),x=0..1); |
Let me compute the Fourier cosine and sine series, first showing everything up to the n = 10 order:
| > |
seq(A[n]=A(f(x),x,n),n=1..10); |
![A[1] = 0, A[2] = 0, A[3] = 0, A[4] = 0, A[5] = 0, A[6] = 0, A[7] = 0, A[8] = 0, A[9] = 0, A[10] = 0](images/lab05answers_19.gif) |
(1.2.6) |
| > |
cosineFS(f(x),x,infinity); |
 |
(1.2.7) |
Wow, the cosine series appears to make no contribution to this function!
| > |
seq(B[n]=B(f(x),x,n),n=1..10); |
![B[1] = `+`(`-`(`/`(1, `*`(Pi)))), B[2] = `+`(`-`(`/`(`*`(`/`(1, 2)), `*`(Pi)))), B[3] = `+`(`-`(`/`(`*`(`/`(1, 3)), `*`(Pi)))), B[4] = `+`(`-`(`/`(`*`(`/`(1, 4)), `*`(Pi)))), B[5] = `+`(`-`(`/`(`*`(`/...](images/lab05answers_21.gif)
![B[1] = `+`(`-`(`/`(1, `*`(Pi)))), B[2] = `+`(`-`(`/`(`*`(`/`(1, 2)), `*`(Pi)))), B[3] = `+`(`-`(`/`(`*`(`/`(1, 3)), `*`(Pi)))), B[4] = `+`(`-`(`/`(`*`(`/`(1, 4)), `*`(Pi)))), B[5] = `+`(`-`(`/`(`*`(`/...](images/lab05answers_22.gif) |
(1.2.8) |
| > |
sineFS(f(x),x,infinity); |
 |
(1.2.9) |
The
sine series on the other hand has every term, and it looks pretty
simple. Let's plot it up as suggested.
| > |
display(
[plot(f(x),x=0..L,color=red,thickness=2,title=sprintf("Function f(x)=%a and ...",f(x))),
seq(plot([f(x),sineFP(f(x),x,n)],x=0..L,color=[red,blue],thickness=2,numpoints=1000,
title=sprintf("... and its Fourier sine series with %d terms, biggest n=%d and ...",n,n)),n=1..20)
],scaling=constrained,insequence=true); |
Part 2] Checking the Fourier cosine and sine series of various functions
a. For each of the functions lised on the lab we are asked to plot the function from x = 0 to L and then determine if the sine/cosine series are nonzero. Let's look at all the functions sequentially.
NOTE: L = 1 for all of this since I never bothered to change it.
funca(x) = x(L-x)
 |
(1.3.1) |
Let me compute the Fourier cosine and sine series, first showing everything up to the n = 10 order:
| > |
seq(A[n]=A(funca(x),x,n),n=1..10); |
![A[1] = `+`(`-`(`/`(1, `*`(`^`(Pi, 2))))), A[2] = `+`(`-`(`/`(`*`(`/`(1, 4)), `*`(`^`(Pi, 2))))), A[3] = `+`(`-`(`/`(`*`(`/`(1, 9)), `*`(`^`(Pi, 2))))), A[4] = `+`(`-`(`/`(`*`(`/`(1, 16)), `*`(`^`(Pi, ...](images/lab05answers_27.gif)
![A[1] = `+`(`-`(`/`(1, `*`(`^`(Pi, 2))))), A[2] = `+`(`-`(`/`(`*`(`/`(1, 4)), `*`(`^`(Pi, 2))))), A[3] = `+`(`-`(`/`(`*`(`/`(1, 9)), `*`(`^`(Pi, 2))))), A[4] = `+`(`-`(`/`(`*`(`/`(1, 16)), `*`(`^`(Pi, ...](images/lab05answers_28.gif) |
(1.3.2) |
| > |
cosineFS(funca(x),x,infinity); |
 |
(1.3.3) |
| > |
seq(B[n]=B(funca(x),x,n),n=1..10); |
![B[1] = 0, B[2] = 0, B[3] = 0, B[4] = 0, B[5] = 0, B[6] = 0, B[7] = 0, B[8] = 0, B[9] = 0, B[10] = 0](images/lab05answers_30.gif) |
(1.3.4) |
| > |
sineFS(funca(x),x,infinity); |
 |
(1.3.5) |
So clearly this function as only Fourier cosine terms and no sine terms. Let's see how this forks (the following plot is NOT required for credit).
| > |
display(
[plot(funca(x),x=0..L,color=red,thickness=2,title=sprintf("Function funca(x)=%a and ...",funca(x))),
seq(plot([funca(x),cosineFP(funca(x),x,n)],x=0..L,color=[red,blue],thickness=2,numpoints=1000,
title=sprintf("... and its cosine series with %d terms, biggest n=%d and ...",n,n)),n=0..20)
],scaling=constrained,insequence=true); |
Now let me repeat these steps for the remaining functions
funcb(x) = (x - L/2)^3
| > |
funcb := x -> (x-L/2)^3; |
 |
(1.3.6) |
| > |
seq(A[n]=A(funcb(x),x,n),n=1..10); |
![A[1] = 0, A[2] = 0, A[3] = 0, A[4] = 0, A[5] = 0, A[6] = 0, A[7] = 0, A[8] = 0, A[9] = 0, A[10] = 0](images/lab05answers_35.gif) |
(1.3.7) |
| > |
cosineFS(funcb(x),x,infinity); |
 |
(1.3.8) |
| > |
seq(B[n]=B(funcb(x),x,n),n=1..10); |
| > |
sineFS(funcb(x),x,infinity); |
 |
(1.3.10) |
Clearly this function as only Fourier sine terms and no cosine terms. I wonder what is different about it versus the first function?
Another plot just to show the fits (this is not required for credit).
| > |
display(
[plot(funcb(x),x=0..L,color=red,thickness=2,title=sprintf("Function funcb(x)=%a and ...",funcb(x))),
seq(plot([funcb(x),sineFP(funcb(x),x,n)],x=0..L,color=[red,blue],thickness=2,numpoints=1000,
title=sprintf("... and its sine series with %d terms, biggest n=%d and ...",n,n)),n=0..20)
],scaling=constrained,insequence=true); |
funcc(x) = abs(x - L/2) [Be careful not to define c(x), since we are using it for the cosine term. Can you tell the mistake I made the first time I wrote this?]
| > |
funcc := x -> abs(x-L/2); |
 |
(1.3.11) |
| > |
seq(A[n]=A(funcc(x),x,n),n=1..10); |
![A[1] = `+`(`/`(`*`(2), `*`(`^`(Pi, 2)))), A[2] = 0, A[3] = `+`(`/`(`*`(`/`(2, 9)), `*`(`^`(Pi, 2)))), A[4] = 0, A[5] = `+`(`/`(`*`(`/`(2, 25)), `*`(`^`(Pi, 2)))), A[6] = 0, A[7] = `+`(`/`(`*`(`/`(2, 4...](images/lab05answers_44.gif) |
(1.3.12) |
| > |
cosineFP(funcc(x),x,1); |
 |
(1.3.13) |
| > |
cosineFS(funcc(x),x,infinity); |
 |
(1.3.14) |
| > |
seq(B[n]=B(funcc(x),x,n),n=1..10); |
![B[1] = 0, B[2] = 0, B[3] = 0, B[4] = 0, B[5] = 0, B[6] = 0, B[7] = 0, B[8] = 0, B[9] = 0, B[10] = 0](images/lab05answers_47.gif) |
(1.3.15) |
| > |
sineFS(funcc(x),x,infinity); |
 |
(1.3.16) |
Clearly this function as only Fourier cosine terms and no sine terms. I wonder what is similar between this function and the first function?
I'll go ahead and plot the fit of the sine and cosine series just to see what is happening (this is not required for credit).
| > |
display(
[plot(funcc(x),x=0..L,color=red,thickness=2,title=sprintf("Function funcc(x)=%a and ...",funcc(x))),
seq(plot([funcc(x),cosineFP(funcc(x),x,n)],x=0..L,color=[red,blue],thickness=2,numpoints=1000,
title=sprintf("... and its cosine series with %d terms, biggest n=%d and ...",n,n)),n=0..20)
],scaling=constrained,insequence=true); |
OK, now the final function in our hit parade...
funcd(x) is -1 for x < L/2 and +1 for x>L/2
| > |
funcd := x->piecewise(x<L/2,-1,x>L/2,+1); |
 |
(1.3.17) |
| > |
seq(A[n]=A(funcd(x),x,n),n=1..10); |
![A[1] = 0, A[2] = 0, A[3] = 0, A[4] = 0, A[5] = 0, A[6] = 0, A[7] = 0, A[8] = 0, A[9] = 0, A[10] = 0](images/lab05answers_52.gif) |
(1.3.18) |
| > |
cosineFP(funcd(x),x,1); |
 |
(1.3.19) |
| > |
cosineFS(funcd(x),x,infinity); |
 |
(1.3.20) |
| > |
seq(B[n]=B(funcd(x),x,n),n=1..10); |
![B[1] = `+`(`-`(`/`(`*`(4), `*`(Pi)))), B[2] = 0, B[3] = `+`(`-`(`/`(`*`(`/`(4, 3)), `*`(Pi)))), B[4] = 0, B[5] = `+`(`-`(`/`(`*`(`/`(4, 5)), `*`(Pi)))), B[6] = 0, B[7] = `+`(`-`(`/`(`*`(`/`(4, 7)), `*...](images/lab05answers_55.gif) |
(1.3.21) |
| > |
sineFS(funcd(x),x,infinity); |
 |
(1.3.22) |
So this function has no Fourier cosine terms, just sine terms. What makes this function the similar to funcb and different form funca and funcc?
Again, its not required, but it would be nice to plot this and see what is happening.
| > |
display(
[plot(funcd(x),x=0..L,color=red,thickness=2,title=sprintf("Function funcd(x)=%a and ...",funcd(x))),
seq(plot([funcd(x),sineFP(funcd(x),x,n)],x=0..L,color=[red,blue],thickness=2,numpoints=1000,
title=sprintf("... and its sine series with %d terms, biggest n=%d and ...",n,n)),n=0..20)
],scaling=constrained,insequence=true); |
b. We are asked "Describe what features of a function seem to be related to which of the series is nonzero."
In short, if the function is symmetric about the middle of the range (x=0.5), it will have a non-zero cosine series and if it is antisymmetric, it will have a non-sine series. For functions that are a mix of symmetric and anti-symmetric components, you will have a require a max of Fourier cosine and sine series to describe the function.