# one liner for a function?

0 views

### sean kim

Jan 30, 2004, 4:51:36 AM1/30/04
to
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==1, b==1,
x==1,
y==0}, {a,b,x,y},{t,0,250}][];

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

### Bob Hanlon

Jan 31, 2004, 5:26:51 AM1/31/04
to
Needs["Graphics`"];

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==1,b==1,x==1,y==0},
{a,b,x,y},{t,0,250}][];

Plot[Evaluate[#[][t]& /@ sol],{t,0,250},
PlotStyle->Table[Hue[(k+2)/6],{k,4}],
Frame->True, Axes->False,
PlotLegend->(#[]& /@ sol),
LegendPosition->{.95,-.35},ImageSize->400];

n=1;DisplayTogetherArray[
Partition[Plot[Evaluate[#[][t]],{t,0,250},
PlotStyle->Hue[(2+n++)/6],
PlotLabel->#[],
Frame->True, Axes->False]& /@ sol,2],
ImageSize->400];

Bob Hanlon

### Bobby R. Treat

Jan 31, 2004, 5:41:10 AM1/31/04
to
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 == 1, b == 1, x == 1, y ==

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 == 1, b == 1, x == 1, y ==

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 == 1, b == 1, x == 1, y ==

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_...@yahoo.com (sean kim) wrote in message news:<bvd9f8\$5k1\$1...@smc.vnet.net>...