       Re: Using "Sum" (i = 1 ... N) in a function definition

• To: mathgroup at smc.vnet.net
• Subject: [mg49713] Re: Using "Sum" (i = 1 ... N) in a function definition
• From: mjperson at mit.edu (Michael J Person)
• Date: Thu, 29 Jul 2004 07:43:33 -0400 (EDT)
• Organization: Massachvsetts Institvte of Technology
• References: <ce5d8p\$b4k\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Using a SetDelayed (:=) instead of a Set (=) seems
to be what you need here...

MyData = {3, 2, 4, 5, 6};

MyFunc[i_] := Sum[MyData[[j]], {j, 1, i}];
(*Notice the := above!*)

MyFunc
5

-MJ Person
mjperson at mit.edu

wilhelm.rainer at gmx.net (Rainer) wrote:
>I was trying to define a function using the Sum command in the
>following way (simplified example):

> MyData = {3,2,4,5,6};
> MyFunc[i_] = Sum[MyData[[j]],{j,1,i}];

>As you can see, the idea is to use the length of the list (i)
>as a variable in the function definition, such that

> MyFunc = 3
> MyFunc = 3 + 2 = 5
> MyFunc = 3 + 2 + 4 = 9

>etc. Mathematica does not like the abobe statements, and prints the
>following error message:

>"Part specification K\$394 is neither an integer nor a list of
>integers."

```

• Prev by Date: Re: Using "Sum" (i = 1 ... N) in a function definition
• Next by Date: Re: Using "Sum" (i = 1 ... N) in a function definition
• Previous by thread: Re: Using "Sum" (i = 1 ... N) in a function definition
• Next by thread: Re: Using "Sum" (i = 1 ... N) in a function definition