MathGroup Archive 2009

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

Search the Archive

Re: Function of list of functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104058] Re: Function of list of functions
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Sat, 17 Oct 2009 07:03:31 -0400 (EDT)

On 10/16/09 at 7:17 AM, oradev at rambler.ru (Andrey) wrote:

>May be anybody can help me. So, I have this:

>L = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
>Fun1[t_] := ListPlot[L[[1, t]]];
>Fun2[t_] := ListPlot[L[[2, t]]];
>Fun3[t_] := ListPlot[L[[3, t]]];

>FunRes[t_] := {Fun1[t], Fun2[t], Fun3[t]};

>Everything is ok, BUT if I want to add element to list of FunRes[t_]
>in loop, I have an error? so I mean this:

>FunRes[t_] := {};
>For[i = 1, i <= 3, i++,
>{Subscript[Fun, i][t_] := ListPlot[L[[i, t]]],
>AppendTo[FunRes[t], Subscript[Fun, i][t]]}]

>Please, help me, where I am wrong?

Where to start?

To begin with functions are not lists. You can add elements to a
list but you cannot add elements to a function. And even if you
could do this, the code you posted cannot work the way you want
since your last definition for FunRes returns an empty list for
all arguments.

Next in the For loop you have the statement

Subscript[Fun, i]

This literally says take a symbol named Fun add a subscript to
it. This has absolutely nothing to do with any of the other
functions you've defined That is Subscript[Fun, 1] has no
relationship whatever with Fun1. Nor is Subscript[Fun, 1]
defined as a function. So trying Subscript[Fun, i][t] will makes
no sense.

In addition, no where in the For loop have you given t a value.
Consequently, even if you were correctly calling your predefined
functions you would get errors since those definitions will only
work properly when t has be assigned an a value which must be
either 1, 2 or 3 given the dimensions of L.

I am guessing what your goal is to create a list of plots. The
most efficient way I know to do this would be to use Map to map
ListPlot to each data set to be plotted. That is

ListPlot/@L

or

Map[ListPlot, L]

will generate the plots.

A few more comments. While it is not an error to use upper case
letters for variable names or function names, it is a very good
idea to get out of the habit of doing so with Mathematica. All
built-in objects in Mathematica start with an uppercase letter.
By starting anything you create with a lowercase letter, you
ensure no conflicts between you definitions and built-in
definitions will occur. Such conflicts can easily arrise
especially when using a single uppercase letter as a variable
name. For example, C, D, E and N all have built-in definitions.
Note, this is intended as an example not a complete list.

It is also not an error to use For and AppendTo as you have
done. But both are very inefficient compared to other methods
available in Mathematica. To add elements to a list, it is far
more efficient to use nested lists and then use Flatten at the
end to remove the nesting.



  • Prev by Date: Re: Code folding and wrapping using the input->code method?
  • Next by Date: how to find with mathematica
  • Previous by thread: Re: Function of list of functions
  • Next by thread: Re: Function of list of functions