问题
I'm attempting to sum the following using the symsum function in MATLAB:
sum (from q=0 to 5) [a(q+1)*x(2)^q]
where a=[a0, a1, ..., a5], x=[x(1), x(2), ...] where x(1), x(2), ... are scalars.
The sum is a0 + a1x(2)+a2x(2)^2 +...+a5x(2)^5.
I've used the following code:
syms q a x
f=a(q+1)*x(2)^q
symsum(f, q, 0, 5)
where x(2)= -4.9.
However, the above code returns "Invalid indexing or function definition".
Using f=x(2)^q does not result in the error, however, using f=a(q+1) does return the error. Therefore the problem lies within the a(q+1) term.
Any help is greatly appreciated!
回答1:
It doesn't matter a
is matrix of symbolics or non-symbolics.
Every call to a matrix need an index(a number, real number like 1,2,3,4,...)
When q
is a symbolic it means q really doesn't equal to any number, so a(q)
has no meaning because q
has no equality to any number. So programs make error and stops before going more.
>>a=0:5;
>> a(1)
ans =
0
>>a(q) % ?-->do you know the real value of `q` right now? NO,nobody knows!
MAKES ERROR
来源:https://stackoverflow.com/questions/47780635/symsum-function-error-invalid-indexing-or-function-definition