Mathematica examples relevant to PDEs:
part 1---Cartesian variables
These are the examples discussed in class, which are similar
to those on the HW
Example problem Sec 13.2 #2 (steady state temperature dependence)
Checking Fourier series representation of temperature along lower edge.
Clear[LL]; LL = 20;
Tlower[nmax_, x_] :=
SumSin[ Pi n x /∕ LL] 200 Cosn Pi 2 -− Cos[n Pi] Pi n, {n, 1, nmax}
Showing the convergence of the Fourier series with nmax=3, 10 and 100:
Plot[{Tlower[3, x], Tlower[10, x], Tlower[100, x]}, {x, 0, LL},
AxesLabel → {x, T}, PlotStyle → {{Thick, Black}, {Thick, Blue}, {Thick, Red}}]
T
120
100
80
60
40
20
5
10
15
20
x
Here’s the full result, with the sum truncated at nmax terms:
ttfull[nmax_, x_, y_] := Sum
Exp[-− n Pi y /∕ LL] Sin[ Pi n x /∕ LL] 200 Cosn Pi 2 -− Cos[n Pi] Pi n, {n, 1, nmax}
Here’s a “3D plot” showing the temperature with nmax=100.
Note that the temperature distribution smooths out quickly at y increases,
since the higher harmonics damp most quickly.
2
PDE1.nb
Plot3D[ttfull[100, x, y], {x, 0, LL}, {y, 0, 2 LL}, AxesLabel → {"x", "y"},
PlotRange → {-− 20, 120}, PlotLabel → "T(x,y)", LabelStyle → "Medium"]
This plot shows cross sections as y increases, showing the smoothness and the decay of the
temperature:
Plot[{ttfull[200, x, 1], ttfull[200, x, 5], ttfull[200, x, 10], ttfull[200, x, 20]},
{x, 0, LL}, AxesLabel → {x, T}, PlotLabel → "Temp profile at y=1, 5, 10 and 20",
PlotLegends → {"y=1", "y=5", "y=10", "y=20"}, LabelStyle → "Medium"]
T
Temp profile at y=1, 5, 10 and 20
80
y=1
60
y=5
y=10
40
y=20
20
5
10
15
20
x
This plot shows how, for y=5, only nmax =10 is needed (since the blue curve for nmax=10,
and the red curved for nmax=50, lie under the green curve for nmax=100)
PDE1.nb
Plot[{ttfull[2, x, 5], ttfull[10, x, 5], ttfull[50, x, 5], ttfull[100, x, 5]},
{x, 0, LL}, AxesLabel → {x, T},
PlotStyle → {{Thick, Black}, {Thick, Blue}, {Thick, Red}, {Thick, Green}},
PlotLabel → "Temp profile at y=5 for nmax=2, 10, 50, 100"]
T
Temp profile at y=5 for nmax=2, 10, 50, 100
30
20
10
5
10
15
20
x
Sec 13.3 #5 (Time dependence of temperature profile due to heat flow.)
Here’s the plot of time dependence, with alpha=1, and using nmax=100
uu5[nmax_, x_, t_] :=
100 + SumExp-− (2 n -− 1) Pi 2 ^ 2 t Sin(2 n -− 1) Pi 2 -− (2 n -− 1) Pi 2
Sin[(2 n -− 1) Pi x /∕ 2] 800 Pi (2 n -− 1) ^ 2, {n, 1, nmax}
Plot3D[uu5[100, x, t], {x, 0, 2}, {t, 0, 1},
AxesLabel → {x, t}, PlotRange → {0, 100}, LabelStyle → "Medium"]
Sec. 13.4 #4 (Wave equation)
Here is the result (with v=h=l=1)
In[104]:=
yy4[nmax_, x_, t_] :=
Sum16 2 Sinn Pi 8 -− Sinn Pi 4 Sin[n Pi x] Cos[n Pi t] n Pi ^ 2, {n, 1, nmax}
3
4
PDE1.nb
Here is the string shape at t=0---checking that the Fourier series coefficients are correct:
In[105]:=
Plot[yy4[100, x, 0], {x, 0, 1}, AxesLabel → {"x", "y"},
PlotStyle → {Thick, Red}, PlotLabel → "t=0", PlotRange → {-− 1, 1}]
t=0
y
1.0
0.5
Out[105]=
0.2
0.4
0.6
0.8
1.0
x
-−0.5
-−1.0
This animation shows the motion of the string for one full cycle.
Can use either “Animate” or “Manipulate”.
We see that this is nothing other than a travelling wave bouncing off the ends.
This command is very slow---better to use the approach below.
In[106]:=
Animate[Plot[yy4[100, x, t], {x, 0, 1}, AxesLabel → {"x", "y"},
PlotRange → {-− 1, 1}, PlotStyle → {Thick, Red}, PlotLabel → t],
{t, 0, 2}, AnimationRate → 0.05, AnimationRunning → False]
Some quick tips on speeding up your animations if they seem “jumpy”:
Instead of making Mathematica repeat the sum every time it calls the function, we just define a new
function as that sum. We allso use //N so that the function is in terms of floating point numbers instead
of exact numbers such as ratios of integers and pi. Note: having a function in terms of floating point
numbers can cause mistakes when large cancelations are required but typically it is fine.
In[107]:=
fasteryy4100[x_, t_] = yy4[100, x, t] /∕/∕ N;
PDE1.nb
In[108]:=
Animate[Plot[fasteryy4100[x, t], {x, 0, 1}, AxesLabel → {"x", "y"},
PlotRange → {-− 1, 1}, PlotStyle → {Thick, Red}, PlotLabel → t],
{t, 0, 2}, AnimationRate → 0.05, AnimationRunning → False]
t
0
y
1.0
0.5
Out[108]=
0.2
0.4
0.6
0.8
1.0
x
-−0.5
-−1.0
Here is the same example using Manipulate instead of Animate.
In[109]:=
Manipulate[Plot[fasteryy4100[x, t], {x, 0, 1}, AxesLabel → {"x", "y"},
PlotRange → {-− 1, 1}, PlotStyle → {Thick, Red}, PlotLabel → t], {t, 0, 2, .1}]
t
0
y
1.0
0.5
Out[109]=
0.2
0.4
0.6
0.8
1.0
x
-−0.5
-−1.0
Another option is to pre-render the frames of your animation. This can be slow initially but is useful if
you want to present an animation to someone else.
In[110]:=
animationframes = Table[Plot[fasteryy4100[x, t], {x, 0, 1}, AxesLabel → {"x", "y"},
PlotRange → {-− 1, 1}, PlotStyle → {Thick, Red}, PlotLabel → t], {t, 0, 2, .1}];
5
6
PDE1.nb
In[111]:=
ListAnimate[animationframes]
0.7
y
1.0
0.5
Out[111]=
0.2
0.4
0.6
0.8
1.0
x
-−0.5
-−1.0
You can even export the frames as a video file (see documentation for possible formats)
In[112]:=
Export["testvideo.avi", animationframes];
© Copyright 2025 Paperzz