Symbolic summation of vectors/matrices

조회 수: 4 (최근 30일)
Alejandro Nava
Alejandro Nava 2022년 1월 18일
댓글: Walter Roberson 2022년 1월 19일
I want to write a code in MATLAB which outputs a symbolic sum. The mathematical equation is the following:
where l is a non-negative integer scalar constant, all the c are non-negative integer scalar constants, s is a complex scalar independent variable, all the p are complex scalar constants, and all the A are complex scalar constants.
What I want is that, given numerical values for l and the cs, compute the previous expression, using s, the A's and the p's as symbolic variables. For the sake of consider some example, let's choose and , and . Thus, the previous expression yields (and the code should yield):
That's what I want the code to output. Notice we didn't assign numerical values to s, the A's and the p's.
How can I achieve this in MATLAB? I know how to do in Wolfram Mathematica, as shown in the following figure. (For those who don't know the syntax for the Mathematica functions I used below, you can read this, this and this official webpages; Mathematica treats vectors as lists, and matrices as lists of lists.)
Figure 1.
Thanks in advance!

채택된 답변

Walter Roberson
Walter Roberson 2022년 1월 18일
편집: Walter Roberson 2022년 1월 18일
In MATLAB, it is tempting to use symsum(), but unfortunately that will not work. The difficulty is that the variable of summation must be symbolic, but that you can never use a symbolic number as an index in MATLAB.
You have a few choices:
  1. Loop.
  2. You just might be able to vectorize involving making 4D matrices and multiplying unselected components by 0 and summing. This would probably be awkward
  3. Nested arrayfun() calls, in which the inputs are indices. You could get rid of the third summation by pre-calculating 1+cumsum(c ) and indexing into that to get the P subscript .
As you are using an old version of MATLAB, you will need to use 'uniform', 0 to return symbolic expressions from arrayfun, and in your version cell2mat() does not support symbolic expressions so you will probably want to add a helper function
CELL2MAT = @(C) [C{:}]
which would then allow you things like
sum(CELL2MAT(arrayfun(STUFF)))
  댓글 수: 2
Alejandro Nava
Alejandro Nava 2022년 1월 19일
Thanks Walter for taking your time to read and answer my question. How would I exactly use arrayfun() in my case? I read the webpage and saw that it applies a function to every element of a vector/matrix. In my case, I have the vector (defined in MATLAB as c = [3 1 2]), from which I compute l in MATLAB as l = size(c, 2). I also have the vector and the matrix , which I could define as empty variables in MATLAB as p = [] and A = []. But after that, I don't see how I could use arrayfun(). Sorry for further asking, but I couldn't find info on Google similar to my question (or I didn't know what to search for).
Walter Roberson
Walter Roberson 2022년 1월 19일
The trick is to arrayfun over indices. For example,
CELL2MAT = @(C) [C{:}];
cumcr = cumsum([1, c]);
L = numel(c);
DEPTH2 = @(k,i) A(k,i)./(s - P(cumcr(k))).^i
DEPTH1 = @(k) sum(CELL2MAT(arrayfun(@(i) DEPTH2(k,i), 1:c(k), 'uniform', 0)))
output = sum(CELL2MAT(arrayfun(DEPTH1, 1:L, 'uniform', 0)))

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

제품


릴리스

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by