Solving a matrix ODE in Matlab

218 Views Asked by At

I have a matrix equation in the form $$ \begin{bmatrix} A(x)\ \end{bmatrix}_{,x} =\frac{\begin{bmatrix} A(x)\ \end{bmatrix}^2}{\begin{bmatrix} B_o\ \end{bmatrix}} +\begin{bmatrix} A(x)\ \end{bmatrix} x+\begin{bmatrix} C_0\ \end{bmatrix} $$

where $A$ is a 2*2 matrix, and $B_0$ & $C_0$ are constant 2*2 matrices. I want to integrate it using Matlab, for the period $$ x_0<x<x_1 $$ where $A(x_1)$ is known, and I want to find $A(x_0)$

I'm using ode45

[xx,A_list]=ode45(@(x,A)(A^2/B0+A*x+C0,[x1 x2],A1);

which has always worked for a scaler equation, but I seem to be missing something when integrating matrix equations. Any suggestions?

1

There are 1 best solutions below

1
On BEST ANSWER

This is really a matlab programming problem. ode45 and all the other solvers only work for flat arrays. You will need to wrap your formula in a duo of reshaping operations.

flat2mat=@(A) reshape(A,2,2)
mat2flat=@(A) reshape(A,4,1)
odefunc=@(x,A) A^2/B0+A*x+C0

[xx,A_list]=ode45(@(x,A)mat2flat(odefunc(x,flat2mat(A))),[x1 x2],mat2flat(A1));

(untested) should work or at least give more interesting error messages.