How to perform the derivation of a symbolic vector on an symbolic function?

11 次查看(过去 30 天)
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.

回答(1 个)

Birdman
Birdman 2018-3-26
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 个评论
Birdman
Birdman 2018-3-27
It is written to show how to define a function dependent on a variable symbolically. Of course, it can be eliminated.

请先登录,再进行评论。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by