Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 } ];
share|improve this question
    
BTW, you can ease your pain by writing With[{a = a, c = c, ...}, ...] . See the third example under With in the Documentation Center –  m_goldberg Nov 12 '13 at 2:48
add comment

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
1  
I see, so basically you are saying that using function parameters is optional. Thats good. –  Tyler Durden Nov 12 '13 at 0:48
    
@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. –  Sjoerd C. de Vries Nov 12 '13 at 6:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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