# Multiple Plots over non-consecutive integer parameter values

The following is a simple illustration of the difficulty I have encountered with multiple plots on the same diagram. The reason for trying to overcome (understand) the difficulty is more involved and is clarified after the illustration.

Consider two definitions of the same function as follows:

g[m_] = m E^(-m x^2)

gi[n_Integer] = n E^(-n x^2)


A plot of g[m] for several values of m simultaneously is possible with

Plot[g[m] /. m -> {1, 2, 3}, {x, -3, 3}, PlotRange -> All]


which results in

or ,

using Evaluate[g[m] /. m -> {1, 2, 3}, {x, -3, 3}] for the latter.

The same application of the Plot function, however, does not work with the second definition

Plot[gi[n] /. n -> {1, 2, 3}, {x, -3, 3}, PlotRange -> All]


Inserting Evaluate[g[m] /. m -> {1, 2, 3}, {x, -3, 3}] makes a difference and will make the plot lines distinct, however it still does not work with the second function gi[n], so I have left it out for simplicity.

The questions:

1. What causes the difference in plotting g[m_] and gi[n_Iinteger] as above?
2. How can Plot work with the second function definition gi[n_Integer], just as it does with the first g[m_]?

Clarification: I am aware of other methods for producing multiple plots in one diagram, such as constructing lists with Table and similar. The reason for pursuing the above approach for multiple plots with a list of values for a parameter, specified as in gi[n_Iinteger], is the possibility to extract non-consecutive terms from solutions of differential equations in the form of a sum. In the following example from the Mathematica Documentation

eqn = I  D[\[Psi][x, t], t] == -2 D[\[Psi][x, t], {x, 2}];
f[x_] := -350 + 155 x - 22 x^2 + x^3
sol = DSolve[{eqn, \[Psi][5, t] == 0, \[Psi][10, t] == 0, \[Psi][x, 2] == f[x]}, \[Psi], {x, t}]


the solution is a function as an infinite sum

,

from which a partial sum is extracted with

u[k_Integer] = \[Psi] /. Activate[First[sol] /. \[Infinity] -> k]


The above function is used to produce multiple plots with Table

...Table[Plot[{u[k][x, 2], f[x]}, {x, 5, 10}, ImageSize -> 200], {k, 4}]...


however, the plots are for consecutive values of k and not readily adaptable for extraction of individual terms and sums for non-consecutive k-values. Therefore, the summary question is

1. How can multiple plots be accomplished as illustrated for g[m_] above with a function specified as u[k_Integer]?

I shall be grateful for any help with these questions.

Clarification edit shifting emphasis to question 3. as the ultimate goal:

The first comment by Bob Hanlon suggested one successful approach to question 2., using Map (thank you Bob)! Unfortunately, I was unable to apply the same method to u[k_Integer] above - question 3. Consequently, I'd be grateful for responses related to the latter.

• Correct the spelling of Integer then Map the function onto the values. Plot[Evaluate[gi /@ {1, 2, 3}], {x, -3, 3}, PlotRange -> All] Aug 21, 2022 at 13:55
• Thank you Bob! Map worked with gi, however I was unsuccessful with applying it to u[k_Integer] in the same way. Aug 21, 2022 at 15:15

Clear["Global*"]

eqn = I D[ψ[x, t], t] == -2 D[ψ[x, t], {x, 2}];
f[x_] := -350 + 155 x - 22 x^2 + x^3
sol = DSolve[{eqn, ψ[5, t] == 0, ψ[10, t] == 0, ψ[x, 2] ==
f[x]}, ψ, {x, t}];

u[k_Integer?Positive] = ψ /. Activate[First[sol] /. ∞ -> k];


For k = {2, 4, 7}

