Fix variable indexing for gradient calculation. Also, perform state variable substitution, e.g., set constant T, prior to variable differentiation. This removes the need to keep providing the value at every iteration and reduces the complexity of the AST. We will need to come back to this at some point to enable more advanced calculations such as determining the phase transition temperature at fixed composition.