Skip to content

Instantly share code, notes, and snippets.

@BrianWeinstein
Created January 27, 2015 04:47
Show Gist options
  • Save BrianWeinstein/7c38a5040f7eb1b56b04 to your computer and use it in GitHub Desktop.
Save BrianWeinstein/7c38a5040f7eb1b56b04 to your computer and use it in GitHub Desktop.
ic[x_, y_] := 1 E^(-350 ((x - 1/5)^2 + ( y - 1/3)^2))
solnDir =
NDSolve[
{D[u[x, y, t], {t, 2}] == D[u[x, y, t], {x, 2}] + D[u[x, y, t], {y, 2}],
u[x, y, 0] == ic[x, y],
(D[u[x, y, t], t] /. t -> 0) == 0,
u[0, y, t] == ic[0, y],
u[1, y, t] == ic[1, y],
u[x, 0, t] == ic[x, 0],
u[x, 1, t] == ic[x, 1]
},
u, {x, 0, 1}, {y, 0, 1}, {t, 0, 2},
PrecisionGoal -> 2
]
solnNeu =
NDSolve[
{D[u[x, y, t], {t, 2}] == D[u[x, y, t], {x, 2}] + D[u[x, y, t], {y, 2}],
u[x, y, 0] == ic[x, y],
(D[u[x, y, t], t] /. t -> 0) == 0,
(D[u[x, y, t], x] /. x -> 0) == 0,
(D[u[x, y, t], x] /. x -> 1) == 0,
(D[u[x, y, t], y] /. y -> 0) == 0,
(D[u[x, y, t], y] /. y -> 1) == 0
},
u, {x, 0, 1}, {y, 0, 1}, {t, 0, 2},
PrecisionGoal -> 2
]
fDir[x_, y_, t_] := Evaluate[u[x, y, t] /. solnDir[[1, 1]]]
fNeu[x_, y_, t_] := Evaluate[u[x, y, t] /. solnNeu[[1, 1]]]
Manipulate[
Plot3D[
Switch[BC, Dir, fDir[x, y, t], Neu, fNeu[x, y, t]],
{x, 0, 1}, {y, 0, 1},
PlotRange -> {-1, 1}, Axes -> False,
ColorFunction -> ColorData["BlueGreenYellow"],
FaceGrids ->
{
{{-1, 0, 0}, {Range[0, 1, 0.2], Range[-1, 1, 0.5]}},
{{0, 1, 0}, {Range[0, 1, 0.2], Range[-1, 1, 0.5]}},
{{0, 0, -1}, {Range[0, 1, 0.2], Range[0, 1, 0.2]}}
},
ViewPoint -> {1.58, -2.72, 1.02}
],
{{t, 0.5}, 0, 2}, {{BC, Dir, "Boundary Condition"}, {Dir -> "Dirichlet", Neu -> "Neumann"}}
]
@PeterChin
Copy link

I do physics but can't quite code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment