[Date Index]
[Thread Index]
[Author Index]
Re: Using "Sum" (i = 1 ... N) in a function definition
*To*: mathgroup at smc.vnet.net
*Subject*: [mg49691] Re: [mg49666] Using "Sum" (i = 1 ... N) in a function definition
*From*: Matthias.Bode at oppenheim.de
*Date*: Thu, 29 Jul 2004 07:42:55 -0400 (EDT)
*Sender*: owner-wri-mathgroup at wolfram.com
Hello Wilhelm,
try:
mydata = {3, 2, 4, 5, 6};
myfunc[n_] := Plus @@ Take[mydata,
n]
Best regards,
Matthias Bode
Sal. Oppenheim jr. & Cie. KGaA
Untermainanlage 1
D-60329 Frankfurt am Main
GERMANY
Tel.: +49(0)69 71 34 53 80
Mobile: +49(0)172 6 74 95 77
Fax: +49(0)69 71 34 95 380
E-mail: matthias.bode at oppenheim.de
Internet: http://www.oppenheim.de
-----Ursprüngliche Nachricht-----
Von: wilhelm.rainer at gmx.net [mailto:wilhelm.rainer at gmx.net]
Gesendet: Dienstag, 27. Juli 2004 13:01
An: mathgroup at smc.vnet.net
Betreff: [mg49666] Using "Sum" (i = 1 ... N) in a function definition
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
Prev by Date:
**Re: Using "Sum" (i = 1 ... N) in a function definition**
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**
| |