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
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:
- What causes the difference in plotting
g[m_]
andgi[n_Iinteger]
as above? - How can Plot work with the second function definition
gi[n_Integer]
, just as it does with the firstg[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
- How can multiple plots be accomplished as illustrated for
g[m_]
above with a function specified asu[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.
Integer
thenMap
the function onto the values.Plot[Evaluate[gi /@ {1, 2, 3}], {x, -3, 3}, PlotRange -> All]
$\endgroup$u[k_Integer]
in the same way. $\endgroup$