Error using symsum and arrays for summing over array elements

Hi all, I have a problem with using symsum and arrays. For example consider the following code, and I get the error 'Error using syms, using input and output arguments simultaneously is not supported '. How could I fix it?
array(1)=0.4;
array(2)=0.99;
array(3)=0.87;
array(4)=0.33;
array(5)=0.33;
array(6)=0.33;
array(7)=0.21;
array(8)=1.3;
array(9)=1.2;
array(10)=0.04;
disp(array)
0.4000 0.9900 0.8700 0.3300 0.3300 0.3300 0.2100 1.3000 1.2000 0.0400
syms q
trial = symsum( array(syms(q))*q^2 , q,2,7)
Error using syms (line 146)
Using input and output arguments simultaneously is not supported.

 Respuesta aceptada

VBBV
VBBV el 6 de Abr. de 2024
Editada: VBBV el 6 de Abr. de 2024
trial = symsum( array(double(q)).*q.^2 , q,2,7)

6 comentarios

VBBV
VBBV el 6 de Abr. de 2024
Editada: VBBV el 6 de Abr. de 2024

q is already defined as sym variable. Using syms inside symsum may not be needed. Doc also symsum function

array(1)=0.4;
array(2)=0.99;
array(3)=0.87;
array(4)=0.33;
array(5)=0.33;
array(6)=0.33;
array(7)=0.21;
array(8)=1.3;
array(9)=1.2;
array(10)=0.04;
disp(array)
0.4000 0.9900 0.8700 0.3300 0.3300 0.3300 0.2100 1.3000 1.2000 0.0400
% Unfortunately, it is still not working...
syms q
trial = symsum( array(double(q)).*q.^2 , q,2,7)
Error using mupadengine/feval2char
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.

Error in sym/double (line 755)
Xstr = feval2char(symengine, "symobj::double", S);
Symbolic variables can never be used to index an array.
When you have a finite set of things to index in an expression, you should not use symsum() or symprod(): instead you should create the full list of values as arrays, and sum() or prod() as appropriate.
@Tsz Tsun Just index the range of array elements in the vector as its given in expression and avoid symsum
array(1)=0.4;
array(2)=0.99;
array(3)=0.87;
array(4)=0.33;
array(5)=0.33;
array(6)=0.33;
array(7)=0.21;
array(8)=1.3;
array(9)=1.2;
array(10)=0.04;
disp(array)
0.4000 0.9900 0.8700 0.3300 0.3300 0.3300 0.2100 1.3000 1.2000 0.0400
trial = sum(array(2:7).*(2:7).^2)
trial = 47.4900
@Tsz Tsun you could also subs the symbolic variable for that index range and use sum
array(1)=0.4;
array(2)=0.99;
array(3)=0.87;
array(4)=0.33;
array(5)=0.33;
array(6)=0.33;
array(7)=0.21;
array(8)=1.3;
array(9)=1.2;
array(10)=0.04;
disp(array)
0.4000 0.9900 0.8700 0.3300 0.3300 0.3300 0.2100 1.3000 1.2000 0.0400
syms q
trial = sum(array(2:7).*subs(q.^2, q,2:7)) % subs the variable
trial = 
double(trial)
ans = 47.4900
Thanks a lot, it works well !

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Versión

R2022b

Preguntada:

el 6 de Abr. de 2024

Comentada:

el 7 de Abr. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by