Symbolic toolbox mpower unexpected behaviour

36 views (last 30 days)
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.

Accepted Answer

Stefan Wehmeier
Stefan Wehmeier on 3 Jul 2017
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 Comment
Yannick Couzinié
Yannick Couzinié on 4 Jul 2017
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.

Sign in to comment.

More Answers (2)

Richard Marveldoss
Richard Marveldoss on 30 Jun 2017
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 Comment
Yannick Couzinié
Yannick Couzinié on 2 Jul 2017
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.

Sign in to comment.


Walter Roberson
Walter Roberson on 3 Jul 2017
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 Comment
Yannick Couzinié
Yannick Couzinié on 3 Jul 2017
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)!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!