Contents - Index

SUM

There are two forms for the SUM function.  EES determines which format is in use by context.

Index Form

SUM(Arg, i=1,50) returns the sum of a series of terms.  The index is represented as i here but it can be any EES variable name.  The lower and upper limits must be integers or variables that have been previously assigned to integer values.  The function is best explained by examples. SUM(j, j=1,4) will return 1+2+3+4 or 10.  The SUM function is most useful when used with array variables, e.g., X[j].  For example, the scalar product of two vectors, X and Y, each with 10 elements can be obtained as SUM(X[j]*Y[j], j=1,10).  The SUM function can be used with array variables to manipulate matrices.  Note that the limits appear in the Sum command must be integers or EES variables that have been previously set to integer values.  The following equations would be valid.

N=50

S=SUM(X[i], i=1,N)

Note:  When used with Functions and Procedures, the index limits must be known during compilation. This requirement means that index limits cannot be passed as parameters or calculated within the Function/Procedure.  For example, the following Function will not work,

Function sumit(a,b  {This function will not work because a and b are not known at compile time}

X[1..10]=[1,2,3,4,5,6,7,8,9,10]

sumit=Sum(X[i],i=a,b)

end

g=sumit(2,3)  {will produce an error}

As an alternative, a Repeat-Until group should be used in place of the Sum function, as illustrated below.

Function sumit(a,b)   {This function will work properly}

X[1..10]=[1,2,3,4,5,6,7,8,9,10]

i=a

sumit=0

Repeat

sumit=Sumit+X[i]

i=i+1

Until (i>b)

End

g=sumit(2,3)   {this will work}

List Form

SUM(Arg1, Arg2, ...ArgN) returns the sum of the arguments.  Array range notation is particularly convenient for the list form.  For example, SUM(X[1..100]) returns the sum of the 100 elements in the X array. Note, however, that mathematical operations within the List Form of the Sum function is not provided.  For example:

Z=SUM(0.1*X[1..100]) "This will not work!!!!!'

If mathematical operations are required, do them in separate equations or use the Index Form.

Mathematical functions