0
$\begingroup$

In Mathematica, I want to do a mass export of many different animated plots of specific functions with different domains. Because I don't necessarily want the plots to always have exactly the same range for each graph, (for example, -1 to 1 on X and -1 to 1 on Y), I want to manually specify this value for each function I create.

For example, in Python, I could do something like this:

def g1(x):
  return x*x + 1
x_range1 = (-2, 2)
y_range1 = (0, 4)

def g2(x):
  return math.log(x)
x_range2 = (0, 8)
y_range2 = (-4, 4)

def g3(x):
  return 2*math.cos(x)
x_range3 = (-4, 4)
y_range3 = (-4, 4)

func_list = [(eval(f'g{ii}'), eval(f'x_range{ii}'), eval(f'y_range{ii}')) for ii in range(1, 3 + 1)]

for function, x_range, y_range in func_list:
  print(function, x_range, y_range)

And I would get the output:

<function g1 at 0x000002063F0EF670> (-2, 2) (0, 4)
<function g2 at 0x000002063F0EF700> (0, 8) (-4, 4)
<function g3 at 0x000002063F0EF790> (-4, 4) (-4, 4)

Basically, I'm able to easily create an arbitrarily long list of functions and domains and then access them however I need to in the for loop. I'm sure that something similar is possible in Mathematica, I just don't know the syntax.

I could just create a list of single plot objects, I suppose, but there might be other aspects that I want to be the same between all of the graphs (such as the line color, axis font, etc.). So really it would be ideal if I could do it very similar to the way I did in Python where I only specify the aspects specific to each function $g_i$ and then force those into a plot object within the for loop so that only the function and range changes, but not the other arguments.

I don't know if Mathematica can do what I'm looking for, ideally in a way similar to what I presented in the Python code above. If so, does anybody have an example?

$\endgroup$
1
$\begingroup$
Clear["Global`*"]

pltData = {{Sin[x], {0, 2 Pi}, {-1, 1}},
   {y*Cos[y], {-2 Pi, 2 Pi}, {-6, 6}},
   {{Sqrt[5^2 - x^2], -Sqrt[5^2 - x^2]}, {-5, 5}, {-5, 5}},
   {{z^2 - z - 6}, {-3, 4}, Automatic}};

Column[Plot @@@ ({#[[1]], 
      Insert[#[[2]], Variables[Level[#[[1]], {-1}]][[1]], 1], 
      PlotRange -> #[[3]]} & /@ pltData)]

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ Does Mathematica have any kind of reflection? For example, if I have several functions named g1, g2, ... gn, could I easily create a list of all g functions without manually adding each one to the list each time I add a new one, similar to what I did in the Python code? $\endgroup$
    – jippyjoe4
    Apr 7 '21 at 5:36
  • 1
    $\begingroup$ I'm not a programmer, I don't know Python nor do I know what "reflection" means. You appear to be defining each function. If you have defined g1[x_] := ... then you can use g1[x] in pltData. $\endgroup$
    – Bob Hanlon
    Apr 7 '21 at 5:45
  • 1
    $\begingroup$ @jippyjoe4, more or less you can do exactly what you did in your question with the Python code, and maybe you can get away without even knowing the range ahead of time if you define your functions creatively enough $\endgroup$ Apr 7 '21 at 6:27
1
$\begingroup$

Like this?

g1[x_] = x*x + 1;
g2[x_] = Log[x];
g3[x_] = 2 Cos[x];
funclist = "g" <> ToString[#] & /@ {1, 2, 3} // ToExpression;
Plot[#[x] & /@ funclist, {x, -5, 5}]

enter image description here


Better way:

g[1][x_] = x*x + 1;
g[2][x_] = Log[x];
Plot[g[#][x] & /@ {1, 2}, {x, -5, 5}]

Actually, it's not recommended to do that in Python. eval is evil. You could use lambda.

funclist = [lambda x: x * x + 1, lambda y: 1]
for i in funclist:
    print(i)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.