2
$\begingroup$

I've got a few ugly equations to take gradients of and I'd love to get matlab to validate my work, but I'm having trouble because the equations are mixed with variable length vectors and some gnarly summations.

If I could get this to work I might be able to express my more complex formulas. Let $f(\boldsymbol{d})=\sum_{n=1}^N d_n\ln(d_n)$ where $\boldsymbol{d} \in \mathbb{R}^N$.

I've tried fixing $N$ to an arbitrary value such as $3$. I've tried converting things to vector notation. But I'm not getting anything to work.

For example:

>> d = sym('d',[3 1])
>> f(d) = symsum(d(n)*log(d(n)),n,1,3)

Error using sym/subsindex (line 737)
Invalid indexing or function definition. When defining a function, ensure that the arguments are symbolic variables and the body of the function is a SYM expression. When
indexing, the input must be numeric, logical, or ':'.

Error in sym/subsref (line 776)
            R_tilde = builtin('subsref',L_tilde,Idx);

I'd love if there were a straight forward way to model the expression as I see it there rather than needing to convert it to something else (e.g. fewer chances for human error). Though I haven't gotten anything to work yet.

If I use vector notation, I'm not quite clear on what I'm getting, and I'm afraid that I won't be able to build up more complex formulas from what I see.

>> f(d) = d'*log(d)

f(d1, d2, d3) =

conj(d1)*log(d1) + conj(d2)*log(d2) + conj(d3)*log(d3)
$\endgroup$
  • 1
    $\begingroup$ syms d; e = sum(d.*log(d)); f = gradient(e); res = subs(f, d, [1,2,3,4]) $\endgroup$ – reuns Feb 15 '16 at 6:22
  • 1
    $\begingroup$ The comment before mine looks like it answers your question, but as an aside, ' is complex conjugate transpose, .' is a normal transpose. It's a good habit to use .' all the time, unless you really want a complex conjugate transpose, because it can lead to some weird errors. $\endgroup$ – David Feb 16 '16 at 2:48
  • $\begingroup$ That's a helpful example, I hadn't thought of substituting in a vector after the fact, thanks for that. If you'd post that as an answer I'll accept it @user1952009 $\endgroup$ – David Parks Feb 29 '16 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.