Defining variables in mathematica

Click For Summary
SUMMARY

Defining variables in Mathematica 8 can be accomplished using function definitions that allow for general variable representation. The user can define a function E with parameters ta[n] and tb[m], and then utilize this function in another expression S. However, it is crucial to avoid using reserved names like Euler's constant for function definitions and to use generic variable names such as x_ and y_ to prevent conflicts. The example provided demonstrates successful implementation with the expression e[x_, y_] := Cos[x] + Sin[y].

PREREQUISITES
  • Familiarity with Mathematica 8 syntax and functions
  • Understanding of variable definitions and function parameters in Mathematica
  • Knowledge of trigonometric functions such as Cosine and Sine
  • Awareness of naming conventions and reserved keywords in Mathematica
NEXT STEPS
  • Explore advanced function definitions in Mathematica, focusing on parameter handling
  • Learn about variable scoping and naming conventions in Mathematica
  • Investigate the use of built-in constants and their implications in function definitions
  • Study examples of complex function compositions in Mathematica for better efficiency
USEFUL FOR

Mathematica users, mathematicians, and programmers looking to optimize their function definitions and variable management in Mathematica 8.

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 30 ·
2
Replies
30
Views
6K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 13 ·
Replies
13
Views
6K