Solve matrix differential system with mathématica

Click For Summary

Discussion Overview

The discussion revolves around solving a matrix differential system using Mathematica, specifically the equation B' = A*B, where B and A are 12x12 matrices with initial conditions B(0) = I. Participants seek assistance in implementing their solutions and understanding the code provided.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant requests help with solving a differential system in Mathematica, specifying the form of the equation and initial conditions.
  • Another participant provides a code snippet to define matrices and solve the differential equation using DSolve, mentioning the use of LogicalExpand for matrix equations.
  • A participant expresses confusion regarding the provided code, particularly the definition of matrix A, and requests clarification on how to implement the suggested solution.
  • Further clarification is offered about the arbitrary choice of matrix A and the need for the participant to define their own matrix A based on their calculations.
  • One participant suggests constructing test cyclic matrices and provides an example of how to define matrix A using nested loops, while also noting potential optimizations using ArrayFlatten.
  • Another participant corrects their earlier statement regarding the type of matrix and discusses the efficiency of accessing array elements in Mathematica.

Areas of Agreement / Disagreement

Participants generally agree on the need to define matrix A appropriately for the solution to work, but there are multiple competing views on how to construct this matrix and the best practices for coding in Mathematica. The discussion remains unresolved regarding the exact implementation details and the definitions of certain matrices.

Contextual Notes

There are several undefined variables and assumptions in the participants' code snippets, such as the definitions of M, CM, KM, premier, second, ID, and Nul, which may affect the clarity and correctness of the proposed solutions.

Nesrine
Messages
8
Reaction score
0
hello ,

I need to solve a differential system with mathematica that has the form :

B'=A*B

knowing that B and A are (12,12) Matrix and with initial conditions B(0)=I

Can you help me please

Thanks
 
Physics news on Phys.org
Try something like

Code:
n = 12;
B[t_] = Array[Subscript[b, ##][t] &, {n, n}];
Bt = Array[Subscript[b, ##] &, {n, n}];
id = IdentityMatrix[n];
(*A=2.id-RotateRight[id,1]-RotateRight[id,-1];*)
A = SparseArray[{Band[{1, 1}] -> 2., Band[{1, 2}] -> -1, Band[{2, 1}] -> -1}, {n, n}];

The key command for getting a matrix equation into something that DSolve and friends like is the command LogicalExpand. (You can also use combinations of Flatten and Thread, but that's not as neat).

Code:
soln = DSolve[LogicalExpand[B'[t] == A.B[t] && B[0] == id], Flatten@Bt, t];

Where we could have used B[t] instead of Bt, but then the resultant replacement rules (soln) would be less useful. For the purely numerical example above, NDSolve would be quicker to get a solution for any particular range of t.

We can then plot the result using ArrayPlot:
Code:
Animate[ArrayPlot[Evaluate[B[t] /. soln[[1]]]], {t, 0, 5 n}]
kLrxh.gif
 
Thank you very much for your help...but the problem is that since I'm not used to Mathematica I don't understand so functions that you have used.

For example I supposed in this part you defined the Matrix A
(*A=2.id-RotateRight[id,1]-RotateRight[id,-1];*)
A = SparseArray[{Band[{1, 1}] -> 2., Band[{1, 2}] -> -1, Band[{2, 1}] -> -1}, {n, n}];

But for me the Matrix A has been calcultaed before.
Can you show me please how to implemented in my programm solution that you proposed to me .

Here is my programm for calcultaing the matrix A :

A = Table[0, {12}, {12}];
premier = -Inverse[M] . CM;
second = -Inverse[M]. KM;
For[k = 1, k <= 3, k++,
For[l = 1, l <= 3, l++,
A[[k]][[l]] = premier[[k]][[l]];
A[[k]][[l + taille]] = second[[k]][[l]];
A[[k + taille]][[l]] =
ID[[k]][[l]]; (*Calcul de la matrice d'état*)


A[[k + taille]][[l + taille]] = Nul[[k]][[l]]
]
];

Where M, CMand KM are periodic matrix (6,6)

Thank you very much I really appreciate
 
I only defined A like that since I needed some coefficient matrix. You can see what I (almost) arbitrarily chose by running MatrixForm[A].
By the way, the stuff in (* ... *) is a comment - an alternative choice of A that I should have deleted before posting.

So to make the code I posted in your program work, you just need your own A.
There are quite a few things not defined in your code, which makes it hard to tell what you want.
It kind of looks like you're constructing the block matrix
{{premier, second}, {ID, Nul}}
but then your iterators should go up to 6...
Anyway, I'll assume that's what you're doing.
If it's not, can you describe in words how you construct A and what the result should be?

Let's first construct some test cyclic matrices:
Code:
M  = NestList[RotateRight, RandomReal[{0, 1}, 6], 5];
CM = NestList[RotateRight, RandomReal[{0, 1}, 6], 5];
KM = NestList[RotateRight, RandomReal[{0, 1}, 6], 5];
and your premier and second matrices:
Code:
premier = -Inverse[M].CM;
second = -Inverse[M].KM;
I'll assume that Nul is the zero matrix and ID the identity matrix
Code:
Nul = Array[0 &, {6, 6}];
ID = IdentityMatrix[6];
Then your loop becomes
Code:
A = Array[0 &, {12, 12}];
taille = 6;
For[k = 1, k <= taille, k++,
  For[l = 1, l <= taille, l++,
   A[[k]][[l]] = premier[[k]][[l]];
   A[[k]][[l + taille]] = second[[k]][[l]];
   A[[k + taille]][[l]] = ID[[k]][[l]];
   A[[k + taille]][[l + taille]] = Nul[[k]][[l]]
   ]];
Which is much more easily created using ArrayFlatten:
Code:
ArrayFlatten[{{premier, second}, {ID, Nul}}]
ArrayFlatten would also be faster since the internal code would be optimized.
 
Oops... I confused periodic matrix (M^(k+1) = M) with cyclic matrix (M_{i,j}=M_{i+1,j+1}) - not that it makes any difference to the above example.

Also, if Nul really is the zero matrix then you can replace Nul[[k]][[l]] with 0 in the loop and Nul with 0 in the ArrayFlatten.

Also, when accessing elements of arrays, it is about twice as fast to use M[[k,l]] than M[[k]][[l]]. To check this, run

Code:
M = RandomReal[{0,1},{6,6}]
Timing[Do[M[[k]][[l]],{k,RandomInteger[{1,6},1000]},{l,RandomInteger[{1,6},1000]}]]
Timing[Do[M[[k,l]],{k,RandomInteger[{1,6},1000]},{l,RandomInteger[{1,6},1000]}]]
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
8
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K