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: one liner for a function?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg45978] Re: one liner for a function?
  • From: drbob at bigfoot.com (Bobby R. Treat)
  • Date: Sat, 31 Jan 2004 05:20:45 -0500 (EST)
  • References: <bvd9f8$5k1$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

How's this?

sol = First@
    NDSolve[{a'[
      t] == -0.1` a[t]
         x[t], b'[t] == -0.05` b[t] y[t], x'[t] == -0.1` a[t] 
          x[t] + 0.05` b[t] y[t], y'[t] == 0.1` a[t] x[t] - 0.05` b[t]
y[t],
            a[0] == 1, b[0] == 1, x[0] == 1, y[0] == 
              0}, {a, b, x, y}, {t, 0, 250}];
Show[GraphicsArray[
            Map[Plot[#[t] /. sol, {t, 0, 250},
               DisplayFunction -> Identity, PlotRange -> All, 
            PlotLabel -> #] &, {{a, b}, {x, y}}, {-1}]], ImageSize ->
500]

The level specification for Map could be {2} rather than {-1} just as
well. {2} means all elements at level two, while {-1} means all leaf
nodes and, for the matrix {{a,b},{x,y}}, the two are the same thing.

In either case, a pure function (Plot[...]&) is mapped onto those
elements of the matrix producing a matrix of Graphics objects, and
that's the input GraphicsArray needs.

Here's another solution for which the level specification can't be
{-1}. Notice the labels are different.

sol = First@
    NDSolve[{a'[
      t] == -0.1` a[t]
         x[t], b'[t] == -0.05` b[t] y[t], x'[t] == -0.1` a[t] 
          x[t] + 0.05` b[t] y[t], y'[t] == 0.1` a[t] x[t] - 0.05` b[t]
y[t],
            a[0] == 1, b[0] == 1, x[0] == 1, y[0] == 
              0}, {a[t], b[t], x[t], y[t]}, {t, 0, 250}];
Show[GraphicsArray[Map[Plot[# /. sol, {t,
               0, 250}, DisplayFunction -> Identity, PlotRange -> All,
           PlotLabel -> #] &, {{a[t], b[t]}, {x[t], y[
          t]}}, {2}]], ImageSize -> 500]

And this combines sol from the second solution with Show from the
first:

sol = First@
    NDSolve[{a'[
      t] == -0.1` a[t]
         x[t], b'[t] == -0.05` b[t] y[t], x'[t] == -0.1` a[t] 
          x[t] + 0.05` b[t] y[t], y'[t] == 0.1` a[t] x[t] - 0.05` b[t]
y[t],
            a[0] == 1, b[0] == 1, x[0] == 1, y[0] == 
              0}, {a[t], b[t], x[t], y[t]}, {t, 0, 250}];
Show[GraphicsArray[Map[Plot[#[t] /. sol, {t, 0, 250}, DisplayFunction
->
          Identity, PlotRange -> All, PlotLabel -> #] &, {{a, b}, {x,
y}}, {
          2}]], ImageSize -> 500]

Bobby

sean_incali at yahoo.com (sean kim) wrote in message news:<bvd9f8$5k1$1 at smc.vnet.net>...
> I have been asking questions for three straight night in a row... 
> 
> I appreciate all the replies. 
> 
> I have another couple of questions tonight. 
> 
> 1. let's say you have to make GraphicsArray using the interpolating
> functions.
> I have managed to write a function that automates the plotting part,
> but i haven't been able to write one that will automate plotting and
> naming the plot object and showing in a graphics array.
> 
> as an example, 
> 
> sol= NDSolve[{a'[t]==-0.1` a[t] x[t], b'[t]==-0.05` b[t] y[t], 
>           x'[t]==-0.1` a[t] x[t]+0.05` b[t] y[t], 
>           y'[t]==0.1` a[t] x[t]-0.05` b[t] y[t], a[0]==1, b[0]==1,
> x[0]==1,
>           y[0]==0}, {a,b,x,y},{t,0,250}][[1]];
> 
> pfunc[name_]:= Plot[Evaluate[{name[t]/. sol}, {t, 0, 250}],
> PlotRange-> All, DisplayFunction -> Identity, PlotLabel -> name];
> 
> pa = pfunc[a];
> pb = pfunc[b];
> px = pfunc[x];
> py = pfunc[y];
> 
> Show[GraphicsArray [{{pa, pb}, {px, py}}], ImageSize-> 500]
> 
> 
> funtion I wrote called "pfunc" does fine, but i was wondering is there
> a way to automate the
> 
> pa = pfunc[a]; ... py = pfunc[y];
>   
> and 
> 
> Show[GraphicsArray [{{pa, pb}, {px, py}}], ImageSize-> 500] 
> 
> part also?  
> 
> I can't seem to figure out a way to take the output of function to
> make another function.. and that seems to me what i have to do.. ( i
> don't even know that make sense to you guys...)
> 
> 
> 2. once you have everythign in function form, can you write it in one
> liner format? ( perhaps with some explanations? )
> 
> as always any and all thoughts and replies are thoroughly appreciated.
> 
> thanks all in advance. 
> 
> sean


  • Prev by Date: Re: Re: Nasty bug in Integrate (version 5.0)
  • Next by Date: Re: AW: Re: Nasty bug in Integrate (version 5.0)
  • Previous by thread: Re: one liner for a function?
  • Next by thread: Defining a function in module problem?