Symbol matrix function differentiation

1.The function 'diff' seems could only work with one-element variable 'v',
diff(f,v); % f(v), v is a one-lelement variable
while I would like do differentiation on a symbol matrix
diff(F, V); % F(V), V is a symbol matrix variable
2.If I make differentiation to each element of the symbol matrix, and obtain the result of diff(F, V), while the result is in element by element format,
[g1(v_i), g2(v_i), ..., gn(v_i)]
so I want to know are there some methods to make the result in symbol matrix variable format like this?
g(V)
3.for example
diff(x^T*A*x, x) = A^T*x + A*x; % A is a constant matrix, x is a vector

 채택된 답변

Walter Roberson
Walter Roberson 2013년 3월 4일
편집: Walter Roberson 2013년 3월 4일

0 개 추천

If you want the result to be like in your (2) above, then use a "for" loop, or use
g = feval(symengine, 'zip', F, V, 'diff');
or use
g = cell2mat(arrayfun(@diff, F, V, 'Uniform', 0));
You might be able to just use
g = arrayfun(@diff, F, V);
but I think I recall seeing that have problems with arrayfun expecting numeric results.

댓글 수: 6

Youssef  Khmou
Youssef Khmou 2013년 3월 4일
편집: Youssef Khmou 2013년 3월 4일
hi Walter, the first & last options :
g=arrayfun(@diff, F, 2) % or V generally
g = cell2mat(arrayfun(@diff, f, 1, 'Uniform', 0))
do not work :
??? Error using ==> arrayfun
Unsupported ARRAYFUN input type: sym
Ah. Well there are always the syntax-disguised for loops:
g = reshape( cell2mat( arrayfun(@(K) diff(F(K), V(K)), 1:numel(F), 'Uniform', 0) ), size(F))
Hi Walter, thx for answering. I already could obtain the result liked in (2) by using 'for' loop, what I want to do is like this: 1.type the command
diff(x.'*A*x, x)
2.the output is better as the follow
ans =
A.'*x + A*x
It seems be a very difficult work for implementation, I'd better deduce the formula on the paper as assistance.
Walter Roberson
Walter Roberson 2013년 3월 4일
If the .' represents transpose, then how could that be the correct output if A is not square?
chen xy
chen xy 2013년 3월 5일
If A isn't square, "x.'*A*x" will be wrong for matrix dimensions.
Walter Roberson
Walter Roberson 2013년 3월 5일
True.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by