# Plot multiple lines with Manipulate

I have a function $$e = f(w,a,i,\lambda)$$. I first plotted the relationship between $$e$$ and $$w$$ for three different values of $$a$$ for given $$i=0.1$$ and $$\lambda=0.5$$. For this my code is:

e[w_] := (w + a (-1 - a w + w^2) - Sqrt[a (a - (-1 + a^2) (i + lambda) w + a (i + lambda) w^2)])/(w + a w (-a + w))
lambda = 0.5; i = 0.1;
Plot[Evaluate@Table[e[w], {a, {0.1, 0.5, 0.9}}], {w, 0, 5}, PlotRange -> {0, 1}, PlotLabels -> {"a=0.1", "a=0.5", "a=0.9"}]


which successfully generated the following:

Now I would like to vary $$a = [0,1]$$ and $$\lambda =[0,1]$$ using Manipulate to see how the above results would be affected. My code for this is:

Clear["Global*"]
e[w_] := (w + a (-1 - a w + w^2) - Sqrt[a (a - (-1 + a^2) (i + lambda) w + a (i + lambda) w^2)])/(w + a w (-a + w))
Manipulate[ContourPlot[Evaluate@Table[e[w], {a, {0.1, 0.5, 0.9}}], {w, 0, 5}, {e, 0, 1}, FrameLabel -> {"w", "e"}], {{i, 0.1}, 0, 1}, {{lambda, 0.5}, 0, 1}]


which generates an error.

\$Version

(* "13.2.1 for Mac OS X ARM (64-bit) (January 27, 2023)" *)

Clear["Global*"]


The control variables in a Manipulate are local and if the function is defined external to the Manipulate, the control variables should be passed as explicit arguments to the function.

e[w_, i_, lambda_] := (w + a (-1 - a w + w^2) -
Sqrt[a (a - (-1 + a^2) (i + lambda) w + a (i + lambda) w^2)])/(w +
a w (-a + w))


Since the control variables define i and lambda you should continue to use Plot rather than switching to ContourPlot.

Manipulate[
Plot[Evaluate@
Table[e[w, i, lambda],
{a, {0.1, 0.5, 0.9}}],
{w, 0, 5},
PlotRange -> {0, 1},
AxesLabel -> {"w", "e"},
PlotLabels -> {"a=0.1", "a=0.5", "a=0.9"}], {{i, 0.1}, 0, 1, 0.01,
Appearance -> "Labeled"},
{{lambda, 0.5}, 0, 1, 0.01, Appearance -> "Labeled"}]


• thank you so much! This was exactly what I was looking for!
– ppp
Commented Feb 13, 2023 at 22:29

You are mixing Manipulate control variables, with the global variables used in your e[w_] function. One way around this is this. There are other ways.

Clear["Global*"]
e[w_] := (w + a (-1 - a w + w^2) -
Sqrt[a (a - (-1 + a^2) (i + lambda) w +
a (i + lambda) w^2)])/(w + a w (-a + w));

Manipulate[Module[{data, w},
data = Table[e[w] /. {i -> i0, lambda -> lambda0}, {a, {0.1, 0.5, 0.9}}];
ContourPlot[data, {w, 0, 5}, {e, 0, 1}, FrameLabel -> {"w", "e"}]
],

{{i0, 0.1, "i"}, 0, 1, .1, Appearance -> "Labeled"},
{{lambda0, 0.5, "lambda"}, 0, 1, .1, Appearance -> "Labeled"},
TrackedSymbols :> {i0, lambda0}
]
`