g=Plot[{Sin[x]Cos[0],Sin[x]Cos[Pi/4],Sin[x]Cos[Pi/2],Sin[x]Cos[3Pi/4],Sin[x]Cos[Pi]},{x,-1,4Pi+1},PlotStyle->{Black,Blue,Green,Orange,Red},Axes->False,AspectRatio->1/4]

Export[NotebookDirectory[]<>"onda-estacionaria.png",g,"PNG",ImageResolution->200,Background->None]

(*Propagating waves*)

ff[x_]:=If[x<0,0,If[x<0.3,x/0.3,If[x<1,-x/0.7+1/0.7,0]]]
gg[x_]:=0*If[x<0,0,If[x<0.3,x/0.3,If[x<1,-x/0.7+1/0.7,0]]]

xmax=9;
Animate[Plot[{ff[x-tt]+gg[x+tt],ff[x-tt],gg[x+tt]},{x,-xmax,xmax+1},PlotRange->{-1.25,1.25},PlotStyle->{Black,{Red,Dashed},{Blue,Dashed}},Axes->False,AspectRatio->1/3],{tt,-xmax+2,xmax-1},AnimationRunning->False,DisplayAllSteps->True]

(*Propagating waves-finite string*)

ff[x_]:=If[x<0,0,If[x<0.3,x/0.3,If[x<1,-x/0.7+1/0.7,0]]]
gg[x_]:=-ff[-x]

L=6;
Animate[{g1=Plot[{ff[x-tt]+ff[x+2L-tt]+ff[x+4L-tt],gg[x+tt]+gg[x-2L+tt]+gg[x-4L+tt]},{x,-L,2L},PlotRange->{-3.75,1.25},PlotStyle->{{Red,Dashed},{Blue,Dashed}},Axes->False,AspectRatio->1/3];,g2=Plot[{ff[x-tt]+ff[x+2L-tt]+ff[x+4L-tt]+gg[x+tt]+gg[x-2L+tt]+gg[x-4L+tt]}-2.5,{x,0,L},PlotStyle->{Black},Axes->False];,Show[g1,g2]},{tt,L/2,2L+L/2},AnimationRunning->False,DisplayAllSteps->True]

fff[x_]:=If[x<-L/2,0,If[x<L/2,x,If[x<3L/2,L-x,0]]]
ggg[x_]:=fff[x]

L=6;
Animate[{g1=Plot[{fff[x-2L-tt]+fff[x-tt]+fff[x+2L-tt]+fff[x+4L-tt],ggg[x+2L+tt]+ggg[x+tt]+ggg[x-2L+tt]+ggg[x-4L+tt]},{x,-L,2L},PlotRange->{-3.75L,1.25L},PlotStyle->{{Red,Dashed},{Blue,Dashed}},Axes->False,AspectRatio->1/3];,g2=Plot[{fff[x-tt]+fff[x+2L-tt]+fff[x+4L-tt]+ggg[x+tt]+ggg[x-2L+tt]+ggg[x-4L+tt]}-2.5L,{x,0,L},PlotStyle->{Black},Axes->False];,Show[g1,g2]},{tt,0,2L},AnimationRunning->False,DisplayAllSteps->True]

(*Normal modes*)

y[x_,t_,min_,max_]:=8/(Pi^2)Sum[(-1)^(n+1)Sin[(2n-1)Pi x]Cos[(2n-1)Pi t]/(2n-1)^2,{n,min,max}]

g=Plot[{1-2Abs[x-1/2],y[x,0,1,1],y[x,0,2,2],y[x,0,3,3],y[x,0,4,4]},{x,0,1},PlotStyle->{{Black},{Red,Dashed},{Blue,Dashed},{Orange,Dashed},{Green,Dashed}}]

Export[NotebookDirectory[]<>"Fourier1.png",g,"PNG",ImageResolution->200,Background->None]

g=Plot[{1-2Abs[x-1/2],y[x,0,1,1],y[x,0,1,2],y[x,0,1,3],y[x,0,1,4]},{x,0,1},PlotStyle->{{Black},{Red},{Blue},{Orange},Green}]

Export[NotebookDirectory[]<>"Fourier2.png",g,"PNG",ImageResolution->200,Background->None]

