0

i have a question regarding sums in MATLAB using the symsum function.

enter image description here

I want to implement this function where ti is the i-th value from the array:

t={2, 5, 6, 10} as an example for n=4

and U is a symbolic variable.

is that somehow possible?

syms i, u
t={2, 5, 6, 10}    
symsum((u-i+1)*t{i},i,1,10);

Matlab then gives me the error:

Error using sym/subsindex (line 769)
Invalid indexing or function definition. When defining a function, ensure that the arguments are
symbolic variables and the body of the function is a SYM expression.

Can someone help me? Thanks in advance

Edit: Changed example formula

5
  • 2
    I think you need [] instead of {} Commented Apr 30, 2018 at 10:42
  • Thanks for your answer.Unfortunately the [] not helped. I still get the same error Commented Apr 30, 2018 at 10:44
  • So you want to symsum a numerical array? Commented Apr 30, 2018 at 10:52
  • What would be the expected output? Commented Apr 30, 2018 at 10:57
  • The expected output would be: link Commented Apr 30, 2018 at 11:26

1 Answer 1

1

You are mixing numerical calculations and symbolic calculations. Which is also written in the error message:

Function arguments must be symbolic variables, and function body must be sym expression.

t is not a symbolic expression, it is numerical (it only contain numbers). The solution is to align your method to either direction. In this case it means to the numerical version as symbolic indexing does not make sense.

Numerical

Use standard sum (this is also the fastest):

i = 1:4
syms U0
t = [2, 5, 6, 10];
f(U0) = sum((U0-i+1).*t);
Sign up to request clarification or add additional context in comments.

3 Comments

or, sum(t) even easier.
agreed, if you want all, then sum(t) is easier, but as i is explicitly written in his picture, I figured that I would make it explicit as well, such that if we want to sum from e.g. 2:3 this was easily changed.
thanks for the answer. Yes this was just a dumb example from me. The formula i want to solve actually is a mix between a symbolic and a numeric part inside the sum. Is it possible to mix these two? --> I edited the asked question to clarify the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.