Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

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: [mg49701] Re: [mg49666] Using "Sum" (i = 1 ... N) in a function definition
  • From: János <janos.lobb at yale.edu>
  • Date: Thu, 29 Jul 2004 07:43:12 -0400 (EDT)
  • References: <200407271100.HAA11129@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

I would start with a fresh Mathematica session, or Clear all the 
variables...

On my machine - G4 OSX 10.3.4 and Mathematica 5.0.1 your code is 
working fine.

dlist = {a, b, c, d, e, f, g, h}

dfunc[i_] := Sum[Part[dlist, j], {j, 1, i}]

dfunc[5]

a + b + c + d + e

János
On Jul 27, 2004, at 7:00 AM, 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
>
>
---------
"During my service in the United States Congress, I took the initiative 
in
creating the Internet." -- Al Gore
"If Al Gore invented the internet, I invented spell check." -- Dan 
Quayle
--- borrowed from  Deirdre Saoirse   *   http://www.linuxcabal.org   *  
  http://www.deirdre.net


  • Prev by Date: Re: GraphSum ignores edge colors?
  • Next by Date: Re: Combinations
  • 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