# Resubstituting variables back into the calculated

I have lengthy second order derivative of a function that is defined with multiple lines of variables. When I compute the derivative how can I put back into the computed result the auxilary variables I defined previously (Kp, sp, d1, d2, V)? I tried Simplify and Reduce with no success.

Kp = F2 + K;
sp = Sqrt[s1 + (s2 F2 /Kp)^2 - 2 \[Rho] s1 s2 F2/Kp];
d1 = 1/(sp Sqrt[T]) (Log[F1/Kp] + sp^2/2 T);
d2 = d1 - sp Sqrt[T];
V = E^(-r T) (F1 CDF[NormalDistribution[], d1] -
Kp CDF[NormalDistribution[], d2]);
D[V, {s1, 2}]


• I am not following your question. Since Kp, sp, d1, d2, V are defined as variables with values, then their values will only show up in the final result. Only symbols with no value will show in the final result, as Mathematica will automatically replace any name with its value. If you mean you want to reconstruct from the final result, the original variable names in the final solution, that will be an impossible task because the values have been used already. Btw, a simpler MWE to illustrate this question would be better. It is possible I missuderstood what you want. Commented May 18, 2023 at 5:55
• Not reconstruct but rather substitute. E.g. when one sees in the final formula square expression of sp, then replace the square root expression with a single variable, say sp_original. In my numeric code I calculate the sp, Kp etc values, I want to take advatage of the fact that they are computed when I numerically evaluate the final derivative expression. Otherwise the final formula is meaningless and one should use finite difference method. Defeats the purpose of analytical solution. Commented May 18, 2023 at 6:08
• May be I was not clear or do not understand what you mean. When you write Kp = F2 + K, then use Kp in computation, the output no longer contain Kp but contains only F2 and k and these could be any where in the final expression of the solution. What you seem to want, is to look for F2+K in the solution and rewrite this as Kp. Is this right? If so, this is impossible in general. But again, a simple MWE could be better. Commented May 18, 2023 at 6:13
• Yes, that lookup in the final formula and substitution is what I want. Why is it impossible? That seems to be something that is amenable to algorithmic solution. It need not be perfect in every possible case, but should lighten up the final formula. Commented May 18, 2023 at 6:15

I am not sure if I have understood your dialog with @Nasser correctly. However, if I am right that you want to get the expression in a more compact way expressed in terms of Kp and sp, I seem to see the way. You only need to define it in a slightly different way. I will try to calculate here the first derivative of V, while the second you will do in the same spirit.

Step 1:

Clear[Kp, sp, d1, d2];

d1 = 1/(sp Sqrt[T]) (Log[F1/Kp] + sp^2/2 T);
d2 = d1 - sp Sqrt[T];
V = E^(-r T) (F1 CDF[NormalDistribution[], d1] -
Kp CDF[NormalDistribution[], d2]);


Step 2:

der1=(D[V, sp] // FullSimplify)*
D[Sqrt[s1 + (s2 F2/Kp)^2 - 2 \[Rho] s1 s2 F2/Kp], s1]


Step3:

slSp = Solve[sp == Sqrt[s1 + (s2 F2/Kp)^2 - 2 \[Rho] s1 s2 F2/Kp],
s1][[1, 1]] // Quiet

(*  s1 -> (-F2^2 s2^2 + Kp^2 sp^2)/(Kp (Kp - 2 F2 s2 \[Rho]))  *)


Step 4:

der1 /. slSp // Simplify


Done. Now this expression is ready for calculation of the second derivative in the same way. I hope I understood you right and this helps.

Have fun!