# How do I use the result of Solve in a function definition?

I'm trying to adapt the following piece to the part where PowerF is a function of amax.

Z = Max[Z /. Solve[FullSimplify[PowerF, Z ∈ Reals] == P0, Z]]


Now I have modified my code because I want to also make a plot that depends on the parameter amax.

However the following does not work:

Z[amax_] := Max[Z[amax] /. Solve[FullSimplify[PowerF[amax], Z ∈ Reals] == P0, Z]]


How to fix this?

-

You're doing a replacement to get the solution, not evaluating a function in the Z /. part, so it can really be any variable. You'll end up with a recursion with a definition like Z[x_]:= f[Z[x]...]. So try something like:

Z[amax_] := Max[z /. Solve[FullSimplify[PowerF[amax], z ∈ Reals] == P0, z]]


If you want the variable that you're solving for to look similar to your function variable, then use formal symbols. It would look something like this:

As always, the standard warning to not use capital letters as function names and variables applies here.

-
Thanks, I will try this, but I think there was a problem when $z$ was already set (by running it once with different amax). Is there a way to reset the value of z before doing a computation? –  Jonas Teuwen Apr 6 '12 at 21:07
@JonasTeuwen Formal symbols cannot be assigned a value and so they'll never have phantom values from previous calculations. –  rm -rf Apr 6 '12 at 21:08
@RM Right, good point! Can you explain why I cannot use capital letters as function names? –  Jonas Teuwen Apr 6 '12 at 21:16
@Jon mma functions (at least, those exposed to the user) always start with a capital letter and there are single letter symbols such as D, E, I, N, etc. Invariably people use these and get confused when they get an error. Most of these symbols have the Protected attribute, meaning you can't modify them easily, however, some such as K don't, which will let you freely modify it, and there are instances where this has caused issues (and hard to debug since there is no obvious error message). Of course, you could say — "I'll avoid just those", but it's good practice to stick to convention. –  rm -rf Apr 6 '12 at 21:23
@JonasTeuwen Z=. or, even better, ClearAll[Z] will do. –  Sjoerd C. de Vries Apr 7 '12 at 6:42