10
$\begingroup$

Still learning the fundamentals of the language I would like to ask you what advantages there might be in writing something like:

a[1] = 2; a[2] = 4; a[3] = "x"; 

It seems to me that it is always better to write

a = {2, 4, "x"}; 

Do you know about any practical constructs where indexed variables would offer an advantage?

$\endgroup$
12
  • $\begingroup$ Check this out, particularly the part on sparse arrays $\endgroup$ Commented May 26, 2014 at 19:10
  • 2
    $\begingroup$ @Rojo I am getting old $\endgroup$ Commented May 26, 2014 at 19:36
  • 1
    $\begingroup$ ... and I am already reading it, and thank both of you :) $\endgroup$ Commented May 26, 2014 at 19:37
  • 3
    $\begingroup$ Indexed variables can be used symbolically. You can Solve[a[1]^2==2, a[1]] but you can't Solve[a[[1]]^2==2, a[[1]] ]. This is what we typically use when we don't know the number of symbolic variables we need beforehand. I would sometimes define a 3 by 3 matrix with explicit symbolic elements as Array[a, {3,3}]. $\endgroup$ Commented May 26, 2014 at 19:55
  • 1
    $\begingroup$ @Szabolcs - magic ! answer just arrived :) $\endgroup$ Commented May 26, 2014 at 22:00

2 Answers 2

12
$\begingroup$

Indexed variables can be used in symbolic calculations. They're useful when the number of variables used needs to be changed programmatically.

Here's an example:

vars = Array[a, 3] (* {a[1], a[2], a[3]} *) Minimize[vars.vars, vars] (* {0, {a[1] -> 0, a[2] -> 0, a[3] -> 0}} *) 

They can also be used to emulate sparse arrays, as described here (old broken link for reference).

$\endgroup$
2
1
$\begingroup$

For me, using index variable is really the only choice, when solving problem from textbook, which is system of differential equations or just system of equations.

Here is an example from text asking to solve this ODE system

enter image description here

I wanted to check my hand solution is correct. So I use Mathematica to verify. The question now becomes, how to enter that above into Mathematica?

The most direct way is to type 4 differential equations. I want to keep the similar notation to text book to make it easier to compare solution with the book which uses $x_1,x_2,x_3,x_4$ for the variables.

There are 4 options:

  1. Uses subscripted variables Subscript[x,1] etc... But these are known to cause problems.
  2. Change the variable names to x[t],y[t],z[t],w[t] and use these instead. But now the solution will harder to compare to book, as have to remember which new variable match which one used by the book.
  3. use x1[t],x2[t],x3[t],x4[t]
  4. Use indexed variables.

The reason 4 is better than 3, is because with indexed variable you get much nicer output and the latex generated give true indexed variable $x_1(t)$ which looks much better than $x1(t)$ in the HW report.

Here is an example using option (3) and (4)

Using (3)

ode1 = x1'[t] == x2[t] - x3[t] + x4[t]; ode2 = x2'[t] == -x2[t] + x4[t]; ode3 = x3'[t] == x3[t] - x4[t]; ode4 = x4'[t] == 2*x4[t]; sol = DSolve[{ode1, ode2, ode3, ode4}, {x1[t], x2[t], x3[t], x4[t]}, t] 

The latex of the solution looks like

enter image description here

Using (4) (indexed variables)

Quit[] x[n_] := Indexed[x, n]; ode1 = x[1]'[t] == x[2][t] - x[3][t] + x[4][t]; ode2 = x[2]'[t] == -x[2][t] + x[4][t]; ode3 = x[3]'[t] == x[3][t] - x[4][t]; ode4 = x[4]'[t] == 2*x[4][t]; sol = DSolve[{ode1, ode2, ode3, ode4}, {x[1][t], x[2][t], x[3][t], x[4][t]}, t] 

The Latex of the solution looks like

enter image description here

Which looks much better and will make the teacher more happy.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.