(*^ ::[ Information = "This is a Mathematica Notebook file. It contains ASCII text, and can be transferred by email, ftp, or other text-file transfer utility. It should be read or edited using a copy of Mathematica or MathReader. If you received this as email, use your mail application or copy/paste to save everything from the line containing (*^ down to the line containing ^*) into a plain text file. On some systems you may have to give the file a name ending with ".ma" to allow Mathematica to recognize it as a Notebook. The line below identifies what version of Mathematica created this file, but it can be opened using any other version as well."; FrontEndVersion = "Macintosh Mathematica Notebook Front End Version 2.2"; MacintoshStandardFontEncoding; fontset = title, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, e8, 24, "Times"; fontset = subtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, e6, 18, "Times"; fontset = subsubtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, italic, e6, 14, "Times"; fontset = section, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, grayBox, M22, bold, a20, 18, "Times"; fontset = subsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, blackBox, M19, bold, a15, 14, "Times"; fontset = subsubsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, whiteBox, M18, bold, a12, 12, "Times"; fontset = text, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 14, "Times"; fontset = smalltext, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 10, "Times"; fontset = input, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeInput, M42, N23, bold, L-5, 12, "Courier"; fontset = output, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L-5, 12, "Courier"; fontset = message, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, R65535, L-5, 12, "Courier"; fontset = print, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L-5, 12, "Courier"; fontset = info, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, B65535, L-5, 12, "Courier"; fontset = postscript, PostScript, formatAsPostScript, output, inactive, noPageBreakInGroup, nowordwrap, groupLikeGraphics, M7, l34, w283, h137, 12, "Courier"; fontset = name, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, italic, 10, "Geneva"; fontset = header, inactive, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = leftheader, inactive, L2, 12, "Times"; fontset = footer, inactive, noKeepOnOnePage, preserveAspect, center, M7, 12, "Times"; fontset = leftfooter, inactive, L2, 12, "Times"; fontset = help, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 10, "Times"; fontset = clipboard, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = completions, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = special1, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = special2, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = special3, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = special4, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = special5, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; paletteColors = 128; currentKernel; ] :[font = section; inactive; Cclosed; preserveAspect; cellOutline; backColorRed = 58981; backColorGreen = 58981; backColorBlue = 58981; startGroup] Setup: :[font = input; preserveAspect; endGroup] Needs["Algebra`Trigonometry`"] Needs["Calculus`FourierTransform`"] Needs["Graphics`Graphics`"] :[font = section; inactive; Cclosed; preserveAspect; cellOutline; backColorRed = 58981; backColorGreen = 58981; backColorBlue = 58981; startGroup] Problem 1: Fourier Transforms by trial and error :[font = text; inactive; preserveAspect] The goal of this exercise is to give the students a feeling for the Fourier transform by giving them the task of constructing a specified "goal" function using a series of sine waves. :[font = subsection; inactive; preserveAspect; cellOutline; startGroup] Setup: :[font = input; preserveAspect] Clear["Global`*"]; :[font = text; inactive; preserveAspect] This just defines some simple functions so that the repetitive task of comparing our sine series with the goal function is simplified. :[font = input; preserveAspect] fun[c0_,c1_,c2_,c3_]:= ( c0 + c1 Sin[ Pi 1 x] + c2 Sin[ Pi 2 x] + c3 Sin[ Pi 3 x] ) :[font = input; preserveAspect; endGroup] doit[c0_,c1_,c2_,c3_]:= Plot[ {goal[x],fun[c0,c1,c2,c3]} //Evaluate ,{x,-1,1} ,PlotStyle->{RGBColor[1,0,0],RGBColor[0,0,1]} ]; :[font = subsection; inactive; preserveAspect; cellOutline; startGroup] Part a) goal[x]= Ceiling[x]-1/2 ;[s] 2:0,0;18,1;33,-1; 2:1,16,12,Times,1,14,0,0,0;1,13,10,Courier,1,12,0,0,0; :[font = text; inactive; preserveAspect] Here is our goal function. :[font = input; preserveAspect] Clear[goal]; goal[x_]=Ceiling[x *0.99]-1/2 Plot[goal[x] ,{x,-1,1}]; :[font = text; inactive; preserveAspect] Here I plot out the different terms in our series, one at a time, and compare this with the goal function. :[font = input; preserveAspect] doit[1,0,0,0]; :[font = input; preserveAspect] doit[0,1,0,0]; :[font = input; preserveAspect] doit[0,0,1,0]; :[font = input; preserveAspect] doit[0,0,0,1]; :[font = text; inactive; preserveAspect; fontColorRed = 65535] Your challenge: try and reproduce the goal function as closely as possible by hand. Then have a look at the "answer" given by NFourierTrigSeries. ;[s] 4:0,0;127,1;145,0;146,1;147,-1; 2:2,16,12,Times,0,14,65535,0,0;2,13,10,Courier,1,12,65535,0,0; :[font = input; preserveAspect] doit[0,0.5,0.5,0.5]; :[font = text; inactive; preserveAspect] Here is the answer. (I'll hide this.) :[font = input; inactive; preserveAspect; endGroup] NFourierTrigSeries[goal[x],{x,-1,1},3] //Chop :[font = subsection; inactive; preserveAspect; cellOutline; startGroup] Part b) goal[x]= x :[font = text; inactive; preserveAspect; fontColorRed = 65535] Your challenge: try and reproduce the goal function as closely as possible by hand. Then have a look at the "answer" given by NFourierTrigSeries. ;[s] 4:0,0;127,1;145,0;146,1;147,-1; 2:2,16,12,Times,0,14,65535,0,0;2,13,10,Courier,1,12,65535,0,0; :[font = input; preserveAspect] Clear[goal]; goal[x_]=x Plot[goal[x] ,{x,-1,1}]; :[font = input; preserveAspect] doit[0,0.5,0.5,0.5]; :[font = input; inactive; preserveAspect; endGroup] NFourierTrigSeries[goal[x],{x,-1,1},3] //Chop :[font = subsection; inactive; preserveAspect; cellOutline; startGroup] Part c) goal[x]= 1-Abs[x] ;[s] 2:0,0;18,1;27,-1; 2:1,16,12,Times,1,14,0,0,0;1,13,10,Courier,1,12,0,0,0; :[font = text; inactive; preserveAspect; fontColorRed = 65535] Your challenge: try and reproduce the goal function as closely as possible by hand. Then have a look at the "answer" given by NFourierTrigSeries. ;[s] 4:0,0;127,1;145,0;146,1;147,-1; 2:2,16,12,Times,0,14,65535,0,0;2,13,10,Courier,1,12,65535,0,0; :[font = input; preserveAspect] Clear[goal]; goal[x_]=1-Abs[x] Plot[goal[x] ,{x,-1,1}]; :[font = input; preserveAspect; endGroup] doit[0,0.5,0.5,0.5]; :[font = input; inactive; preserveAspect; endGroup] NFourierTrigSeries[goal[x],{x,-1,1},3] //Chop :[font = section; inactive; Cclosed; preserveAspect; cellOutline; backColorRed = 58981; backColorGreen = 58981; backColorBlue = 58981; startGroup] Example 2: Fourier expansion of a saw-tooth :[font = input; preserveAspect] Clear["Global`*"] :[font = subsection; inactive; preserveAspect] Part a) :[font = text; inactive; preserveAspect] Here is our goal function: f[x]. :[font = input; preserveAspect] Clear[f]; f[x_]:=x /; x <= N[Pi]; f[x_]:=0 /; x >= N[Pi]; :[font = input; preserveAspect] Plot[f[x],{x,0,2 Pi}]; :[font = text; inactive; preserveAspect] We'll do the first coefficient by hand since this is a special case. :[font = input; preserveAspect] c[0]= 1/(2 Pi) Integrate[ x ,{x,0,1 Pi}] :[font = text; inactive; preserveAspect] The rest of the coefficients are given by: :[font = input; preserveAspect] c[m_]= 1/(2 Pi) Integrate[ x Exp[- I m x] ,{x,0,1 Pi}] :[font = text; inactive; preserveAspect] Here's a rule to help Mathematica simplify our expressions. ;[s] 3:0,0;22,1;33,0;62,-1; 2:2,16,12,Times,0,14,0,0,0;1,16,12,Times,2,14,0,0,0; :[font = input; preserveAspect] rule={Exp[+2 I Pi m] -> 1 ,Exp[-2 I Pi m] -> 1 ,Exp[+ I Pi m] -> (-1)^m ,Exp[- I Pi m] -> (-1)^m }; :[font = text; inactive; preserveAspect] Given the coefficients, we can now create our series. :[font = input; preserveAspect] (* Sometimes we have to work out special cases to avoid divide by zero and other problems. *) term[0]= c[0]; :[font = input; preserveAspect] term[1]= Limit[ c[m] Exp[I m x] + c[-m] Exp[-I m x] ,m->1] :[font = input; preserveAspect] term[m_]= c[m] Exp[I m x] + c[-m] Exp[-I m x] //.rule //Together :[font = text; inactive; preserveAspect] Sum the series. :[font = input; preserveAspect] series[n_]:= Sum[ term[m] ,{m,0,n}] :[font = text; inactive; preserveAspect] and plot it for a range of terms. :[font = input; preserveAspect] Plot[ Join[{f[x]},Table[series[i],{i,2,5,1}] ] //Evaluate ,{x,0,2 Pi}]; :[font = text; inactive; preserveAspect] Even for 30 terms, we have trouble describing the sharp features of our function. :[font = input; preserveAspect] Plot[ {f[x], series[30] } //Evaluate ,{x,0,2 Pi}]; :[font = text; inactive; preserveAspect] While we have taken this to be an exponential series, it is trivial to convert this into a Trig series. :[font = input; preserveAspect] term2[0] = term[0] //ComplexToTrig //Simplify; term2[1] = term[1] //ComplexToTrig //Simplify; :[font = input; preserveAspect] term2[m_]= term[m] //ComplexToTrig //Simplify :[font = input; preserveAspect] series2[n_]:= Sum[ term2[m] ,{m,0,n}] :[font = input; preserveAspect] series[5] :[font = input; preserveAspect; endGroup] series2[5] :[font = section; inactive; preserveAspect; cellOutline; backColorRed = 58981; backColorGreen = 58981; backColorBlue = 58981] Problem 3: Repeat above, with the function f[x]=x over x=[0, 2 Pi]. :[font = section; inactive; Cclosed; preserveAspect; cellOutline; backColorRed = 58981; backColorGreen = 58981; backColorBlue = 58981; startGroup] Example 4: Relate continous and discrete Fourier Transform :[font = text; inactive; preserveAspect] This is a simple exercise to help student understand the extension of a discrete Fourier transform to a continous one. :[font = input; preserveAspect] Clear["Global`*"] :[font = text; inactive; preserveAspect] Pick some Fourier coefficients: :[font = input; preserveAspect] c={5,0,2,0,3}; BarChart[c,PlotRange->{{0, 10},All}]; :[font = text; inactive; preserveAspect] Make a Fourier Series: :[font = input; preserveAspect] Clear[f]; f[x_]= Sum[ c[[n]] Sin[n x] ,{n,1,5}] :[font = text; inactive; preserveAspect] Make a data table from this function. :[font = input; preserveAspect] data= Table[f[x] //N,{x,0,2 Pi,2 Pi/2^7}]; data //Short :[font = text; inactive; preserveAspect] Plot the Fourier Series: :[font = input; preserveAspect] ListPlot[data ,PlotJoined->True]; :[font = text; inactive; preserveAspect] Fourier Transform Series: :[font = input; preserveAspect] fdata=Fourier[data] //Abs //Chop; fdata //Short :[font = input; preserveAspect] (* Note, I Dropped the c_0 element so that the numbers match up better; i.e., c_1 is at 1, ... Compare this with first plot. *) BarChart[fdata //Drop[#,1]& ,PlotRange->{{0, 10},All} ]; :[font = text; inactive; preserveAspect] Compare with: :[font = input; preserveAspect] BarChart[c,PlotRange->{{0, 10},All}]; :[font = text; inactive; preserveAspect] Same thing with ListPlot instead. (ListPlot is more general for our purposes.) ;[s] 3:0,0;35,1;79,0;80,-1; 2:2,16,12,Times,0,14,0,0,0;1,16,12,Times,2,14,0,0,0; :[font = input; preserveAspect] ListPlot[fdata //Drop[#,1]& ,PlotJoined->True ,PlotRange->{{0,10},All} ,AxesOrigin->{0,0} ]; :[font = input; preserveAspect; endGroup] LinearLogListPlot[fdata //Drop[#,1]& ,PlotJoined->True ,PlotRange->All ]; :[font = section; inactive; preserveAspect; cellOutline; backColorRed = 58981; backColorGreen = 58981; backColorBlue = 58981] Problem 5: Repeat above, with c= Table[Exp[-n^2/5],{n,1,5}] ^*)