1
$\begingroup$

I defined a function funloglogplot to make a loglogplot of a function (function1) depending on several variables

funloglogplot[function1_, xmin_, xmax_, numpoints_] := 
  Module[{list1, listRe, listIm},
         list1 = {#1, function1[eps1, 1, #1, R1ideal, R2, ordre]}& /@ 
         logspace[xmin, xmax, numpoints];
         listRe = {#1[[1]], Re[#1[[2]]]}& /@ list1;
         listIm = {#1[[1]], -Im[#1[[2]]]}& /@ list1;
         ListLogLogPlot[{listRe, listIm}, Joined -> True] 
        ]

Nevertheless I would like to be able to choose the variables I am using for the plot (in this case I plot function1 as a function of its 3rd argument and I would like to be able to select the argument).
Can anyone see a solution?

$\endgroup$
4
  • $\begingroup$ Something like this? f[x_, y_] := Sin[x/y]; funPlot[f_, var_] := Plot[f, var]; GraphicsRow[{funPlot[f[a, 1], {a, 1, 2}], funPlot[f[1, a], {a, 1, 2}]}] $\endgroup$ Apr 22, 2014 at 12:24
  • $\begingroup$ No. I would like to parametrize the argument of my function. It means to be able to put the #1 in an other place for example like this function1[eps1, 1, 1, #1, R2, ordre] (the #1 is at fourth position this time) $\endgroup$ Apr 22, 2014 at 12:38
  • $\begingroup$ Instead of #1 I used a. What other difference is between my try and your intent? $\endgroup$ Apr 22, 2014 at 12:44
  • $\begingroup$ Yes but how can I use the Map (or &/@) with this formulation? $\endgroup$ Apr 22, 2014 at 13:15

1 Answer 1

0
$\begingroup$

You could pass a generic function f and the variable you want to plot it against and then use a replace inside the body of the Module. I am writing this without testing the code, but something along the lines of

funloglogplot[f_, {x_, xmin_, xmax_, numpoints_}] := Module[
  {list1, listRe, listIm}, 
  list1 = {#1, (f /. x -> #1)} & /@ logspace[xmin, xmax, numpoints];
  listRe = {#1[[1]], Re[#1[[2]]]} & /@ list1;
  listIm = {#1[[1]], -Im[#1[[2]]]} & /@ list1;
  ListLogLogPlot[{listRe, listIm}, Joined -> True]
]

Might do the trick. (You might have to tune the f /. x -> #1 assignment part. I remember it can be tricky).

You will then call your procedure specifying all parameters in your function except the variable one which you can call any name (x, for example, or z - you just have to be consistent) as in

funloglogplot[function1[eps1, 1, 1, z, R2, ordre], {z, 2, 6, 120}]

I hope this is pointing you in the right direction. It worked for me when I created my plotting routines (now, the problem is: am I recalling it correctly?)

$\endgroup$
1
  • $\begingroup$ Yes that's it! Thank you very much $\endgroup$ Apr 22, 2014 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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