Re: Plotting vector-valued functions
- To: mathgroup@smc.vnet.net
- Subject: [mg10550] Re: Plotting vector-valued functions
- From: Allan Hayes <hay@haystack.demon.co.uk>
- Date: Tue, 20 Jan 1998 16:53:54 -0500
- References: <69na52$860@smc.vnet.net>
Malcolm Boshier wrote:
>
> I have a problem which is related to the recent thread about
> plotting lists of functions. In the case when a vector-valued function
> is expensive or impossible to Evaluate before plotting, Plot apparently
> forces you to evaluate the function repeatedly at each value of the
> independent parameter. This can be very inefficient.
> As an example, suppose that f[z] returns the eigenvalues of a 5 x 5
> matrix which is a function of z. In general this function cannot be
> evaluated without a value for z, so
> Plot[ Evaluate[f[z]], {z, zmin, zmax}] doesn't work.
> The only way around this that I have found is something like:
>
> Plot[{f[z][[1]], f[z][[2]], f[z][[3]], f[z][[4]], f[z][[5]]}, {z, zmin,
> zmax}]
>
> which of course requires 5 evaluations of f[z] for each value of z.
> It seems that unless the head of the first argument to Plot is List,
> Plot assumes that it will evaluate to a real number and returns with an
> error if it later finds that it doesn't. Why can't Plot trust the user
> long enough to discover that the function will evaluate to a list?
> Thanks for any solutions or explanations, Malcolm
Malcolm
I'll take
f[z_] := Chop[Eigenvalues[Table[Sin[(n m)z],{n,5},{m,5}]]]
(symmetric matrix, so real eignvalues, but could be spurious small
imaginary parts, so use Chop)
Following your recipe:
Plot[{f[z][[1]],f[z][[2]],f[z][[3]],f[z][[4]],f[z][[5]]},
{z,0,1}]//Timing//First
11.3833 Second
One problem is that Plot[{f,g}, {x,xmin, xmax}] seems to compute the
lines for f and g separately and then show them together (this is so
even if we set PlotDivision -> 1).This means that the adaptive sampling
usually chooses different point for the two functions. So remembering
everything that was found for f might not help in calculating for g.
For example,
Plot[{0,Sin[10x]},{x,0,1}, PlotDivision->1]
The number of points used for the two lines is
Cases[%,ln_Line:> Length@@ln, Infinity]
{25,34}
Here is a faster way, but it is not adaptive and so gives a poorer plot.
Show[Graphics[Line/@
Transpose[Thread/@Table[{z,f[z]}, {z,0.,1., .005}]]],
Axes->True]//Timing//First
2.46667 Second
But we see that the first plot seems to have lost some features near z =
.36. Let's see -
Plot[{f[z][[1]],f[z][[2]],f[z][[3]],f[z][[4]],f[z][[5]]}, {z,0,1},
PlotPoints -> 200]//Timing//First
21.6833 Second
--
Allan Hayes
Training and Consulting
Leicester, UK
hay@haystack.demon.co.uk
http://www.haystack.demon.co.uk
voice: +44 (0)116 271 4198
fax: +44 (0)116 271 8642