Problems in dynamic symsum variables in solving a system of equation
3 views (last 30 days)
Show older comments
Dear all,
I enounter some problems in using dynamic symsum variables in solving a system of linear equation. I don't know what has gone wrong and don't know how to fix it. My code is as follows:
clear;
syms n
a = sym('a',[1 20])
b = sym('a_3',[1 20])
theta_0 = pi/3;
theta_1 = 2*pi/3;
beta = 0.5;
delta = 0.5;
U= delta*exp(i*theta_0) - ((1-delta^2)*exp(i*theta_0/2 + theta_1)/(1-delta*exp(i*theta_1)));
eqn1 = beta*a(1) + U*symsum((besselj(1+n ,2)*(-a(n) + beta*b(n))),n,-10,10) == U*b(1)
eqn2 = beta*a(2) + U*symsum((besselj(2+n ,2)*(-a(n) + beta*b(n))),n,-10,10) == U*b(2)
eqn2 = beta*a(3) + U*symsum((besselj(3+n ,2)*(-a(n) + beta*b(n))),n,-10,10) == U*b(2)
0 Comments
Accepted Answer
Paul
on 20 Jul 2023
Hi Tsz Tsun,
Why not just use regular sum instead of symsum when the goal is to just take the sum of a finite number of terms?
Define a and b, they each have 20 elements
%syms n integer
a = sym('a',[1 20])
b = sym('a_3',[1 20])
Proably best to use symbolic constant pi, and symbolic consntants for beta and delta
theta_0 = sym(pi)/3;
theta_1 = 2*sym(pi)/3;
beta = sym(0.5);
delta = sym(0.5);
Used 1i for these expressions, sometimes i is already in use as a variable
U= delta*exp(1i*theta_0) - ((1-delta^2)*exp(1i*theta_0/2 + theta_1)/(1-delta*exp(1i*theta_1)))
U could probalby be futher simplified.
Here, the original symsum expression was over 21 terms, but a and b only have 20 terms. I assumed they were mathematically indexed from -10 to 9. Also, Matlab uses 1-based indexing, so the indices into a and b are shifted by 11. Use elementwise multiplication, .*, and take the sum. Convert n to sym on the argument to besselj, unless it's desired to express those terms as actual numerical values.
n = -10:9;
eqn1 = beta*a(1) + U*sum(besselj(1+sym(n) ,2).*(-a(n+11) + beta*b(n+11))) == U*b(1)
Is that close to the desired result?
%eqn1 = beta*a(1) + U*symsum((besselj(1+n ,2).*(-a(n+11) + beta*b(n+11))),n,-10,9) == U*b(1)
10 Comments
Paul
on 20 Jul 2023
Edited: Paul
on 20 Jul 2023
The operator .' is shorthand for transpose, .' (which is, in general, not the same as ctranspose, '). The code is taking advantage of "binary singleton expansion," see here: Compatible Array Sizes.
(0:4).' + sym(n)
is adding a column vector and row vector to yield a matrix, which in turn is used to compute besselj at each element of thta matrix.
That matrix is then elementwise muliplied with .* with (-a(n+3) + beta*b(n+3)), which is a row vector. That row vector is "expanded" by copying that row as many time as needed to form a matrix that has as many rows as the matrix returned from besselj. That result is then element-wise multiplied with the besselj matrix. The resulting matrix is summed across the columns to yield a column vector.
If still not clear, break up that line into multiple steps, one for each operation.
More Answers (1)
Walter Roberson
on 20 Jul 2023
Edited: Walter Roberson
on 20 Jul 2023
A symbolic variable or expression or symfun or symmatrix can never be used as an index.
I cannot show you the corrected code because you are also trying to use n as negative indices -- you want to use a(n) with n from -10 to 10, but there is no possibility of a(-10)
You could construct
N = -10:10;
A = sym("a_" + regexprep(string(N), '-', '_'))
B = sym("b_" + regexprep(string(N), '-', '_'))
an = @(N) A(N+(length(A)+1)/2)
bn = @(N) B(N+(length(B)+1)/2)
and then you would be able to use things like
sum(besselj(2+N).*(-a(N) + beta*b(N)))
5 Comments
Walter Roberson
on 20 Jul 2023
No, before you were trying to use besselj(2+n,2) with n starting from -10, so when N is -10 to +10 then you would use besselj(2+N,2) not besselj(1+N,2)
I did, however, miss the ,2 parameter for besselj
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!









