Defining variables in mathematica

AI Thread Summary
Defining general variables in Mathematica can be achieved, but using specific names like Euler's constant for functions can lead to issues. Instead of using ta[n] and tb[m] as function parameters, it's recommended to use generic placeholders like x_ and y_ for clarity. The example provided demonstrates that defining a function e[x_, y_] works correctly, allowing for expressions like e[ta[1], tb[2]] to evaluate as intended. Additionally, if S is meant to be a function, it should be defined with appropriate parameters rather than specific variable names. Clarifying the underlying problem may reveal simpler solutions.
Qubix
Messages
82
Reaction score
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
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.
 
Back
Top