Are you maybe looking for jacobian(a*b+c,c)?
How to perform the derivation of a symbolic vector on an symbolic function?
5 views (last 30 days)
Show older comments
Hello,
I would like to perform a derivation of a function containing symbolic vectors and matrices.
As an example:
a = sym('a', [3 3], 'real');
b = sym('b', [3 1], 'real');
c = sym('c', [3 1], 'real');
d = a*b + c
diff(d,c)
Here I get an error saying:
Error using sym/diff (line 70) Second argument must be a variable or a nonnegative integer specifying the number of differentiations.
The problem is that C is defined as:
c =
c1
c2
c3
And this way I cannot differentiate for c. I also tried to define separate symbols c1 c2 c3 but this does not work either.
Answers (1)
Birdman
on 26 Mar 2018
The error that you get actually tells you what to do: Variable d is not a function of variable c. You need to indicate that d(c) and then do the operations. For example, let's consider the possible approaches:
1- Your initial case:
a = sym('a', [3 3], 'real');
b = sym('b', [3 1], 'real');
c = sym('c', [3 1], 'real');
d = a*b + c
diff(d,c)
d is not dependent on any of those variables, therefore its change according to c will not mean anything, which will end up zero mathematically. This is normal.
2- d being dependent on c:
a = sym('a', [3 3], 'real');
b = sym('b', [3 1], 'real');
c = sym('c', [3 1], 'real');
d(c) = a*b + c
diff(d,c)
This will either not work because c contains three elements. You need to specify according to which element of c you are taking derivative. For instance:
>> diff(d,c(1))
ans(c1, c2, c3) =
1
0
0
>> diff(d,c(2))
ans(c1, c2, c3) =
0
1
0
Hope this helps.
2 Comments
Christopher Creutzig
on 27 Mar 2018
The d(c) step is not needed. It's perfectly reasonable to call diff(a*b+c,c(1)).
Birdman
on 27 Mar 2018
It is written to show how to define a function dependent on a variable symbolically. Of course, it can be eliminated.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!