Analytically take the derivtive of a matrix for use with JuMP variables



I am solving optimal control problems where I need to calculate the D or differentiation matrix, it is full of derivartives of Lagrange Polynomials . This matrix is also a function of the final time (tf, which is a design variable within the optimization). My first attempt (F2) to calculate this matrix was to modify this function (F1) for JuMP by creating many @NLexpression()s. The output for F1 and F2 match perfectly until the final time is small tf < 0.1 I am not sure why this is. Could it be a simplification that @NLexpression() is making?

Anyways, after all of this I decided that there must be a better way to accomplish my goal. What (I think) I need to do is generate an analytical expression of the D matrix in terms of tf before optimization. Then, during optimization, I can use the elements of the D matrix (which will be functions of tf) in an @NLconstraint() current code.

Any suggestions or thoughts ? It seems that automatic differentiation may not work for my case since I don’t know tf before optimization. I was thinking about using Calculus.jl, but I am not sure if that will be waisting more of my time (if there is a better package or better way to do this). Also, I was thinking about registering the functions of tf in the D matrix using JuMP, but I am very concerned with speed and since the registering functionality uses ForwardDiff I am not sure if it will be fast enough.

Thank you very much!


Could it be a simplification that @NLexpression() is making?

No. JuMP doesn’t do this.

Any suggestions or thoughts ?

You’re free to derive symbolic expressions in an automated way and input those expression into constraints:


@miles.lubin that is a good idea, thanks for the feedback.

Is there an example where variables or constraints are generated programatically using JuMP? I looked into this before and had some difficulty figuring it out.

Is there a package that you recommend to derive these symbolic expressions? Like Calculus.jl?


Or might SymPy.jl be better for my purpose as I have a lot of math to carry out on the variables.

And if I use SymPy.jl can the symbolic variables that it creates be used (or converted into JuMP variables)?


I like using SymEngine.jl for this. Similar to SymPy but its core gets better performance.