numeric solution to matrix ODE with matrix
Show older comments
I am now trying to solve a matrix ODE numerically, let's simplify my problem as follows:
%{ I don't know the best way to define a 4*4 matrix function%}
syms rho11(t) rho12(t) rho13(t) rho14(t);
syms rho21(t) rho22(t) rho23(t) rho24(t);
syms rho31(t) rho32(t) rho33(t) rho34(t);
syms rho41(t) rho42(t) rho43(t) rho44(t);
%{ rho is a 4*4 matrix, each element is a function of time %}
rho=[rho11, rho12, rho13, rho14;rho21, rho22, rho23, rho24;rho31, rho32, rho33, rho34;rho41, rho42, rho43, rho44];
%{ define constant matrix %}
s2p=zeros(4,4);s1p=zeros(4,4);
s2p(1,2)=1;s2p(3,4)=1;s1p(1,3)=1;s1p(2,4)=1;
init=zeros(4,4);init(2,2)=1;
%{now we need to solve the following ODE %}
d(rho)/dt=-(rho *s1p - rho *s2p);
%{initial condition:%} rho(0)=init
I know one way is to transform the matrix to a vector, but the actual equation I am doing is too complex to transform it myself. Can anybody tell me the code to make this transformation and solve the ODE? Thanks!
Answers (1)
James Tursa
on 26 Sep 2016
Generally, the only thing you need to use for this is the reshape function. It doesn't matter how "complex" your matrix stuff is. E.g.,
% Define all of your matrix stuff here
% Reshape the matrix inputs into vectors
% Call ode with the vector inputs
% Reshape the ode vector outputs as desired.
And then in your derivative function
% function signature here (gets vector inputs from the solver)
% reshape the vector inputs to matrices as needed
% do your matrix operations
% reshape the matrix derivative into vector for output
1 Comment
Jieyu You
on 26 Sep 2016
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!