# Plotting an Expression that is a Summation in Mathematica

I was solving the Helmholtz equation using Mathematica and got as an output an expression which is a summation. I first want to turn this expression into a function of $$x$$ and $$y$$. I then want to use Mathematica to plot this function in the domain $$D=[-3,3] \times [-3,3]$$ but I cannot figure out how to do this. My code to produce the expression is:

eqn = Laplacian[bottom[x, y], {x, y}] + bottom[x, y] == 0;
bc4 = {bottom[-3, y] == 0, bottom[3, y] == 0,
bottom[x, -3] == Sin[(\[Pi]x)/6] - Csc[3/Sqrt] Sin[x/Sqrt],
bottom[x, 3] == 0};
sol4 = DSolveValue[{eqn, bc4}, bottom[x, y], {x, y}] // FullSimplify


This outputs:

Inactive[Sum][-((2 Csch[Sqrt[-36 + \[Pi]^2 K^2]] ((-1)^K (1 +
(-1)^K) \[Pi]^2 K^2 - (-1 + (-1)^K) (-18 + \[Pi]^2 K^2)
Sin[\[Pi]x/6]) Sin[1/6 \[Pi] (3 + x) K] Sinh[1/6 (-3 + y) Sqrt[-36
+ \[Pi]^2 K^2]])/(\[Pi] K (-18 + \[Pi]^2 K^2))), {K, 1, \
[Infinity]}]


Now I tried using:

ans[x_,y_] = Evaluate[sol4];
Plot3D[answer, {x, -3, 3}, {y, -3, 3}, ColorFunction -> "TemperatureMap"]


But the plot that's produced just shows nothing. I'm assuming my issue is that the Evaluate function isn't doing what I want it to do but I'm not quite sure what else to use - any thoughts? Do I need to activate the summation?

• I don't expect that infinite sum to be easily plottable as is. Instead, I would suggest doing something like Plot3D[Evaluate[Activate[sol4 /. Infinity -> 10]], {x, -3, 3}, {y, -3, 3}, ColorFunction -> "TemperatureMap"] instead to see an approximation. Dec 12, 2021 at 19:05
• hmm I'm still getting just a blank plot Dec 12, 2021 at 19:12
• Your bottom[x, -3] condition as currently written is faulty, apparently. Don't forget to separate multiplied variables with a space! bottom[x, -3] == Sin[(π x)/6] - Csc[3/Sqrt] Sin[x/Sqrt] Dec 12, 2021 at 19:15
• Oh thanks for pointing that out, I just edited that and still nothing changed though. Dec 12, 2021 at 19:24
• After you correct the Pix to Pi x, the summation includes a $\frac{1}{1-K^2}$ term with K the summation index so it's indeterminate when K=1. Also, the sum is "inactivated" for some reason I don't know. So I first explicitly change K=1 to just k=2 for now (don't know how that would change the solution though), change the upper limit to say 100, and then activate it: mySol = sol4 /. {K -> k, [Infinity] -> 100} mySol[] = {k, 2, 100} myf[x_, y_] = Activate@mySol; Plot3D[myf[x, y], {x, -3, 3}, {y, -3, 3}] And I get a plot which looks reasonable
– josh
Dec 12, 2021 at 20:16

another option instead of skipping the first term is to take its real part. (can easily change this below to takes its absolute value also and compare).

Clear["Global*"]
eqn = Laplacian[bottom[x, y], {x, y}] + bottom[x, y] == 0;
bc4 = {bottom[-3, y] == 0, bottom[3, y] == 0,
bottom[x, -3] == Sin[(Pi*x)/6] - Csc[3/Sqrt] Sin[x/Sqrt],
bottom[x, 3] == 0};
sol4 = DSolveValue[{eqn, bc4}, bottom[x, y], {x, y}] // FullSimplify;
sol4 = sol4 /. K -> n;
lim = Limit[sol4[], n -> 1];
sol = Sum[Piecewise[{{Re[lim], n == 1}, {sol4[], n > 1}}], {n, 1,10}];

p1 = Plot3D[sol, {x, -3, 3}, {y, -3, 3},
ColorFunction -> "TemperatureMap",
PlotLabel -> "Analytical solution", ImageSize -> 300];

solN = NDSolve[{eqn, bc4}, bottom, {x, -3, 3}, {y, -3, 3}];
p2 = Plot3D[Evaluate[bottom[x, y] /. solN], {x, -3, 3}, {y, -3, 3},
ColorFunction -> "TemperatureMap",
PlotLabel -> "numerical solution", ImageSize -> 300];

Grid[{{p1, p2}}] Increase the summation index to get more accurate analytical solution. Now it uses only 10 terms.

This is what I propose:

The solution is:

$$\tiny \underset{K=1}{\overset{\infty }{\sum }}-\frac{2 \left((-1)^{K}+1\right) K \left(\frac{\pi ^2 (-1)^{K}}{\pi ^2 K^2-18}+\frac{1}{1-K^2}\right) \text{csch}\left(\sqrt{\pi ^2 K^2-36}\right) \sin \left(\frac{1}{6} \pi (x+3) K\right) \sinh \left(\frac{1}{6} (y-3) \sqrt{\pi ^2 K^2-36}\right)}{\pi }$$

and note there is the term $$\frac{1}{1-K^2}$$ with $$K$$ the summation index which is indeterminate at $$K=1$$. However, the limit at $$K=1$$ exists:

$$\lim_{K->1} \text{sol4}=i \csc \left(\sqrt{36-\pi ^2}\right) \cos \left(\frac{\pi x}{6}\right) \sin \left(\frac{1}{6} \sqrt{36-\pi ^2} (y-3)\right)$$

so that we we are left with a complex solution but the PDE is linear so the real and imaginary components of sol4 are solutions. So I propose taking the sum starting with k=2 as one solution (the real component), and the imaginary component of the limit above as a second solution. Note back-substituting the imaginary component of that limit also satisfies the PDE.

Code for real sol:

eqn = Laplacian[bottom[x, y], {x, y}] + bottom[x, y] == 0;
bc4 = {bottom[-3, y] == 0, bottom[3, y] == 0,
bottom[x, -3] == Sin[(\[Pi] x)/6] - Csc[3/Sqrt] Sin[x/Sqrt],
bottom[x, 3] == 0};
sol4 = DSolveValue[{eqn, bc4}, bottom[x, y], {x, y}] // FullSimplify
mySol = sol4 /. {K -> k, \[Infinity] -> 100}
mySol[] = {k, 2, 100}
myF[x_, y_] = Activate@mySol;
Plot3D[myF[x, y], {x, -3, 3}, {y, -3, 3}]
`