Problems in dynamic symsum variables in solving a system of equation
조회 수: 4 (최근 30일)
이전 댓글 표시
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
채택된 답변
Paul
2023년 7월 20일
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
Paul
2023년 7월 20일
편집: Paul
2023년 7월 20일
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.
추가 답변 (1개)
Walter Roberson
2023년 7월 20일
편집: Walter Roberson
2023년 7월 20일
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
Walter Roberson
2023년 7월 20일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear Algebra에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!










