BTM 9.7.8

Define our target function on the interval [-1,1]:

f[x_] = Exp[-Abs[x]]

Plot[f[x],{x,-1,1}]

Skip the following cell on the first pass; later on come back and repeat with:

(*target function 2: *) f[x_] = Cosh[x]

Choose the exp() basis functions:

ϕ[n_,x_] = (1/Sqrt[2]) Exp[π I n x]

Check that we normalized them correctly:

Integrate[ϕ[n,x]*ϕ[-n,x],{x,-1,1}]

And go compute the coefficients.  For the homework, you want to do it “by hand,” i.e. computing two integrals, one from -1 to 0 where the target function is Exp[x], and one from 0 to 1 where the function changes to Exp[-x].  You know the steps:

indefinite1= Integrate[Exp[x]*ϕ[-m,x],x]

definite1 = (indefinite1/.x->0)-(indefinite1/.x->-1)

indefinite2= Integrate[Exp[-x]*ϕ[-m,x],x]

definite2 = (indefinite2/.x->1)-(indefinite2/.x->0)

total = definite1+definite2

And of course by hand we’d automatically simplify using our knowledge that “m” is an integer:

Simplify[total,Assumptions->Element[m,Integers]]

Or, here on a computer we can hide the details as we ask for everything in one line:

fm[m_] = Simplify[Integrate[f[x]*ϕ[-m,x],{x,-1,1}],Assumptions->Element[m,Integers]]

Here we define the function which approximates our target f(x) by a finite sum over modes:

g[n_,x_] := Sum[fm[m]*ϕ[m,x],{m,-n,n}]

Plot[{f[x],g[10,x]},{x,-1,1},PlotStyle->{Hue[.7],Hue[0]}]

If the goal is to express the answer in terms of sines and cosines, then we can just expand the Exp[I m x]’s:

ExpToTrig[fm[m]ϕ[m,x]+fm[-m]ϕ[-m,x]]//Simplify[#,Assumptions->Element[m,Integers]]&

Or we can go back to square one, defining these properly normalized basis functions:

ϕc[n_,x_] = Cos[n π x]
ϕs[n_,x_] = Sin[n π x]

(*checking normalization etc*)Integrate[ϕc[1,x]^2,{x,-1,1}]
Integrate[ϕc[2,x]*ϕs[1,x],{x,-1,1}]

But oops the m=0 Cos (i.e. the constant mode) needs fixing:

Integrate[ϕc[0,x]^2,{x,-1,1}]

ϕc[0,x_] = 1/Sqrt[2]

Here are the coefficients for generic n:

Integrate[ϕc[n,x]*f[x],{x,-1,1}]
a[n_] = Simplify[%,Assumptions->Element[n,Integers]]

Integrate[ϕs[n,x]*f[x],{x,-1,1}]
b[n_] =Simplify[%,Assumptions->Element[n,Integers]]

And the special n=0 mode:

a[0]= Integrate[ϕc[0,x]*f[x],{x,-1,1}]

And finally assemble the modes to check everything is working:

g2[n_,x_] := a[0]*ϕc[0,x]+Sum[a[m]*ϕc[m,x]+b[m]*ϕs[m,x],{m,1,n}]

Plot[{f[x],g[5,x],g2[5,x]},{x,-1,1},PlotStyle->{Hue[.7],Hue[0],Hue[.3]}]

BTM 9.7.12/13

Start with BTM 9.7.12.  Define a starting shape for the string:

f[x_] = 2x UnitStep[(1/2)-x] + 2(1-x)UnitStep[x-(1/2)]

Plot[f[x],{x,0,1}]

Fourier decompose in terms of sin(), which we check is properly normalized

ϕ[n_,x_] = Sqrt[2] Sin[ π n x]

Integrate[ϕ[12,x]^2,{x,0,1}]

Get the coefficients:

a[n_] = Integrate[ϕ[n,x]f[x],{x,0,1}]

Notice that all the even terms vanish:

Table[a[n],{n,1,10}]

So one can write that one in terms of a sum over odd to save computation time:

g[n_,x_] := Sum[a[j]*ϕ[j,x],{j,1,n,2}]

Plot[{f[x],g[15,x]},{x,0,1}]

And then animate:

ψ[n_,x_,t_] := Sum[a[j]ϕ[j,x]Cos[j π t],{j,1,n}]

frames=Table[Plot[ψ[15,x,t],{x,0,1},PlotRange->{{0,1},{-1,1}}],{t,.1,2,.1}];

ListAnimate[frames]

Your job: repeat with the target function of problem 9.7.13.

Visualizing Functions of a Complex Variable

Before proceeding, download Mario’s package, open it and “Run Package”.
This defines this function:

?CPlot

Here is the basic color map:

z0 = 2+2I;
CPlot[z,{z,-z0,z0}]

Here is the function z^2, which has a double zero (twice around the rainbow) at 0:

CPlot[z^2,{z,-z0,z0}]

And here is a simple pole at z=1/2

CPlot[1/(z-1/2)^2,{z,-z0,z0}]

Here is a polynomial with 4 distinct roots:

Solve[{z^4+z^2+1==0}]

So 1/polynomial has poles at those 4 locations:

CPlot[1/(z^4+z^2+1),{z,-z0,z0}]

The cube roots of z0=(1+I) are:

cuberoots = Solve[z^3==1+I]//ComplexExpand

To pick one root:

z1 = z/.cuberoots[[1]]

when we take powers we get:

z1^2//Expand

and of course:

z1*%//Expand

Here is the sine function extended to the complex plane.

CPlot[Sin[z],{z,-5-5I,5+5I}]

Note all the zeros are on the real axis, something you could see by looking at the real/imaginary pieces of:

ComplexExpand[Sin[x+I y]]

(Since we need both real and imaginary parts to vanish, and since Cosh[y] never vanishes (for real y), x must be one of those values where Sin[x]==0, i.e. where Cos[x]=+1 or -1.  Either way, that means Sinh[y] must vanish, i.e. y=0, i.e. real.)

The same expression is helpful in computing e.g. arcsin of 5

ArcSin[5]//ComplexExpand

And understanding why the Log term is in fact

ArcCosh[5]//ComplexExpand

Here is the complex plot which shows some of the possible arcsin(5)'s.
Note there are two ArcCosh[5]'s, positive and negative.

CPlot[Sin[z]-5,{z,-4π(1+I),4π(1+I)}]

Synthesizing Sound ala Moog

Here are some cells which you can listen to if you have earbuds.

(* Choose e.g. concert A 440 *) ν = 440

(*triangle*) f1[t_] = 1-Mod[ν t,1]
(*square*)     f2[t_] = Sign[Mod[ν t,1]-1/2]
(*another square*) f3[t_] = If[Mod[ν t,1]>3/4,1,1/4]

Plot[{f1[t],f2[t],f3[t]},{t,0,.01},PlotStyle->{Red,Green,Blue}]

Here is where we choose octaves and mix:

f[t_] = 1.0*f1[t]+1.1*f2[ t/2] + 2.1*f3[2 t]

Plot[f[t],{t,0,.02}]

And here we make a two-second chunk of sound to play

Play[f[t],{t,0,2}]

And finally we might shape the sound with attack and decay envelopes:

Tattack = 0.05
Tdecay = 0.25
shape[t_] = (1-Exp[-t/Tattack])Exp[-t/Tdecay]

Plot[shape[t],{t,0,2}]

Play[f[t]*shape[t],{t,0,1}]