MathGroup Archive 2009

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

Search the Archive

Re: Manipulating list of functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg96837] Re: Manipulating list of functions
  • From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
  • Date: Thu, 26 Feb 2009 07:52:17 -0500 (EST)
  • References: <go31hn$fa1$1@smc.vnet.net>

Hi Syd,

Your small program contains quite a number of errors and/or matters of
programming style:

1. The line

 funcList := #[x] & /@ {nbrshares, initialinvestment, profit,
totalofholding};

gives an argument to all symbols in the list even to ones you don't
have defined as having one (initialinvestment, totalofholding)

2. The line

 totalofholding := profit + initialinvestment;

uses profit without argument wheras you have defined it as a function
with an argument.

3. The two functions that you do define use buyprice as the single
argument. Especially in profit I'd put at least finalprice in the
argument list. Function definitions containing variables and
parameters that are not part of the argument list have a great
potential for programming errors. As least the variables used should
be present in the argument list.

4. It's not good programming style to redefine functions and constants
(that have not changed) over and over again during the evaluation of
the body of Manipulate

5. You are plotting different values with different dimensions and
potentially considerable different value ranges in the same plot

The following works:

DynamicModule[{initialinvestment = 100000, nbrshares, profit,
  totalofholding},
 nbrshares[buyprice_] := initialinvestment/buyprice;
 profit[buyprice_, finalprice_,
   nbrshares_] := (finalprice - buyprice)*nbrshares;
 totalofholding[buyprice_, finalprice_, nbrshares_] :=
  profit[buyprice, finalprice, nbrshares] + initialinvestment;
 Manipulate[
  Plot[
   {
    nbrshares[buyprice],
    initialinvestment,
    profit[buyprice, finalprice, nbrshares[buyprice]],
    totalofholding[buyprice, finalprice, nbrshares[buyprice]]
    },
   {buyprice, 4, 7}, Axes -> False, Frame -> True, PlotRange -> All
   ],
  {finalprice, 16, 24, 1}
  ]
 ]

Cheers -- Sjoerd

On Feb 25, 11:03 am, Syd Geraghty <sydgerag... at me.com> wrote:
> Hi all,
>
> we recently had a thread regarding manipulating a list of functions  
> which worked fine:-
>
> Manipulate[Block[{funcA, funcB, funcC, funcList},
>    funcA[z_] := Sin[z + a];
>    funcB[z_] := Cos[z + b];
>    funcC[z_] := funcA[z] + funcB[z];
>    funcList = #[x] & /@ {funcA, funcB, funcC};
>    Plot[Evaluate[funcList], {x, 1, 10}, Axes -> False, Frame ->  
> True]], {a, 0,
>    Pi}, {b, 0, Pi}]
>
> I have tried what I thought would be a trivial a variant of this for  
> very simple functions:
>
> Manipulate[
>   Block[{initialinvestment, nbrshares, profit, totalofholding,  
> funcList},
>    initialinvestment := 100000;
>    nbrshares[buyprice_] := initialinvestment/buyprice;
>    profit[buyprice_] := (finalprice - buyprice)*nbrshares;
>    totalofholding := profit + initialinvestment;
>    funcList := #[x] & /@ {nbrshares, initialinvestment, profit,  
> totalofholding};
>    Plot[Evaluate[funcList], {x, 4, 7}, Axes -> False, Frame -> True,
>     PlotRange -> {0, 650000}]], {finalprice, 16, 24, 1}]
>
> However I only get the nbrshares plotted, after several attempts I  
> cannot get the desired output and would appreciate advice as to why  
> the simpler function specifications lead to failure.
>
> It appears that unless the functions all contain explicit references  
> such as to "z" in the first working example then they are omitted.
>
> Thanks in advance ... Syd
>
> Syd Geraghty B.Sc, M.Sc.
>
> sydgerag... at mac.com
>
> Mathematica 7.0.0 for Mac OS X x86 (64 - bit) (21st November, 2008)
> MacOS X V 10.5.6
> MacBook Pro 2.33 GHz Intel Core 2 Duo  2GB RAM



  • Prev by Date: Re: Re: listing user defined, independent variables
  • Next by Date: Re: Map and functional constructs to replace iterative
  • Previous by thread: Re: Re: Manipulating list of functions
  • Next by thread: OpenSQLConnection too many times causes complete hang.