Mathematica: Variables and Functions

In summary, I need to find a way to input coordinates into a function and output the H-R diagram as a image.In summary, the user would like to be able to input coordinates into a function and output the H-R diagram as an image.
  • #1
Niles
1,866
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
  • #2
potential[z_]:=1/(constant + z);
 
  • #3
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]".
 
  • #4
Thanks for this, I will also check out the reference.Niles.
 
  • #5
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.
 
  • #6
You can use:

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

and it should work.
 
  • #8
Thanks for the help, it is kind of both of you.Niles.
 
  • #9
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.
 

Related to Mathematica: Variables and Functions

What is a variable in Mathematica?

A variable in Mathematica is a symbol that represents a value or expression. It can be assigned a value, and that value can be changed at any time. Variables are used to store and manipulate data in Mathematica.

How do I define a variable in Mathematica?

To define a variable in Mathematica, you use the assignment operator "=" followed by the value or expression you want to assign to the variable. For example, x = 5 defines a variable x with a value of 5.

What is a function in Mathematica?

A function in Mathematica is a set of rules that specifies how one or more input values are transformed into an output value. Functions are used to perform calculations and manipulate data in Mathematica.

How do I define a function in Mathematica?

To define a function in Mathematica, you use the Function command followed by a list of arguments and a body that specifies the function's behavior. For example, f[x_] := x^2 defines a function f that takes one argument x and returns its square.

Can I use variables in a function definition in Mathematica?

Yes, you can use variables in a function definition in Mathematica. These variables are known as local variables and are only accessible within the function. They are defined using the underscore notation, such as x_, and their values are determined by the arguments passed to the function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
691
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
886
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top