Message Boards Message Boards

0
|
3683 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Are parameters in functions handled better with Set (=) or Patterns (tau_)

Posted 9 years ago

Returning to Mathematica after 20 years. I have functions such as F(r,t):=something. There are parameters set at the beginning of the notebook. Is it better to set them, e.g. p1=5.0, and use them in the function such as F(r,t):=r^2*p1+t^3 or should they be referenced explicitly. For example p1_=5.0 F(r,t,p1_)=r^2*p1+t^3

Thanks

POSTED BY: Forrest Meiere
2 Replies

I get good results using a construction like

p1 = 5.0; F[r_, t_] := r^2*p1 + t^3
POSTED BY: S M Blinder
Posted 9 years ago

For me it depends on on what I am doing with the notebook, and how much versatility I want the functions defined in it to retain as opposed to how much clutter I'm willing to put up with.

If I am writing a notebook where the computations always involve a few variables with a fixed definition throughout, I set those variables in an obvious section at the top of the notebook, where I can easily change them an do "Evaluate Notebook."

If it's the case that my interest is purely numerical, and always will be, I use Set (=) to define them. If I have an interest in the symbolic solutions, then I often define them as something like values = { a list of rules for the values } and then use Replace when I want to see numbers.

If I expect that I will at times want to use the functions I'm writing with different values for the parameter, then I will pass it specifically as an argument.

It's all a bit of a tradeoff: To many variables in global scope make a notebook and the functions in it less versatile, and as usual global scope can lead to surprises. To many variables passed as arguments can lead to a very cluttered notebook. I find I often choose to work in symbols, with the solution /. values at the end to get numbers.

POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract