MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49709] Re: [mg49666] Using "Sum" (i = 1 ... N) in a function definition
  • From: Murray Eisenberg <murray at math.umass.edu>
  • Date: Thu, 29 Jul 2004 07:43:26 -0400 (EDT)
  • Organization: Mathematics & Statistics, Univ. of Mass./Amherst
  • References: <200407271100.HAA11129@smc.vnet.net>
  • Reply-to: murray at math.umass.edu
  • Sender: owner-wri-mathgroup at wolfram.com

MyFunc[i_] := Fold[Plus, 0, Take[MyData, i]]

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[1] = 3
>  MyFunc[2] = 3 + 2 = 5
>  MyFunc[3] = 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."
> 
> Does anybody know how to define (correctly) such a function. Should be
> possible ...?
> Thanks
> Rainer
> 
> 

-- 
Murray Eisenberg                     murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305


  • Prev by Date: Re: Combinations
  • 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