# Summation equations in matlab

15 views (last 30 days)
William on 3 Jan 2015
Edited: Shoaibur Rahman on 3 Jan 2015
How can I write this equation into Matlab?
Is it equivalent to....
SOAM = (CP(1)+CP(2)+CP(3))/ [P(1) - P(0)]) ?
I'm not so hot on the summation notation.
Cheers

Shoaibur Rahman on 3 Jan 2015
Edited: Shoaibur Rahman on 3 Jan 2015
You can try with this:
SOAM = sum(CP(1:n-3))/sum(abs(P(2:n)-P(1:n-1)))
The 2nd sum is confusing because of the indexing (k and k-1). Lets say, n = 5 , then the last sum will look like:
n-1
sum |p(k) - p(k-1)| = |p(1)-p(0)| + |p(2)-p(1)| + |p(3)-p(2)| + |p(4)-p(3)|
k=1
But in Matlab, 0 indexing is not possible. The first element is indexed as 1. So, shift the index by 1, which will look like, and tractable in Matlab:
|p(2)-p(1)| + |p(3)-p(2)| + |p(4)-p(3)| + |p(5)-p(4)|
Therefore, first index is 2:5 that is 2:n and second index is 1:4 that is 1:n-1

### More Answers (2)

Star Strider on 3 Jan 2015
The equation is a bit difficult to read. The second P subscript is ‘k-minus-something’, but I can’t figure out what the ‘something’ is.
This part of your interpretation is correct:
(CP(1)+CP(2)+CP(3))
There is no ‘zero index’ in MATLAB (indices are always integers greater than zero), so P(0) is incorrect with regard to your coding it in MATLAB. In practical terms, that means you have to incorporate the appropriate offsets in your code, so that (in this instance), the numerator and denominator both refer to the appropriate elements in your CP and P vectors, respectively.

John D'Errico on 3 Jan 2015
Edited: John D'Errico on 3 Jan 2015
The numerator is a simple sum of terms. As you write it, it is simply
CP(1) + CP(2) + ... + CP(n-3)
A sum is just a sum is just a sum. In MATLAB, you might write that as
sum(CP)
assuming there are n-3 terms in the vector CP. If CP has more terms than that, then the numerator might be simply written as
sum(CP(1:(n-3)))
However, the denominator is NOT what you have written. Expand the sum, and we get...
|P(2) - P(1)| + |P(3) - P(2)| + |P(4) - P(3)| + ... + |P(n-1) - P(n-2)|
where stuff is the absolute value of stuff, thus abs(stuff). If the vector of elements represented by P is not sorted by their value, then sometimes P(k) will be greater than P(k-1), sometimes the reverse will be true. In that case, then the absolute value will essentially subtract those terms in the opposite order. Think of it as if the absolute value inserts a minus sign in there when needed.
However, IF it is true that P is sorted in order, then that sum could be compressed down by some amount, but it still matters if some of the elements of P were negative. So I would suggest that you not try to compress that denominator sum. (And no matter what, it would not be what you have written.) Write the denominator as something like this:
sum( abs( P(1:(n-1)) - P(1:(n-2)) ) )
(And, it is indeed true that P(0) simply does not exist in MATLAB. MATLAB uses a 1-based index system.)