# How can I speed up this plot manipulation? Why does Evaluate not help?

If I were able to "Save" the computed inverse function somewhere and then plot it, the manipulation would become faster. As I understand, Evaluate should do the job, but for some reason it does not. Why?

f[x_] := Sqrt[x]; c[x_] := x^2; g[x_] := c'[x]/f'[x];
x[gamma_, δ_, φ_, ρ_, mode_] :=
InverseFunction[g][gamma/(1 - δ*φ*ρ)*mode];

Manipulate[
Plot[
{Evaluate@x[gamma, δ, φ, ρ, 1],
Evaluate@x[gamma, δ, φ, ρ, 2]},
{gamma, 0, 10},
PlotRange -> {{0, 10}, {0, 10}}],

{δ, 0, 1}, {φ, 0, 1}, {ρ, 0, 1}
]

-

In the Manipulate as you wrote it, the InverseFunction has to be re-calculated every time the function x is called. To avoid this, define x with Set instead of SetDelayed:

f[x_] := Sqrt[x]; c[x_] := x^2; g[x_] := c'[x]/f'[x];

Clear[x];

Block[{gamma, δ, φ, ρ, mode},
x[gamma_, δ_, φ_, ρ_, mode_] =
InverseFunction[g][gamma/(1 - δ*φ*ρ)*mode]
]

(*
==> ((gamma mode)/(1 - δ ρ φ))^(2/3)/(2 2^(
1/3))
*)

Manipulate[
Plot[{x[gamma, δ, φ, ρ, 1],
x[gamma, δ, φ, ρ, 2]}, {gamma, 0, 10},
PlotRange -> {{0, 10}, {0, 10}}], {δ, 0, 1}, {φ, 0,
1}, {ρ, 0, 1}]


Then you can also leave out the Evaluate as I did above.

-
thanks for the reply. However, the Manipulate does not seem to work in your example.. –  P.Escondido Jul 21 '13 at 1:54
Sorry, this was the Mathematica problem. I restarted it and it worked alright! –  P.Escondido Jul 21 '13 at 3:38
Jens, IMO you need to protect against the symbols used in x having values assigned. I am going to wrap that definition in Block. –  Mr.Wizard Jul 21 '13 at 5:58
@Mr.Wizard What specific to this situation necessitates that Block be used? –  Sjoerd C. de Vries Jul 21 '13 at 6:13
@Mr.Wizard You're right, it's better to be safe than sorry. I could also have added the variables to the Clear statement. But using Block is more flexible because it leaves any previously assigned values for δ, φ, ρ etc. intact while at the same time preventing them from becoming part of the definition of x. –  Jens Jul 21 '13 at 7:04

Evaluate needs to be the top-most expression inside Plot. As in, the code needs to look exactly like:

Plot[Evaluate[...], ...]


or else the call to Evaluate will be effectively ignored.

-
I implemented what you've suggested and Manipulate stopped working –  P.Escondido Jul 21 '13 at 3:01
Perhaps you need to restart Mathematica for some reason? I'm using Manipulate[Plot[Evaluate[{x[..., 1], x[..., 2]}],...],...] without any problems. –  Brett Champion Jul 21 '13 at 3:08
Indeed, restart helped! Thanks! Is that common that Mathematica needs a restart to work properly? –  P.Escondido Jul 21 '13 at 3:39
It was probably working properly before, but you may have had some intermediate state (such as another definition for x) that was causing unexpected behavior. This would have been difficult to track down online, so I suggested a restart. –  Brett Champion Jul 21 '13 at 4:17