# Problems in dynamic symsum variables in solving a system of equation

4 vues (au cours des 30 derniers jours)
Tsz Tsun le 20 Juil 2023
Commenté : Tsz Tsun le 21 Juil 2023
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])
a =
b = sym('a_3',[1 20])
b =
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)
Error using indexing
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.

Error in indexing (line 936)
R_tilde = builtin('subsref',L_tilde,Idx);
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 commentairesAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Paul le 20 Juil 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])
a =
b = sym('a_3',[1 20])
b =
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 =
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)
eqn1 =
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 commentairesAfficher 9 commentaires plus anciensMasquer 9 commentaires plus anciens
Tsz Tsun le 21 Juil 2023
Thanks for the explanation ! It is very clear!

Connectez-vous pour commenter.

### Plus de réponses (1)

Walter Roberson le 20 Juil 2023
Modifié(e) : Walter Roberson le 20 Juil 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 commentairesAfficher 4 commentaires plus anciensMasquer 4 commentaires plus anciens
Tsz Tsun le 20 Juil 2023
I see, thanks for the clarification !

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Linear Algebra dans Help Center et File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by