Defining variables in mathematica

In summary, the person is asking if they can define variables in Mathematica 8 using a specific method and if the resulting expression can be used in another function. The expert advises against using certain names and suggests using x_ and y_ instead. They also provide an example of how the suggested method can work. The expert also suggests providing a clear explanation of the problem to potentially find a simpler solution.
  • #1
Qubix
82
1
I need to define some "general" variables in Mathematica 8, and I wonder if this can be done the following way.

Say ta[n] and tb[m] are my variables, in turn depending on n and m.

I want to define a function, for example

E[ta[n]_ , tb[m]_ ] := Cos[ ta[n] ] + Sin [tb[m] ] (just a simple example).

Then I want to be able to use the above expression in yet another function

S := E[ta[1],tb[2]] + E[ta[3],tb[1]] + etc...

First of all, does this work in Mathematica ?

Second, is S also a function, for example, if the line would stop before etc, should i write it as

S[ta[1]_ , tb[2]_, ta[3]_, tb[1]_ ]?


Remember, ta[1] , ta[2] etc are different variables.

I am asking all this because it would save me a serious amount of time in writing everything down explicitly.
 
Physics news on Phys.org
  • #2
First, E is defined to be Euler's constant. Trying to use that name for something else is a path to grief.
Second, trying to use function names, like ta[1]_ as a name of a parameter in a function definition is going to be another path to grief. But just writing x_ and y_ should work for you.
This works

In[1]:= e[x_, y_] := Cos[x] + Sin[y] ;
e[ta[1], tb[2]] + e[ta[3], tb[1]]

Out[2]= Cos[ta[1]] + Cos[ta[3]] + Sin[tb[1]] + Sin[tb[2]]

If you could clearly explain what your real problem is then there might be a simpler way for you to get there.
 

Related to Defining variables in mathematica

1. What are variables in Mathematica?

Variables in Mathematica are symbols that represent a value or an expression. They are used to store and manipulate data, and can be either numeric or symbolic.

2. How do I define a variable in Mathematica?

To define a variable in Mathematica, you can use the assignment operator "=" or the Set function. For example, x = 5 or Set[x, 5] will both define the variable x with a value of 5.

3. Can I change the value of a variable in Mathematica?

Yes, you can change the value of a variable in Mathematica by reassigning it using the assignment operator or the Set function. For example, x = 5 will change the value of x to 5.

4. What are the rules for naming variables in Mathematica?

Variables in Mathematica can be named using letters, numbers, and special characters, but cannot start with a number. They are case-sensitive, and cannot be the same as any built-in function or command in Mathematica.

5. How can I use variables in mathematical expressions in Mathematica?

To use variables in mathematical expressions in Mathematica, you can simply use them in place of numbers. For example, x + 2 will return the value of x added to 2. You can also use the Substitute function to substitute a value for a variable in an expression.

Similar threads

  • Calculus and Beyond Homework Help
Replies
2
Views
626
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Replies
4
Views
904
  • Computing and Technology
Replies
30
Views
3K
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
5K
  • Introductory Physics Homework Help
Replies
1
Views
662
  • Introductory Physics Homework Help
2
Replies
41
Views
4K
Back
Top