Numerical Solution for Matrix Differential Equation

15 views (last 30 days)
I am trying to solve the following differential equation:
where matrix Y is the desired solution, and A is a known matrix. The matrix sizes can be up to 8x8.
To check that MATLAB accepts a matrix ODE, I tried the following codes for the simplest case of 2x2 matrix and a constant A:
A=[1, 2; -1, -2];
ode_func =@ (X) -X*X-A;
Y0 = [1, 1; 1, 1]; T0=0; Tf=100;
Y=zeros(2,2);
[T,Y] = ode45(@(t,y) ode_func(y), [T0,Tf], Y0);
But it returns the error:
Error using *
Inner matrix dimensions must agree.
Error in =@ (X) -X*X -A
Error in @(t,y)ode_func(y)
Error in odearguments (line 88)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
It seems the codes are not working. So, I am wondering whether MATLAB is able to solve a matrix ODE?
Thank you,
  2 Comments
Walter Roberson
Walter Roberson on 23 Mar 2019
The second parameter will always be passed as a column vector, and the result of the ode_func must be a column vector of exactly the same length.
ode_func = @(X) reshape( - reshape(X,2,2).^2 - A, [], 1)
Ramtin Taheri
Ramtin Taheri on 24 Mar 2019
Thanks that solved the problem.
Also I had to change the initial condition to a column vector Y0(:)

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!