Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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}
]
share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

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.

share|improve this answer
    
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
show 3 more comments

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.