0
$\begingroup$

This expression:

FullSimplify[Solve[dx == dL - (1/2) dd, dL]]

Gives this solution (I've simplified it for brevity):

$$\left\{\left\{ d_L\rightarrow \frac{\sqrt{1+z}}{2+z} \right\}\right\}$$ Which appears to be a 'rule', but now I want to create a function from that rule. Something like:

F[z_] := $\frac{\sqrt{1+z}}{2+z}$

but with the actual result from the Solve function. I've seen various attempts with the ReplaceAll command, but I can't seem to get it to work. How do I do this?

EDIT: I'm reasonably comfortable getting the answer using substitution like in the examples on this question: Assign the results from a Solve to variable(s). What I can't figure out is how to make a proper Function.

$\endgroup$
  • $\begingroup$ Maybe this will help: it gives a list of pure Function, one for each solution: sol = Solve[a^2 + z a - 2 == 0, a]; Function[z, #] & /@ Values[sol] $\endgroup$ – Michael E2 Apr 9 at 19:42
  • $\begingroup$ @Artes - Not as far as I can see. I see several examples where substitution is used to get an answer, but I don't see any answers that show a function being created. Am I missing something? $\endgroup$ – Quarkly Apr 9 at 19:42
  • $\begingroup$ @MichaelE2 - No, that doesn't work. If you cut-and-paste your text exactly into Mathematica, then try Function[1024], all you get is 1024&. $\endgroup$ – Quarkly Apr 9 at 19:55
  • $\begingroup$ You could define two function in my example, since it has two solutions, with {F, G} = Function[z, #] & /@ Values[sol]. (Tho, you should avoid starting names with a capital, esp. single-letter symbols.) $\endgroup$ – Michael E2 Apr 9 at 19:56
  • $\begingroup$ @MichaelE2 - That looks more promising. Let me work on that. $\endgroup$ – Quarkly Apr 9 at 20:00
5
$\begingroup$

I generally do it this way.

{{dL -> Sqrt[1 + z]/(2 + z)}}

f[z_] = dL /. %[[1]]

f[2]
(*Sqrt[3]/4*)
| improve this answer | |
$\endgroup$
  • $\begingroup$ That appears to be the right answer. Thank you. $\endgroup$ – Quarkly Apr 9 at 20:27

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