Sum over an array with symsum

1 vue (au cours des 30 derniers jours)
steven
steven le 3 Avr 2019
Commenté : steven le 3 Avr 2019
I would like to sum over an array of length n using symsum. To give you an exmaple of what I mean, consider
a = [1 2 3]
symsum(a(n), n, 1, 3)
I would like to have the output 1+2+3=6 but I receive the following error (sorry for bad editting, I am new):
Error using sym/subsindex (line 836)
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic
variables, and function body must be sym expression.
I know there are other methods, such as sum and for-loop but I would like to use symsum for my purposes. Is there any way that this can be done in Matlab?
Thanks

Réponses (1)

Walter Roberson
Walter Roberson le 3 Avr 2019
No. It is never possible to use a symbolic variable as a subscript in MATLAB.
Remember that the a(n) part will be evaluated in the process of constructing the arguments of the function, before the function can be evaluated. Therefore a(n) would have to be useable by itself, but it is not.
  1 commentaire
steven
steven le 3 Avr 2019
Then how would it be possible to define a function like
where a(n) is changing at each iteration.
I was thinking to use the following code
f = @(x) symsum(a(n)*exp(1i*pi*n*x), n, 0, 5)
which does not give the output I expect.
Is there a better way to go about this?

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by