MathGroup Archive 2005

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

Search the Archive

Re: a question about plot a list of functions.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg58207] Re: [mg58203] a question about plot a list of functions.
  • From: Andrzej Kozlowski <andrzej at akikoz.net>
  • Date: Wed, 22 Jun 2005 03:29:33 -0400 (EDT)
  • References: <200506220555.BAA00713@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 22 Jun 2005, at 14:55, Zhou Jiang wrote:

> *This message was transferred with a trial version of CommuniGate 
> (tm) Pro*
>
> I defined a function as
>
> K=(2 r/(1-r^2))^2;
> y=1/(1+K (Sin[x])^2);
> y=y/.{r->{0.2,0.9,0.995}}
>
> Now the result is a list.
> I want to plot y vs. x in one figure and I did the following
>
> Plot[y, {x, 0, 10 Pi}];
>
> But Mathematica gave me an error.
> I changed it to
>
> Plot[{y[[1]], y[[2]], y[[3]]}, {x, 0, 10 Pi}];
>
> Mathematica gave me a correct plot. Can anyone give me some idea  
> why the error message is given by Mathematica since y is a list and  
> nothing is diffrent from {y[[1]], y[[2]], y[[3]]}?
>
> Thanks.
>

Mathematica uses a different algorithm for plotting a single function  
with

Plot[f[x],{x,a,b}]

and for plottign sevral function,a s wiht

Plot[{f[x],g[x]},{x,a,b}]

Plot has the Attribute HoldAll so it does not evaluate it's  
arguments. So if you are going to plot a list of functions  
Mathematica has to be able to see that the first argument has the  
Head List, otherwise it will try using the wrong algorithm.

In your case you are plotting y, but why is a symbol, not a list. It  
only becomes a List after evaluating. So the following will work fine:

Plot[Evaluate[y], {x, 0, 10 Pi}];

In this case Mathematica first evaluates y, sees that it has Head  
List and then uses the correct algorithm (for plotting multiple  
functions).

Your second approach

Plot[{y[[1]], y[[2]], y[[3]]}, {x, 0, 10 Pi}]

also works because again Mathematica can see the the Head List and  
applies the correct algorithm.

But with

Plot[y, {x, 0, 10 Pi}]

the wrong algorithm (for plotting a single real valued function) is  
used and naturally Mathematica complains that your function values  
are not real numbers (they are actually lists  of real numbers).

Andrzej Kozlowski

Chiba, Japan


  • Prev by Date: Re: a small problem from a newbee...
  • Next by Date: Re: a small problem from a newbee...
  • Previous by thread: a question about plot a list of functions.
  • Next by thread: Re: a question about plot a list of functions.