Re: Plotting vector-valued functions
- To: mathgroup@smc.vnet.net
- Subject: [mg10531] Re: [mg10456] Plotting vector-valued functions
- From: seanross@worldnet.att.net
- Date: Tue, 20 Jan 1998 02:23:17 -0500
- References: <199801160934.EAA08223@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 I tend to use the "function that remembers its values" construct. For example: f[x_]:=f[x]=Module[{},Return[{f1,f2,f3}]] g[x_]:=Module[{},Return[{g1,g2,g3}]] This function returns a vector. Suppose I want to graph all three components on the same graph Plot[{f[x][[1]],f[x][[2]],f[x][[3]]},{x,low,high}] The f function executes once for every point. The second and third values are just look-ups. On the other hand Plot[{g[x][[1]],g[x][[2]],g[x][[3]]},{x,low,high}] The g function executes 3 times for every point. If memory is a problem, then you can Clear it after the graph. -- Remove the _nospam_ in the return address to respond.
- References:
- Plotting vector-valued functions
- From: Malcolm Boshier <m.g.boshier@sussex.ac.uk>
- Plotting vector-valued functions