# Simplify symbolic equation substituting parts with other expressions

I will try to be as clear as possible.

I need to calculate the derivative of a function. The result is a very long equation and I would like Mathematica to find and substitute parts naming them with an assigned symbol.

### Example

I solve this system of equations:

Solution =
Refine[
Reduce[{x == AI*(L - x - 3*y), y == AJ*(L - x - 3*y)^3}, {x,y}, Reals],
Assumptions -> L > 0 && AI > 0 && AJ > 0
]


where the variables AI and AJ represent exponential functions with some coefficients before.

At this point I differentiate this function with respect to v, expanding AI and AJ (because they contain the variable v inside):

D[
Last @ ReplaceAll[
Solution[[1]],
{
AI -> BI*aaI*kI*Exp[-(ZI/VTH)*(v - VREF)],
AJ -> BJ*aaJ*kJ*Exp[-(ZJ/VTH)*(v - VREF)]
}
],
v
]


The result is a complicated and very long expression. Is there a way to simplify the resulting expression by regrouping back all the AI and AJ? ReplaceAll does not work properly because the pattern does not appear as the original expression (they are separated by the internal simplification)

I think I should change the way I'm doing this, but I'm new with Mathematica and I could not find any similar question.

• Why not just call the replacement variables functions of v? As in solution = Refine[Reduce[{x == AI*(L - x - 3*y), y == AJ*(L - x - 3*y)^3}, {x, y}, Reals], Assumptions -> L > 0 && AI > 0 && AJ > 0][[1, 2]] /. {AI -> AI[v], AJ -> AJ[v]}. Now it can be differentiated with respect to v and no substitution is needed unless and until you want to express in terms of the original larger terms. Aug 8 '18 at 17:45

If I understand correctly, you have two functions of v, AI and AJ, which when differentiated are themselves functions of AI and AJ. It is much simpler to make the dependence on v explicit, so I will deal with AI[v] and AJ[v] instead:

eqns = And[
AI[v] == BI*aaI*kI*Exp[-(ZI/VTH)*(v - VREF)],
AJ[v] == BJ*aaJ*kJ*Exp[-(ZJ/VTH)*(v - VREF)]
];


Here is one way to find the dependence of AI'[v] and AJ'[v] on AI[v] and AJ[v]:

soln = First @ Solve[eqns && D[eqns, v], {AI'[v], AJ'[v]}, {BI,BJ}]


{AI'[v] -> -(ZI AI[v])/VTH, AJ'[v] -> -(ZJ AJ[v])/VTH}

where the last argument of Solve is used to eliminate those variables. Now, we can define the derivatives:

Clear[AI, AJ];
AI' ^= Function[v, Evaluate[AI'[v] /. soln]];
AJ' ^= Function[v, Evaluate[AJ'[v] /. soln]];


Check:

AI'[x]
AI''[y]


-((ZI AI[x])/VTH)

(ZI^2 AI[y])/VTH^2

Finally, we can change AI -> AI[v] and AJ->AJ[v] in your solution, and differentiate:

Simplify @ D[Last @ Solution[[1]] /. p:(AI|AJ) -> p[v], v]


(2 ZI AI[v]^2 (2 + 3 AI[v]) (9 L AI[v]^3 AJ[v]^2 + Sqrt[ AI[v]^6 AJ[ v]^3 (4 + 12 AI[v] + 12 AI[v]^2 + 4 AI[v]^3 + 81 L^2 AJ[v])]) + ( 2^(1/3) ZJ (9 L AI[v]^3 AJ[v]^2 + Sqrt[ AI[v]^6 AJ[ v]^3 (4 + 12 AI[v] + 12 AI[v]^2 + 4 AI[v]^3 + 81 L^2 AJ[v])])^(5/3))/ AJ[v] - (2 AI[v]^5 (1 + AI[v]) AJ[ v]^2 (2 (7 ZI + 3 ZJ) AI[v]^4 AJ[v] + 2 (8 ZI + 3 ZJ) AI[v]^5 AJ[v] + 2 (3 ZI + ZJ) AI[v]^6 AJ[v] + 3 L (3 ZI + 2 ZJ) Sqrt[ AI[v]^6 AJ[ v]^3 (4 + 12 AI[v] + 12 AI[v]^2 + 4 AI[v]^3 + 81 L^2 AJ[v])] + AI[v]^3 AJ[ v] (2 (2 ZI + ZJ) + 27 L^2 (3 ZI + 2 ZJ) AJ[v])))/(Sqrt[ AI[v]^6 AJ[ v]^3 (4 + 12 AI[v] + 12 AI[v]^2 + 4 AI[v]^3 + 81 L^2 AJ[v])]) - (2^(1/3) AJ[v] (9 L AI[v]^3 AJ[v]^2 + Sqrt[ AI[v]^6 AJ[ v]^3 (4 + 12 AI[v] + 12 AI[v]^2 + 4 AI[v]^3 + 81 L^2 AJ[v])])^( 2/3) (2 (7 ZI + 3 ZJ) AI[v]^7 AJ[v] + 2 (8 ZI + 3 ZJ) AI[v]^8 AJ[v] + 2 (3 ZI + ZJ) AI[v]^9 AJ[v] + 3 L (3 ZI + 2 ZJ) AI[v]^3 Sqrt[ AI[v]^6 AJ[ v]^3 (4 + 12 AI[v] + 12 AI[v]^2 + 4 AI[v]^3 + 81 L^2 AJ[v])] + AI[v]^6 AJ[ v] (2 (2 ZI + ZJ) + 27 L^2 (3 ZI + 2 ZJ) AJ[v])))/(Sqrt[ AI[v]^6 AJ[ v]^3 (4 + 12 AI[v] + 12 AI[v]^2 + 4 AI[v]^3 + 81 L^2 AJ[v])]))/(3 2^(2/3) VTH (9 L AI[v]^3 AJ[v]^2 + Sqrt[ AI[v]^6 AJ[ v]^3 (4 + 12 AI[v] + 12 AI[v]^2 + 4 AI[v]^3 + 81 L^2 AJ[v])])^( 4/3))

• Yes this solution does exactly what I needed. Thank you. I just point out, for who wants to reproduce your code that I had to split it in two cells. In fact the code before your 'clear...' had to be run only one time (like an initialization cell) otherwise some kind of recursive errors are obtained. Aug 10 '18 at 8:24