Skip to main content
Source Link
Nasser
  • 156.1k
  • 12
  • 173
  • 396

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.

Post Made Community Wiki by Nasser