Here is another attempt. May be this is what is meant.
This plots solution for up to some fixed time. It has Manipulate to allow changing some of the parameters to see the resonance.
To play do
I do not see the amplitude being largest at x=L/3
when w=v*2*Pi*m/L
. But may be because time was not large enough. I had to make maximum time fixed, otherwise, NDSolve
will solve it again and again each time the slider moved and it become slow.
You can modify and play with it as needed. But I see resonance effect there at this frequency.
code
Manipulate[
Module[{A, L, k, u, x, t, maxTime = 40},
pde = D[u[x, t], {t, 2}] ==
v^2*D[u[x, t], {x, 2}] +
Piecewise[{{A*Sin[forcingFrequency*t], x == 0}, {0, True}}];
L = 2 Pi;
A = 5;
k = 2*Pi*m/L;
(*set up periodic BC*)
bc = {u[0, t] ==
u[L, t], (D[u[x, t], x] /. x -> 0) == (D[u[x, t], x] /. x -> L)};
bc = {u[0, t] == u[L, t]};
(*set up initial conditons BC. let it all be zero*)
ic = {u[x, 0] == 0, (D[u[x, t], t] /. t -> 0) == 0};
(*solve*)
sol = Quiet@NDSolve[{pde, bc, ic}, u, {x, 0, L}, {t, 0, maxTime}];
Grid[{{Plot3D[
Evaluate[u[x, t] /. sol], {x, 0, 2 Pi}, {t, 0, maxTime},
PlotRange -> All, AxesLabel -> {"x", "time", "u"},
ImageSize -> 300],
Plot[Evaluate[u[L/3, t] /. sol], {t, 0, maxTime},
PlotRange -> {Automatic, {-1, 10}}, ImageSize -> 300,
AxesLabel -> {"time", "Amplitude at x=L/3"}]}
}
]
],
{{forcingFrequency, 1, "forcing Frequency w"}, 0, 10, .1,
Appearance -> "Labeled"},
{{m, 2, "m"}, 0, 10, 1, Appearance -> "Labeled"},
{{v, 2.2, "v"}, 0, 10, .1, Appearance -> "Labeled"},
Grid[{{Button["Set at resonance",
forcingFrequency = v*2*m*Pi/(2*Pi)]}}],
TrackedSymbols :> {forcingFrequency, m, v}
]
Original answer
I do not understand this
I need to tell Mathematica that I inject a sine wave Asin(ωt) to some
location in the medium, say at x=0.
Initial conditions should depend on x
only. It can't have t
in it. In wave PDE, you give BC, and initial conditions. IC's are initial position and initial velocity. Both which can only depend on x, since t=0
.
Unless you mean there is an external source present. But your PDE as you wrote it, does not show this.
You can always plot the solution as follows. (No need to use numerical solver for this, as Mathematica can easily solve it analytically)
ClearAll[u, x, t];
pde = D[u[x, t], {t, 2}] == v^2*D[u[x, t], {x, 2}];
L = 2 Pi; A = 1; w=2;v=1;
(*set up periodic BC*)
bc = {u[0, t] == u[L, t], (D[u[x, t], x] /. x -> 0) == (D[u[x, t], x] /. x -> L)};
(*set up initial conditons BC. Give some initial configuration, and zero speed*)
ic = {u[x, 0] == A*Sin[w*x], (D[u[x, t], t] /. t -> 0) == 0};
(*solve*)
sol = u[x, t] /. First@DSolve[{pde, bc, ic}, u[x, t], {x, t}]
Manipulate[
Plot[sol /. t -> time, {x, 0, 2 Pi}, PlotRange -> {Automatic, {-1, 1}}],
{{time, 0, "time"}, 0, 10, .1, Appearance -> "Labeled"},
TrackedSymbols :> {time}
]
You can modify as needed and add more controls for other parameters if you want.
If you meant that $\sin(\omega t)$ is a source, then the PDE should include it. You can modify the code to be
ClearAll[u, x, t];
(*wave PDE with source term*)
pde = D[u[x, t], {t, 2}] == v^2*D[u[x, t], {x, 2}] + A*Sin[w*t];
L = 2 Pi; A = 1; k = 2; w = 2; v = 1;
(*set up periodic BC*)
bc = {u[0, t] ==
u[L, t], (D[u[x, t], x] /. x -> 0) == (D[u[x, t], x] /. x -> L)};
(*set up initial conditons BC. Give some initial configuration, and \
zero speed*)
ic = {u[x, 0] == A*Sin[w*x], (D[u[x, t], t] /. t -> 0) == 0};
(*solve*)
sol = NDSolve[{pde, bc, ic}, u, {x, 0, L}, {t, 0, 10}];
Manipulate[
Plot[Evaluate[u[x, t] /. sol] /. t -> time, {x, 0, 2 Pi},
PlotRange -> {Automatic, {-1, 10}}],
{{time, 0, "time"}, 0, 10, .1, Appearance -> "Labeled"},
TrackedSymbols :> {time}
]
Add controls as need to analyze solution. DSovle
could not solve the PDE with the source term added. May be in version 13 it can.