2
$\begingroup$

If I have many possible variables related to one another, why do I need to define my function with parameters for plotting? For example, if I have an equation like:

output_value := a * b * c * d / ( e * f * g * h );

Assuming that all values are defined and have some default value, why can't I just do this to plot:

Plot[ output_value, { b, 0, 100 } ];

The annoying thing is having to do this:

output_value[aparam_, bparam_, cparam_, dparam_, eparam_, fparam_, gparam_, hparam_] := aparam * bparam * cparam * dparam / ( eparam * fparam * gparam * hparam );

and then:

With[ {aparam=a,cparam=c,dparam=d,eparam=e,fparam=f,gparam=g}, Plot[ output_value, { bparam, 0, 100 } ];
$\endgroup$
1
  • $\begingroup$ BTW, you can ease your pain by writing With[{a = a, c = c, ...}, ...] . See the third example under With in the Documentation Center $\endgroup$
    – m_goldberg
    Commented Nov 12, 2013 at 2:48

1 Answer 1

3
$\begingroup$

You do not have to define a function to plot it. You can certainly do

a=1;
f:=a+x;
Plot[f,{x,0,10}]

and the output is the graph you expected. Defining the function as above has many drawbacks:

1) You cannot evaluate it without making an explicit replacement. For example, f(2) becomes

f/.x->2

I would generalize this point and say that defining $f(x)$ with a f[x_] gives convenience down the road.

2) You have to be careful not to use the variable that you are "reserving" for the independent variable:

<<lots of code>> x=3; <<lots of code>> f/.x->2

The last evaluation of f/.x->2 will not necessarily give you the $f(2)$ that you might have expected. I would generalize this point and say that defining $f(x)$ with a f[x_] makes it more difficult to shoot oneself in the foot.

$\endgroup$
2
  • 1
    $\begingroup$ I see, so basically you are saying that using function parameters is optional. Thats good. $\endgroup$ Commented Nov 12, 2013 at 0:48
  • $\begingroup$ @TylerDurden One of the places where parameter lists are almost a necessity is in Manipulate, where sliders won't update a parameter in a function if it isn't explicitly visible to Manipulate. $\endgroup$ Commented Nov 12, 2013 at 6:38

Your Answer

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

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