MATLAB: How can i eliminate below error

binary to decimal conversionrange(-10 10)

function x = binary2decimal( b , xl , xu ,q )
syms p
s=symsum(((2^p)*b(p)),p ,1 ,(q-1))
x = (xl+((xu-xl)*(s/((2^q)-1))))
error
Invalid indexing or function definition. When defining a function, ensure that the body of the function is a SYM object.
When indexing, the input must be numeric, logical or ':'.
Error in binary2decimal (line 3)
s=symsum(((2^p)*b(p)),p ,1 ,(q-1))

Best Answer

You cannot use a symbolic variable as an index into an array, not even within a symsum() or symprod() .
Instead you need to create all of the elements as a vector, and sum() them together.
There is no good reason to use the symbolic toolbox here at all.
p = 1 : q-1;
s = sum( 2.^p .* b(p) );
This is equivalent to what you tried to code. It deliberately has the same bug you had. (Hint: why does it only produce even results ?)