Tigran
Tigran

Reputation: 21

summation of symbolic variables in Matlab

I want to make a summation of symbolic variables constructed by,

 x =  transpose(sym('x',[1 5]))

I thought I could just call x1 by x(1). Hence I did the following,

syms p 
symsum(p^(i)*x(i),i,1,5)

Unfortunately I got the following error

Array indices must be positive integers or logical values.

Is there a way to fix this error ?

Upvotes: 2

Views: 428

Answers (1)

Sardar Usama
Sardar Usama

Reputation: 19689

According to the documentation of symsum, the variable for range (i in your case should be a symbolic variable). You haven't defined i as a symbolic variable and hence it is considered to be the imaginary unit (which is its default builtin definition). It is always wise to avoid the use of i and j as variables since these are for imaginary unit in MATLAB.

However fixing just this won't solve all the problems. Apparently indexing a symbolic variable is not allowed in the function definition for symsum. To do your intended operation, I would use sum like this:

x =  sym('x', [1 5]);   
syms p;
sum(p.^(1:5) .* x)

which gives:

ans = 
x5*p^5 + x4*p^4 + x3*p^3 + x2*p^2 + x1*p

Upvotes: 1

Related Questions