Re: Plotting vector-valued functions
- To: mathgroup@smc.vnet.net
- Subject: [mg10519] Re: [mg10456] Plotting vector-valued functions
- From: David Withoff <withoff@wolfram.com>
- Date: Tue, 20 Jan 1998 02:23:06 -0500
> 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 My two suggestions here are to cache the results from f[z], and/or to consider using a non-adaptive plotting function, such as ListPlot. Since Plot uses an adaptive algorithm, evaluating each function at different points depending on the features of the curve, it is very likely that Plot will evaluate each element in your list {f[z][[1]], f[z][[2]], f[z][[3]], f[z][[4]], f[z][[5]]} at different points. Although some of the evaluations will be at the same points, leading to the redundant evaluations that you mentioned, it is likely that many of the evaluations will be at different points for different curves. You can avoid the redundant evaluations by caching results, such as by defining your function using f[z_] := f[z] = ... . If you don't need the adaptive plotting algorithm, it is easy to do your own non-adaptive plotting, such as by constructing a list of values to plot (e.g. Table[f[z], {z, zmin, zmax}]), generating a list of plots using ListPlot, and using Show to display the plots together. Dave Withoff Wolfram Research