Plot[Evaluate[u[#][x, 2] & /@ {2, 4, 7}], {x, 5, 10},
PlotLegends -> Placed[
LineLegend[{2, 4, 7}, LegendLabel -> k],
{.7, .65}],
Frame -> True,
FrameLabel -> (Style[#, 14] & /@
{x, HoldForm[u[k][x, 2]]})]


EDIT: For two parameters

u2[k1_Integer?Positive, k2_Integer?Positive] = ψ /.
Activate[First[sol] /.
{{K[1], 1, Infinity} :> {K[1], k1, k2}}];

kValues = {{1, 2}, {1, 4}, {3, 7}};

Plot[Evaluate[u2[#[[1]], #[[2]]][x, 2] & /@ kValues],
{x, 5, 10},
PlotLegends -> Placed[LineLegend[ToString /@ kValues,
LegendLabel -> {Subscript[k, 1], Subscript[k, 2]}],
{.2, .3}],
Frame -> True,
FrameLabel ->
(Style[#, 14] & /@ {x,
HoldForm[u2[Subscript[k, 1], Subscript[k, 2]][x, 2]]})]


• Thanks again, Bob! This is clear and completes the answer quite well. If I am not overstepping the kindness of your efforts, would you, please, help with extending for two parameters, u[k1_Integer,k2_integer], specifically how to present two lists at the mapping /@ accordingly. The purpose is to be able to specify arbitrary sets of values for the lower and upper limits of the solution sum. Gratefully - Aug 21, 2022 at 18:41
• Or you could use Evaluate[(u2 @@ #)[x, 2] & /@ kValues] Aug 21, 2022 at 19:19
• That’s absolutely great. Not only it does the job beautifully, I learned a lot, too. Wonderful help! Aug 21, 2022 at 19:27

Consider: g[m] /. m -> {1, 2, 3} This will evaluate to: g[{1,2,3}] and further: {1, 2, 3} E^(-{1, 2, 3} x^2) and finally to: {E^-x^2, 2 E^(-2 x^2), 3 E^(-3 x^2)}

However, gi[n] /. n -> {1, 2, 3} evaluates only to: gi[{1, 2, 3}] and it can not evaluete further because {1,2,3}has a head: List and not "Integer".

If you want to use gi[n] /. n -> {1, 2, 3}, you must give gi the attribute "Listable"

Clear["Global*"]
SetAttributes[gi, Listable];
gi[n_Integer] = n E^(-n x^2);
Plot[Evaluate[gi[n] /. n -> {1, 2, 3}], {x, -3, 3}, PlotRange -> All]


The third question is a bit more complex because you have 2 variables and the functions are complex. To make a simple example I will plot the Abs value and define u[k,t,x]:

SetAttributes[u, Listable]
u[k_Integer, t_,
x_] := (\[Psi] /. Activate[First[sol] /. \[Infinity] -> k] )[t, x]

Plot3D[Evaluate@Abs[u[{1, 2}, x, y]], {x, -1, 1}, {y, -1, 1}]


• Thank you very much Daniel. Not only it works but also helped me understand the significance of Listable in this situation. Appreciated Aug 21, 2022 at 19:16
• Apologies for returning to the discussion. Just for completeness, what would be the reason for the SetAtribute to Listable approach, you suggested to work with the gi and not for the u[k_Integer] as in the context of my 3rd question? At least I could not make it work. Aug 22, 2022 at 17:52
• Look at the addendum to my answer. Aug 22, 2022 at 19:37

g[m_] = m E^(-m x^2)

gi[n_Integer] = n E^(-n x^2)

Attributes[gi] = {Listable}

gi[n] /. n -> {2, 3, 4}

gi[{2, 3, 4}]   (*  both yield   *)

(*   {2 E^(-2 x^2), 3 E^(-3 x^2), 4 E^(-4 x^2)}   *)


Many build in functions have this attribute.

Attributes[Sin]


If you don't do, or cancel it again, with Replace you get the whole list as argument of gi, which is not an Integer.

Attributes[gi] = {}

gi[n] /. n -> {2, 3, 4}

(*   gi[{2, 3, 4}]   *)


Without a restriction for argument like _Integer, function g is per se Listable g[{2, 3, 4}]. But

 Attributes[g]    (*   {}   *)


does not show it. Don't know why?

• Thanks Akku14. It is a complementary helpful perspective to the Listable approach. Aug 21, 2022 at 18:49