Computing derivative of a CVX expression

Uncategorized
Mar 6, 2024
A

I declared a variable x(2,N) being N the horizon time. Then I got an expression using the following portion of code:

    cvx_begin 
    variable x(2,N)
    expression rho_sum1 
    rho_sum1 = 0;
    x1_limit1_pert = 5;

    for i=1:N
            rho_sum1 = rho_sum1 + exp(-k1*(x(1,i) - x1_limit1_pert));
    end

%%%
%%% More code ahead…

If I want to compute the derivative of rho_sum with respect to x(1,i), I use diff(rho_sum1,x(1,i)), however it shows up an error:
Incorrect number or types of inputs or outputs for function ‘diff’.

I appreciate the help on how to properly compute the derivatives in the CVX environment. Thank you !!

M

You need to compute derivatives outside of CVX, and provide them to CVX in a manner consistent with its rules. For instance, derivative w.r.t x of exp(k*x) would be provided as k*exp(k*x).

diff, which does (finite) differencing is not supported by CVX (i.e., for CVX variables or expressions). However, it is a linear operation, and can be implemented for a vector (CVX varaible) X of length n as X(2:end)-X(end-1);