Mathematica indices as variables

In summary, the person is asking for help in creating a matrix A with functions as its components and being able to input a specific value for n to get the corresponding matrix. They have attempted a solution but it was not successful. They have also encountered an error while trying to write a similar code for another matrix. A helpful response suggests using a different variable for the second matrix and clearing the value of the first matrix before trying again.
  • #1
Qubix
82
1
1. I'll post here a simplified version of my problem

Say you have a matrix A, and you want all its components to be functions, for example:

A11 = Sin(a_n)
A12 = Cos(a_n)
A21 = Sin(b_n)
A22 = Cos(b_n)

And I want to be able to do this in mathematica so as to have the matrix A a function of n.


Homework Equations



How can I do this, such that when I ask

MatrixForm[A[4]] for example, it would give me the matrix with all components in terms of a_4 and b_4 ?

The Attempt at a Solution



Rather long and wrong, so better not post it here.
 
Physics news on Phys.org
  • #2
Code:
A[n_] := {{Sin[a[n]], Cos[a[n]]}, {Sin[b[n]], Cos[b[n]]}}
 
  • #3
Thanks for that DrClaude, it worked :)

PS:

Why won't Mathematica allow me to write:

(R[p_] := {{p/2, 0, 0, p/2}, {0, 1 - p, 0, 0}, {0, 0, 0, 0}, {p/2, 0, 0, p/2}}) // MatrixForm

It gives the error:

SetDelayed::write: "Tag List in {{p/2,0,0,p/2},{0,1-p,0,0},{0,0,0,0},{p/2,0,0,p/2}}[p_] is Protected.
 
Last edited:
  • #4
It looks like you have already assigned that list to R. Try clearing the value of R and then try this again.
 
  • #5


I can suggest a solution to this problem using Mathematica's indexing functionality. Instead of using individual variables for each component of the matrix, we can define a function that takes the index as an argument and returns the desired value. For example:

A[n_] := {{Sin[a[n]], Cos[a[n]]}, {Sin[b[n]], Cos[b[n]]}}

This way, when we call A[4], we will get the matrix with all components in terms of a_4 and b_4. We can also use the MatrixForm function to display the matrix in a more organized format.

Furthermore, we can use Mathematica's Table function to generate a list of matrices for different values of n. For example:

Table[A[n], {n, 1, 10}]

This will generate a list of 10 matrices, each with its components in terms of a_n and b_n.

I hope this helps with your problem. Good luck!
 

What are Mathematica indices?

Mathematica indices are symbols used to represent the position or order of elements in an expression or list. They are commonly used in mathematical and computational contexts to simplify calculations and make code more concise.

How are indices represented in Mathematica?

In Mathematica, indices are typically represented as lowercase letters or Greek symbols, such as i or α. They can also be represented as subscripts, denoted by _ followed by the index symbol.

Can indices be used as variables in Mathematica?

Yes, indices can be used as variables in Mathematica. They can be assigned values and used in mathematical operations just like any other variable. However, it is important to keep in mind the context in which the indices are being used, as they may have specific meanings in certain functions or algorithms.

What is the purpose of using indices as variables?

Using indices as variables can help simplify and generalize mathematical expressions and algorithms. They allow for a more concise notation and make it easier to write and manipulate complex equations and functions. Indices also play a crucial role in vector and matrix operations, where they represent the position of elements within the data structure.

Are there any limitations to using indices as variables in Mathematica?

One limitation to using indices as variables in Mathematica is that they must be defined before they can be used. This means that they cannot be used as placeholders or placeholders for unknown values. Additionally, indices should not be used as variable names if they have a specific meaning or function in a given context.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
853
  • Linear and Abstract Algebra
Replies
6
Views
882
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Calculus and Beyond Homework Help
Replies
6
Views
391
  • Programming and Computer Science
Replies
4
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
875
  • Differential Equations
Replies
3
Views
2K
  • Calculus and Beyond Homework Help
2
Replies
37
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top