# replacement – Going between assignment rules and functions

I am trying to develop a model using Mathematica, and continuously run into difficulties going between the outputs of various functions (Solve, Differentiate, etc.) and back to functions (in the colloquial sense). Often in my application the results of one function are the inputs to another.

I’ve read the relevant documentation pages, and looked for previous questions. I was able to solve some of my issues this way, mostly by defining additional variables and using ReplaceAll (/.) to replace these additional variables with the assignment rules from previous steps.

Now I’ve run into an issue that I can’t seem to ‘replace’ my out of. I’ve tried to simplify the question to get at the core of my misunderstanding. I define the following function:

``````Rh(a_) = Ch + Z*(A - a)
Pih = x*Rh(a) - (Gamma)*0.5*Q*x^2 + P
``````

I now want to differentiate `Pih` w.r.t. x:

``````RESULT = Solve(D(Pih, x) == 0, x) // Simplify
``````

The result is an assignment `x-> (Ch+Z(A-a))/(Q(Gamma))`

Now in order to use this result I would normally do something like:

``````RESULT2 = Solve(x==a/.RESULT,Q)
``````

Where often I’d have to use multiple replacements (if, for example, `a` was also the result of some previous calculation.) I would play around with the location and ordering of the `/.`, sometimes with Flatten() until it worked.

Questions 1 – is this the correct method, or am I missing something much simpler?
If there are any resources that deal with this explicitly I would highly appreciate it (I’ve read the documentation for assignment and functions, and this is the best I came up with)

Question 2 – more specifically, I have not been able to solve the following. Based on the same example as above:
Suppose I want to redefine a function h(a) like this:

``````h(a_)=h0+Y*a
``````

Where Y is the derivative of `RESULT` from the previous calculation `-Z/(Q(Gamma))`. However, when I apply D(RESULT,a), the result is `0->-Z/(Q(Gamma)`.
I successfully used

``````h(a_)=h0+0*a/.D(RESULT, a)
``````

Which works but is terrible and I cannot believe is the right way to do this. I understand why Mathematica is doing this, but I’m lost as to what would be a better, more general alternative.

I tried using := in various places, to no avail (I usually just get the whole Solve(…) function as RESULT…)

Thanks for any help