Animate[Plot[{1-2Abs[x-1/2],y[x,0,1,nn]},{x,0,1},PlotStyle->{{Black},{Red}},PlotLabel->{"N=",nn}],{nn,1,21,2},AnimationRunning->False,DisplayAllSteps->True]

g1=Plot[{1-2Abs[x-1/2]},{x,0,1},PlotStyle->{{Black}}]
g2=Plot[{y[x,0,1,1],y[x,0,1,2],y[x,0,1,10]},{x,-1,2},PlotStyle->{{Red},{Blue},{Orange},Green}]
g=Show[g2,g1]

Export[NotebookDirectory[]<>"Fourier3.png",g,"PNG",ImageResolution->200,Background->None]

Animate[Plot[{y[x,tt,1,1],y[x,tt,2,2],y[x,tt,3,3],y[x,tt,4,4],y[x,tt,1,15]-2},{x,0,1},PlotRange->{-3.15,1.25},PlotStyle->{{Red,Dashed},{Blue,Dashed},{Orange,Dashed},{Green,Dashed},Black},Axes->False,AspectRatio->1/3],{tt,0,2},AnimationRunning->False,DisplayAllSteps->True]

f[x_,d_]:=If[Abs[x-1/2]<d,1-Abs[x-1/2]/d,0]

Plot[f[x,0.2],{x,0,1},PlotRange->{-1,1}]

(*Normal modes: yn = sin[kn x] * (an cos[wn t] + bn sin[wn t]) *)
(*kn L = n Pi   =>   kn = n Pi   (SET L = h = 1) *)
(* wn = c kn    (SET c = 1)*)
(* an = (-1)^((n-1)/2)(1-(-1)^n)*4*sin^2[n*Pi*d/2]/(d(n*Pi)^2)  *)
(* bn*wn = h c/ d * (-1)^(n/2)(1+(-1)^n)*sin^2[n*Pi*d/2]/(n*Pi/2)  *)
(*Note that an = 0 for n even and bn = 0 for n odd*)

a=Table[If[Mod[n,2]==1,Integrate[f[x,d]*Sin[Pi*n*x],{x,0,1},Assumptions->0<d<1/2],0],{n,1,3}]

b=Table[If[Mod[n,2]==1,(-1)^((n-1)/2)4/d Sin[Pi n d/2]^2/(n Pi)^2,0],{n,1,7}]
c=Table[Cos[(n-1)Pi]4/d Sin[Pi (2n-1) d/2]^2/((2n-1) Pi)^2,{n,1,4}]

FullSimplify[a-b]

ff[x_,d_,nmax_]:=2Sum[Cos[(n-1)Pi]4/d Sin[Pi (2n-1) d/2]^2/((2n-1) Pi)^2*Sin[Pi(2n-1)x],{n,1,nmax}]

d=0.05;
Plot[{f[x,d],ff[x,d,17]},{x,0,1},PlotRange->{-1,1}]
Clear[d]

Sign[-1]

a=Table[Integrate[If[Abs[x-1/2]<d,Sign[x-1/2],0]*Sin[Pi*n*x],{x,0,1},Assumptions->0<d<1/2],{n,1,8}]

Table[Cos[n Pi]2/(n Pi)Sin[n Pi d]^2,{n,1,4}]

y[x_,t_,d_,nmax_]:=2Sum[Cos[(n-1)Pi]4/d Sin[Pi (2n-1) d/2]^2/((2n-1) Pi)^2*Sin[Pi(2n-1)x] Cos[Pi(2n-1)t],{n,1,nmax}]+2/d Sum[Cos[n Pi]Sin[Pi n d]^2/(n Pi)^2*Sin[Pi 2n x]Sin[Pi 2n t],{n,1,nmax}]

Animate[Plot[y[x,tt,0.1,27],{x,0,1},PlotRange->{-1.5,1.5}],{tt,0,2},AnimationRunning->False]

(*Introduce dispersion: wn = kn Sqrt(1+alpha k^2)*)

pts=Import[NotebookDirectory[]<>"wave-packet-dispersion-e=0.1.dat","MAT"];
pts2=Import[NotebookDirectory[]<>"wave-packet-dispersion-e=0.3.dat","MAT"];

