Mathematica: Variables and Functions

Click For Summary

Discussion Overview

The discussion revolves around the use of variables and functions in Mathematica, specifically focusing on how to evaluate a potential function at different values of a variable without rewriting existing procedures. The scope includes technical explanations and practical applications related to programming in Mathematica.

Discussion Character

  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant describes a procedure that calculates potential in the form of an expression involving a variable z and seeks a more efficient way to evaluate this potential at different values of z.
  • Another participant suggests defining potential as a function using the syntax potential[z_]:=1/(constant + z), which allows for direct evaluation at specific values.
  • A different participant explains the significance of using an underscore in function definitions, indicating that an argument must be supplied when calling the function.
  • One participant expresses difficulty in using a variable defined by an external procedure and shares their attempt to create a function that does not yield the expected output.
  • Another participant provides a solution using replacement rules to evaluate the potential function at different values of z.
  • There are multiple suggestions on how to define the function and evaluate it, with some variations in syntax and approach.
  • A participant shares their frustration with learning Mathematica and expresses a desire to share their experiences with others.

Areas of Agreement / Disagreement

Participants present various methods for defining and using functions in Mathematica, with no consensus on a single best approach. The discussion includes differing opinions on how to handle external procedures and variable assignments.

Contextual Notes

Some participants' solutions depend on specific syntax and may not address all potential issues with variable scope or external dependencies. There are unresolved questions regarding the effectiveness of different approaches in various contexts.

Who May Find This Useful

Users of Mathematica, particularly those interested in programming functions and evaluating expressions efficiently, may find this discussion beneficial.

Niles
Messages
1,834
Reaction score
0
Hi

I have a long procedure, which calculates the potential of a physical system. The procedure returns something having the form
Code:
potential = 1/(constant + z)
where z is a variable. Now, I need to evaluate potential at different z. I can of course do something like
Code:
z = 1;
potential

z=2;
potential
and so on, but is there a better way to do this without having to rewrite the procedure? I'm thinking about having something like potential[z], but that is perhaps too late for me to use this, without having to rewrite the whole thing?

Thanks for any help.


Niles.
 
Physics news on Phys.org
potential[z_]:=1/(constant + z);
 
As posted above, putting a bracket after the variable makes it a function and putting an underscore after a variable means that you must supply an argument for that variable when you call the function.

So, if you wanted the variable "U" to be your z, you would type "potentia" into the function as defined by djelovin above. If you wanted z to be equal to .5, you would type "potential[.5]".
 
Thanks for this, I will also check out the reference.Niles.
 
I have tried using
Code:
potenial = 1+z;
func[z_] := potential
but when I call func[1], then I don't get 2 out. Using
Code:
func[z_] := 1+z
is not an option for me, since 1+z is returned to me by some external procedure which I don't command. I only get the output 1+z in the variable potential.Niles.
 
You can use:

potential = 1 + z;
func[x_] := potential /. z -> x
 
USE:
potential = 1 + z
func[z_] = potential​

and it should work.
 
Thanks for the help, it is kind of both of you.Niles.
 
I have wasted many an hour trying to figure out how to do certain operations in Mathematica so I might as well share what I have learned. I still have not figured out how to use it to make H-R diagrams without writing scripts.
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K