How can I solve a matrix differential equation within MATLAB?

6 Ansichten (letzte 30 Tage)
Edu
Edu am 21 Mär. 2017
Bearbeitet: Edu am 26 Mär. 2017
I am interested in solving an ODE dF/dt=F*A, where both A and F are matrices (in particular, 5x5 matrices). I have used ode45 and dsolve before for problems like dx/dt=A*x, where x is a vector but not a matrix like in this case.
EDIT:
I am using now a version of your example code with my 5x5 matrix A which is complex and defined as an anonymous function. Seems to work fine, is there any implemented method in MATLAB to check the solutions given by ode45? Thank you.

Akzeptierte Antwort

James Tursa
James Tursa am 21 Mär. 2017
Bearbeitet: James Tursa am 23 Mär. 2017
E.g., if you are using ode45, then simply reshape F and the initial Fo into column vectors. Inside the derivative routine, reshape the input argument F into a matrix, do your F*A matrix multiply, then reshape the multiply result back into a column vector for output.
EDIT:
Here is a small example showing the technique
function matrix_deriv_example
A = rand(2,2); % Some arbitrary matrix we will use
F0 = eye(2); % Some arbitrary matrix initial value
odefun = @(t,y) deriv(t,y,A); % Anonymous derivative function with A
tspan = [0 5];
[T,F] = ode45(odefun,tspan,F0(:)); % Pass in column vector initial value
F = reshape(F.',2,2,[]); % Reshape the output as a sequence of 2x2 matrices
n = size(F,3);
e = zeros(2,n);
for k=1:n
e(:,k) = eig(F(:,:,k)); % Calculate the eigenvalues of the 2x2 matrices
end
plot(T,e(1,:),T,e(2,:)); grid on % Plot them
xlabel('Time')
ylabel('Eigenvalues')
end
function dy = deriv(t,y,A)
F = reshape(y,size(A)); % Reshape input y into matrix
FA = F*A; % Do the matrix multiply
dy = FA(:); % Reshape output as a column vector
end
  1 Kommentar
Edu
Edu am 22 Mär. 2017
Bearbeitet: Edu am 26 Mär. 2017
Ok, let's see if I understood well. Based on your answer, I would code something like this:
Reshape F and F0 into column vectors: F = F(:); F0 = F0(:);
Reshape F into matrix: F = reshape(F, size(A));
Why reshaping F in the previous step then??
F*A multiplication: dFdt = F*A;
Reshape result into column vector: dFdt = dFdt(:);
Use ode45 now?: [T F] = ode45(dFdt, [t0 tf], F0);
In case this is (close to be) correct, I have two questions:
  1. How should I define F in the first place?
  2. Can I use these method for a matrix A defined as an anonymous function?
Thanks very much.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by