Symbolic Sum to find a general expression for arbitrary n terms and arbitrary variable definitions

15 views (last 30 days)
Hello, I have a need to add some terms defined using an index for the index running from 1 to and arbitrary number n.
I have equations similar to (actual equations are too long but the problem is the same):
where is an arbitrary number associated with
Goal:
I wish to find the average of these terms running from
I know I can use symsum for accomplishing this task.
syms x m
e = x + p_{m} % I want to know how to define this p_{m}
symsum(e, m, 1, n)
But I have the following question:
Question:
1) How do I define arbitrary symbolic variables 's for each equation. So that when I get the final sum of all the symbolic variables from 1 to n it will contain .
Please provide example code.
  2 Comments
atharva aalok
atharva aalok on 24 Sep 2022
Edited: atharva aalok on 24 Sep 2022
Ok I got some lead. I got how to create a symbolic variable with an arbitrary name.
syms x n
syms(sprintf('p%s', n)) % Create symbolic variable with name p_{n}
whos
Name Size Bytes Class Attributes cmdout 1x33 66 char n 1x1 8 sym pn 1x1 8 sym x 1x1 8 sym
Now the only problem left is to run symsum() and to create a new variable and use it in the sum everytime.
That is to make the declaration in the same statement where I use the variable

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 24 Sep 2022
You cannot do that in MATLAB for symbolic limits.
For specific limit, n being a given positive integer, then you would not use symsum for this purpose. Instead,
n = a positive integer
syms a [1 n]
syms x [1 n]
y = sum(a.*x)
That is, you create vectors or arrays in which each entry is a scalar symbol. You then do vectorized operations, producing a vector or array of definite terms. You then sum() the definite terms.
symsum is not primarily for creating a sum of definite terms: symsum is primarily for trying to find closed formulas for indefinite or infinite summations.
Under no circumstances can you use a symbolic variable as an array index.
  4 Comments
Walter Roberson
Walter Roberson on 24 Sep 2022
So what would be passed to the sprintf is not going to be the "current" value of k for a series of values 1 to n: what is going to be passed to sprintf is the unresolved symbol k. So the sprintf would be invoked once to create literal 'pk' which gets you symbol pk and x + pk would get passed to symsum where that pk has no more connection to k than x does.
symsum will then internally subs(EXPRESSION, k, n) and subs(EXPRESSION, k, n+1) and possibly other terms and will work with those to try to come up with a closed formula. The difference would be 0 because x+pk contains no reference to k so symsum will predict the formula (x+pk) * (n-1+1). symsum will not add up the terms one by one.

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!