I am trying to differentiate the following symbolic expression using a created symbolic vector but I keep getting errors. That is, I would like df/dx1, df/dx2, and df/dx3. Here is what I have tried:
>> x = sym('x', [3 1])
x =
x1
x2
x3
>> symbolic = 0.5*transpose(x)*eye(1)*x + [1 1 1]*x
symbolic =
x1^2/2 + x1 + x2^2/2 + x2 + x3^2/2 + x3
>> diff(symbolic, x)
Error using mupadmex
Error in MuPAD command: Invalid argument. [contains]
Evaluating: (Dom::Matrix(Dom::ExpressionField()))::_mult1
Error in sym/diff (line 44)
R = mupadmex('symobj::diff', S.s, x.s, int2str(n));
>> diff(symbolic, x.x1)
Error using sym/subsref
Too many output arguments.
Any assistance would be greatly appreciated. Thanks!
You can try one of these two options:
I prefer the ‘option 2’, because it is easier to evaluate expressions.
I think that eval has only the two functions I used above:
You can use eval to evaluate a matlab command written as a string, as if you were writing it as normal code. Try this to see what I mean:
a = 1; % set value of a to 1
eval(‘a = 2’); % change value of a to 2
eval([‘a = ‘,num2str(5)]); % set value of a to 5;
Hope this helps,