- Loop.
- You just might be able to vectorize involving making 4D matrices and multiplying unselected components by 0 and summing. This would probably be awkward
- 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 .
Symbolic summation of vectors/matrices
4 views (last 30 days)
Show older comments
Alejandro Nava
on 18 Jan 2022
Commented: Walter Roberson
on 19 Jan 2022
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!
0 Comments
Accepted Answer
Walter Roberson
on 18 Jan 2022
Edited: Walter Roberson
on 18 Jan 2022
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:
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 Comments
Walter Roberson
on 19 Jan 2022
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)))
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!