I have a matrix, A
:
A=[ x.^2 + y , 0;
0 , x.^2 + y ]
I also have an operator matrix, D
:
D = [d/dx , 0;
0 , d/dy ]
I want to be able to multiply D*A
and end up with a matrix that looks like:
B = [ diff(A(1,1),x) , 0 ;
0 , diff(A(2,2),y) ]
obviously I can't do this with the diff() function since that function is not an operator that can multiply through a function. So how can I go about this using symbolic operators? In reality, my matrices are large so performing without operator multiplication is not preferable.
Also, lets say I find a way to produce the above B
matrix, which would look like:
B =
[ 2*x, 0
0, 1]
How can I evaluate B
at, for example, x=2, y=1
;
My attempt:
subs(B,x,2,y,1)
But this is obviously incorrect arguments for the sym.subs
function
I also tried:
subs(B,2,1)
and that did not work either, so my other question is how can I substitute in for x
and y
in the B
matrix.
For your second question: the syntax of substituting multiple values for symbolic variables is
subs(B, {x,y}, {2,1})
(and typing help subs
in Matlab command prompt would give this example).
For the first: your idea of speeding up the computation of symbolic derivatives by vectorizing them cannot work. Vectorization makes sense for low-level operations that can be performed in bulk by some existing C++ library designed to handle arrays. Symbolic differentiation is something quite different: there's a sophisticated algorithm to be run for each expression. Using for loop to find multiple symbolic derivatives is appropriate, no matter how many. If it takes long, it's because taking a lot of symbolic derivatives takes long.
One possible optimization I'd look for is finding common elements between the functions in matrix B, so that they can be differentiated once. But this requires working with the details of what functions are contained in the matrix.