Symbolic toolbox mpower unexpected behaviour

조회 수: 10 (최근 30일)
Yannick Couzinié
Yannick Couzinié 2017년 6월 24일
댓글: Yannick Couzinié 2017년 7월 4일
I am trying to run a symbolic calculation which produces an error, and I think I have nailed down the cause to some behaviour of mpower in combination with following operations, which I just don't understand (being fairly new to matlab, so maybe I have made a basic mistake here). The following is a minimal working example
clear
syms A B C N;
assume(N, 'integer')
T = [A B; B C];
TN = mpower(T, N);
tr = trace(TN);
prob = diff(tr, B, 2)
It produces the error
Error using symengine
An arithmetical expression is expected.
which is not the error I am getting on my real code (Not a square matrix by symengine) but the problem should be analogous (I hope). After running mpower(T,N) the returned object is
TN = matrix([[A, B], [B, C]])^N
which is not a matrix (why I call this unexpected on my end), but some kind of scalar since the trace has no effect on it:
tr = matrix([[A, B], [B, C]])^N
So I have been trying to solve this for quite some time now, but I really don't know what I am doing wrong, except of trying to do a non-trivial calculation (I realize that taking general powers is not the easiest thing to handle). Any helping input would be appreciated.

채택된 답변

Stefan Wehmeier
Stefan Wehmeier 2017년 7월 3일
You can use symbolic N but not in connection with mpower. As T^N = exp(N*log(T)), write
TN = expm(N*logm(T))
and proceed as in your example. You may want to apply simplify in the end:
simplify(prob)
  댓글 수: 1
Yannick Couzinié
Yannick Couzinié 2017년 7월 4일
I guess this really is the base of the problem I was having. Once I get the calculations to run through I will accept this answer.

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

추가 답변 (2개)

Richard Marveldoss
Richard Marveldoss 2017년 6월 30일
According to the expression used in the given example I assume that A,B,C are not matrices. Since the result of mpower operation is going to vary based on the value of N , the expression cannot be simplified beyond what is the result obtained which makes it improbable for the trace function to be applied on it. A possible workaround would be to a give a value to N and an expression would be obtained(TN) where N would be a fixed value rather than a variable. Below is an example code :
clear
syms A B C ;
%assume(N, 'integer')
N=4;
T = [A B; B C];
TN = mpower(T, N);
tr = trace(TN);
prob = diff(tr, B, 2)
  댓글 수: 1
Yannick Couzinié
Yannick Couzinié 2017년 7월 2일
편집: Yannick Couzinié 2017년 7월 4일
I should've written the assumptions for A, B, C. For all intents and purposes they are assumed to be reals.
I realize that this is not a simple arithmetic task. And I guess, inserting values for N and guessing the general result by induction would've been easier than trying to solve the general case.
But just out of interest, is it not still unexpected that the trace operator has no effect on a matrix object? Exponentiated matrices are still matrices, so at least giving an error like 'this is too complex' or 'Expected integer and not symbol as exponential' or something would be better, wouldn't it? It is that point that still bugs me.

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


Walter Roberson
Walter Roberson 2017년 7월 3일
trace is not defined for symbolic expressions https://www.mathworks.com/help/symbolic/functionlist.html
The generic trace() function is seeing that it is being passed what looks like a scalar object to it, and the diagonal of a scalar is the scalar itself, so the result is the same as the input.
diag is defined symbolically so you could use sum(diag(TN))
  댓글 수: 1
Yannick Couzinié
Yannick Couzinié 2017년 7월 3일
편집: Yannick Couzinié 2017년 7월 4일
I figured as much. And, I guess, I should've found the sum(diag(x)) way myself. Thank you very much (though sadly it turns out it doesn't solve my particular problem)!

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

카테고리

Help CenterFile Exchange에서 Symbolic Variables, Expressions, Functions, and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by