Subject: Graphing a function for several parameter values
From: Jaime Villate
Date: Mon, 13 Feb 2012 14:36:22 +0000
On 02/13/2012 12:17 PM, Patrick Blanchenay wrote:
> I would like to plot a function several times for different values of
> parameters.
>
> The function depends on a set of parameters, say paramOne and paramTwo.
> I would like to be able to specify several sets of those parameters, and
> for each set, get the function graphed.
>
> I do not want the number of parameter sets to be hard-coded, so I
> thought about using a for ... in ... do loop. This is what I tried, but
> it doesn't seem to work:
>
> parametersList: [
> [paramOne=valueOne, paramTwo=valueTwo],
> [paramOne=valueThree, paramTwo=valueFour],
> ]$
> for parameterSet in parametersList do
> block(
> i++,
> functionList[i] : ev(function(x,paramOne,paramTwo),parameterSet),
> return functionList
> )
> wxplot2d(functionList,[x,0,1])$
Hi,
welcome to the list. You have several errors.
1- The statement "functionList[i]: ..." will not create a list but an
array. By the
end of the loop, functionList will not refer to the whole array, but
rather to another
variable, different from the array you created, and thus undefined. In
any case,
the plot command expects a list of functions and not an array.
2- i++ does not work in Maxima (unless you defined the postfix operator
++); you
should have used i:0 .... i: i+1
3- return functionList will also fail; perhaps return(functionList) but
it will not work as
you expect from other programming languages. For that type of for loop
you do not
need to return anything from the loop.
I recommend that you use makelist() to create your list of functions.
Something like this:
(%i2) f (x,A,w):= A*sin(w*x)$
(%i3) plist: [ [p1=1, p2=2], [p1=3, p2=4] ]$
(%i4) flist: makelist ( ev(f(x,p1,p2), p), p, plist);
(%o4) [sin(2*x),3*sin(4*x)]
(%i5) plot2d ( flist, [x, 0, 7]);
As a matter of personal taste, I find simpler to use expressions, even
though in this case the
result is the same:
(%i2) f: A*sin(w*x)$
(%i3) plist: [ [A=1, w=2], [A=3, w=4] ]$
(%i4) flist: makelist (subst (p,f), p, plist);
(%o4) [sin(2*x),3*sin(4*x)]
(%i5) plot2d ( flist, [x, 0, 7]);
Cheers,
Jaime