Mathematica Defining 2x2 Matrices with Variables for Solving DEs

  • Thread starter Thread starter McLaren Rulez
  • Start date Start date
  • Tags Tags
    Matrices Variables
AI Thread Summary
To solve differential equations involving 2x2 matrices in Mathematica, users are advised to express each matrix equation as four independent equations for clarity and functionality. The initial attempt to define a matrix with variables using matrix[a_, b_, c_, d_] := {{a, b}, {c, d}} is not effective due to Mathematica's handling of variable definitions. Instead, it is suggested to write out the equations explicitly, such as \frac{d}{dt}A_{ij}=B_{ij}, to avoid complications. Users should also refrain from subscripting variables to simplify the process. Ultimately, while function definitions for matrices are possible, a straightforward approach often yields better results in Mathematica.
McLaren Rulez
Messages
289
Reaction score
3
Hi,

I have a bunch of closed differential equations that I want to solve. The variables of the DEs are 2x2 matrices. So, I want to enter some 2x2 matrices of variables and then use NDSolve to get the solution.

How should I define a 2x2 matrix with four variables inside it? I tried

matrix[a_, b_, c_, d_] := {{a, b}, {c,d}} but this doesn't work. Rather embarassingly, I can't find the solution after quite a bit of googling.


Thank you for your help!
 
Last edited:
Physics news on Phys.org


In Mathematica = and := are very different things. := means, and this is way too simplified but you almost certainly don't want to know all the ugly details, that sometime in the future if you ever use matrix[p,q,r,s] THEN you replace it with the right hand side {{p,q},{r,s}}. But until that future gets here := just tells Mathematica to remember that direction and do nothing until then.

I will warn you, the more anyone wants to "desktop publish" their problem, the more desperately they want to have everything in 2-d notation, the more problems they are likely going to find in trying to actually get a useful answer out of Mathematica.

If I were going to try to get an answer to your differential equations I would manually translate the whole mess into a form that looks very much like the simplest examples that you find in the help system. I've found that is the quickest path to a hopefully correct and useful answer.
 


Thank you for the reply Bill.

So, what you're saying is that I should simply write out each matrix DE as four independent DEs? For instance if I have a very simple example

\frac{d}{dt}A=B

I should simply write four equations of the form \frac{d}{dt}A_{ij}=B_{ij}

This can indeed be done (and for me, it's not too bad since they are only 2x2 matrices) but I was wondering if there was an easier way to tell mathematica to create a matrix that has all its entries as variables. The reason I thought matrix[a_, b_, c_, d_] := {{a, b}, {c,d}} might work was because it worked for one variable. If I try matrix[a_] := {{a, 2}, {3,4}}, it understands that a is a variable so the input of matrix[1] gives the output {{1, 2}, {3,4}} and so on.

It's not that I am lazy to write it out, I just wanted to see if there was a nicer solution. Thank you :)
 


McLaren Rulez said:
Thank you for the reply Bill.

So, what you're saying is that I should simply write out each matrix DE as four independent DEs? For instance if I have a very simple example

\frac{d}{dt}A=B

I should simply write four equations of the form \frac{d}{dt}A_{ij}=B_{ij}

Yes. And avoiding the urge to subscript your variables will make your life easier too.

McLaren Rulez said:
This can indeed be done (and for me, it's not too bad since they are only 2x2 matrices) but I was wondering if there was an easier way to tell mathematica to create a matrix that has all its entries as variables. The reason I thought matrix[a_, b_, c_, d_] := {{a, b}, {c,d}} might work was because it worked for one variable. If I try matrix[a_] := {{a, 2}, {3,4}}, it understands that a is a variable so the input of matrix[1] gives the output {{1, 2}, {3,4}} and so on.

It's not that I am lazy to write it out, I just wanted to see if there was a nicer solution. Thank you :)

You can write function definitions to create matricies and vectors. That is not a problem. But usually the more you try to "desktop publish" the queries you are giving Mathematica the more difficulty you will have.

Mathematica, like many other pieces of software, started out as a command line driven tool. Only later did they try to glue on desktop publishing and that is not uniformly implemented.

You are certainly free to try giving matricies of equations to DSolve. Give it a good serious try. Then report how well that worked.
 


Thank you for your help.
 
Back
Top