The main equation may not really be anywhere near so simple as that.
E.g. for the kind of stuff I do for work the main equation is something like output a vector which is the instructions for every powerplant in Texas of how much power to produce, such that every person and business that needs power has power, subject to constraints about the capacity of each powerline
Differentiate total emissions of this process with respect to demand.
It's is an equation yes.
But it's actually a multi-hundred line program that includes thousands more lines of constants and a step of running ab internal linear optimization solver inside the problem.
Symbolic Differentiation, like Mathematica tends to start to slow down and generate massively amounts of code once functions get complicated.
I am also not sure that it can handle dynamic length loops.
Source to Source AD, like Zygote (Julia), Tapenade (Fortran), Jax (Python), Enzyme (LLVM) have a lot of what you might want though.