Sum over an array with symsum

조회 수: 1 (최근 30일)
steven
steven 2019년 4월 3일
댓글: steven 2019년 4월 3일
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

답변 (1개)

Walter Roberson
Walter Roberson 2019년 4월 3일
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
steven
steven 2019년 4월 3일
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?

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by