Animate[ListPlot[{Table[{pts[[x,t,2]],pts[[x,t,3]]},{x,1,251}],Table[{pts2[[x,t,2]],pts2[[x,t,3]]},{x,1,251}],Table[{x,Sqrt[2Pi]Exp[-(x-(t-1)/20)^2/2]},{x,-5,20,0.1}]},PlotRange->{-1,2.5},Axes->False,Joined->True,PlotStyle->{{Thick,Blue},{Thick,Red},{Dashed,Black,Thick}}],{t,1,401,10},AnimationRunning->False,DisplayAllSteps->True]

Int1=Table[Sum[Abs[pts[[x,t,3]]],{x,1,251}],{t,1,401}];
Int2=Table[Sum[Abs[pts2[[x,t,3]]],{x,1,251}],{t,1,401}];
x1=Table[Sum[pts[[x,t,2]]*Abs[pts[[x,t,3]]],{x,1,251}]/Int1[[t]],{t,1,401}];
x2=Table[Sum[pts2[[x,t,2]]*Abs[pts2[[x,t,3]]],{x,1,251}]/Int2[[t]],{t,1,401}];
xx1=Table[Sum[(pts[[x,t,2]])^2*Abs[pts[[x,t,3]]],{x,1,251}]/Int1[[t]],{t,1,401}];
xx2=Table[Sum[(pts2[[x,t,2]])^2*Abs[pts2[[x,t,3]]],{x,1,251}]/Int2[[t]],{t,1,401}];
Dx1=Table[Sqrt[xx1[[t]]-x1[[t]]^2],{t,1,401}];
Dx2=Table[Sqrt[xx2[[t]]-x2[[t]]^2],{t,1,401}];

ListPlot[{Table[{(t-1)/20,x1[[t]]},{t,1,180}],Table[{(t-1)/20,x2[[t]]},{t,1,100}],Table[{(t-1)/20,(t-1)/20},{t,1,180}]},Joined->True,PlotStyle->{{Thick,Blue},{Thick,Red},{Black,Thick,Dashed}},AxesLabel->{"Tempo","<x>"}]
ListPlot[{Table[{(t-1)/20,Dx1[[t]]},{t,1,180}],Table[{(t-1)/20,Dx2[[t]]},{t,1,100}],Table[{(t-1)/20,1},{t,1,180}]},Joined->True,PlotStyle->{{Thick,Blue},{Thick,Red},{Black,Thick,Dashed}},AxesLabel->{"Tempo","Dx"}]

ListPlot[{Table[{(t-1)/20,Int1[[t]]},{t,1,401}],Table[{(t-1)/20,Int2[[t]]},{t,1,401}]},Joined->True,PlotStyle->{{Thick,Blue},{Thick,Red}},AxesLabel->{"Tempo","Dx"}]

yy[x_,t_,d_,nmax_,alpha_]:=2Sum[Cos[(n-1)Pi]4/d Sin[Pi (2n-1) d/2]^2/((2n-1) Pi)^2*Sin[Pi(2n-1)x] Cos[Pi(2n-1)Sqrt[1+alpha(Pi (2n-1))^2]t],{n,1,nmax}]+2/d Sum[Cos[n Pi]Sin[Pi n d]^2/(n Pi)^2*Sin[Pi 2n x]Sin[Pi 2n Sqrt[1+alpha(Pi 2n)^2]t],{n,1,nmax}]

d=0.02;alpha=10^(-7);Plot[{yy[x,0,d,77,alpha],yy[x,18,d,77,alpha]},{x,0,1},PlotRange->{-1.5,1.5}]
Clear[d,alpha]

Animate[Plot[yy[x,tt,0.05,47,0.00001],{x,0,1},PlotRange->{-1.5,1.5}],{tt,0,2},AnimationRunning->False]

yyy[x_,t_,d_,nmin_,nmax_,alpha_]:=2Sum[Cos[(n-1)Pi]4/d Sin[Pi (2n-1) d/2]^2/((2n-1) Pi)^2*Sin[Pi(2n-1)x] Cos[Pi(2n-1)Sqrt[1+alpha(Pi (2n-1))^2]t],{n,nmin,nmax}]+2/d Sum[Cos[n Pi]Sin[Pi n d]^2/(n Pi)^2*Sin[Pi 2n x]Sin[Pi 2n Sqrt[1+alpha(Pi 2n)^2]t],{n,nmin,nmax}]

yyyy[x_,t_,d_,nmin_,nmax_,alpha_]:=2Sum[Cos[(n-1)Pi]4/d Sin[Pi (2n-1) d/2]^2/((2n-1) Pi)^2*Sin[Pi(2n-1)x] Cos[Pi(2n-1)Sqrt[1+alpha(Pi (2n-1))^2]t],{n,nmin,nmax}]

dy=0.3;
A=3;
t=0;
d=0.1;
alpha=0.000001;
g=Plot[{yyy[x,t,d,1,199,alpha], yyyy[x,t,d,4,4,alpha]-dy,2A yyyy[x,t,d,8,8,alpha]-dy,9A yyyy[x,t,d,12,12,alpha]-dy},{x,0.1,0.9},Axes->False,PlotStyle->{{Thick,Black},{Thick,Red},{Thick,Brown},{Thick,Green}},PlotRange->All]

Export[NotebookDirectory[]<>"Disperion-a.png",g,"PNG",ImageResolution->200,Background->None]

dy=0.3;
A=3;
t=12;
d=0.1;
alpha=0.000001;
g=Plot[{yyy[x,t,d,1,199,alpha], yyyy[x,t,d,4,4,alpha]-dy,2A yyyy[x,t,d,8,8,alpha]-dy,9A yyyy[x,t,d,12,12,alpha]-dy},{x,0.1,0.9},Axes->False,PlotStyle->{{Thick,Black},{Thick,Red},{Thick,Brown},{Thick,Green}},PlotRange->All]

Export[NotebookDirectory[]<>"Disperion-b.png",g,"PNG",ImageResolution->200,Background->None]

Plot[{Cos[k-Sqrt[0.5+k^2]],Sin[k-Sqrt[0.5+k^2]]},{k,-2Pi,2Pi}]

(*Klein-Gordon: wn = Sqrt[w0^2+kn^2]*)

yyy[x_,t_,d_,nmax_,w0_]:=2Sum[Cos[(n-1)Pi]4/d Sin[Pi (2n-1) d/2]^2/((2n-1) Pi)^2*Sin[Pi(2n-1)x] Cos[Sqrt[w0^2+(Pi (2n-1))^2]t],{n,1,nmax}]+2/d Sum[Cos[n Pi]Sin[Pi n d]^2/(n Pi)^2*Sin[Pi 2n x]Sin[Sqrt[w0^2+(Pi 2n)^2]t],{n,1,nmax}]

d=0.3;
w0=8 *10^(-1);
nmax=37;
Plot[{yyy[x,0,d,nmax,w0],yyy[x,20,d,nmax,w0]},{x,0,1},PlotRange->{-1.5,1.5}]
Clear[d,w0,nmax]

Animate[Plot[yyy[x,tt,0.05,37,17],{x,0,1},PlotRange->{-1.5,1.5}],{tt,0,20},AnimationRunning->False]

(*Beats: Gravity waves*)

psi[x_,t_,k1_,k2_,w1_,w2_]:=Cos[k1 x - w1 t]+Cos[k2 x - w2 t]

l1=0.5;
l2=0.58;
k1=2Pi/l1;
k2=2Pi/l2;
w1=k1;
w2=k2;
w1=Sqrt[k1];
w2=Sqrt[k2];
cf=(w1+w2)/(k1+k2);
cg=(w1-w2)/(k1-k2);
xmax=1*4Pi/Abs[k1-k2];
tmax=xmax/Min[cg,cf] ;
Animate[Show[Plot[{psi[x,tt,k1,k2,w1,w2](*,2Cos[((k1-k2)x-(w1-w2)tt)/2],-2Cos[((k1-k2)x-(w1-w2)tt)/2]*)},{x,0,xmax},PlotRange->{-3,3},PlotPoints->100,Axes->False,PlotStyle->{Black,Thick(*,{Red,Dashed},{Red,Dashed}*)}],Graphics[{PointSize[Large],Black,Point[{Mod[(w1+w2)tt/(k1+k2),xmax],psi[(w1+w2)tt/(k1+k2),tt,k1,k2,w1,w2]}]}],Graphics[{PointSize[Large],Red,Point[{Mod[(w1-w2)tt/(k1-k2)+Pi/Abs[k1-k2],xmax],psi[(w1-w2)tt/(k1-k2)+Pi/Abs[k1-k2],tt,k1,k2,w1,w2]}]}]],{tt,0,2tmax},AnimationRunning->False]
(*Clear[l1,l2,w1,w2]*)