필터 지우기
필터 지우기

ODE45 and 3D arrays

조회 수: 3 (최근 30일)
Salar
Salar 2016년 7월 27일
댓글: James Tursa 2016년 7월 27일
Hello,
I have a question about 3D-arrays and ODE45. I have a 3D array called "A". "A" contains a set of a matrices at different instants of time, so for example A(:,:,1) will give me a matrix at T_0 . I also have a equation that I need to solve using ODE45. btw all the matrices in A are 6X6. My equation is Phi_dot(:,:,k) = A(:,:,k)*Phi, I'm trying to integrate this, but I keep getting an error on "indices". I would really appreciate it if you could help me with this. Thank you. My code is below :
function [ Phi_dot ] = Phi( A, Phi, t )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
Phi_dot = A*Phi;
end
Phi_0 = eye(6);
tspan = [0 pi];
opts = odeset('RelTol',1e-12,'AbsTol',1e-12);
for k = 1:i
[t1, Phi] = ode45(@(t,Phi) Phi(A(:,:,k),Phi(:,:,k),t),tspan,Phi_0,opts);
end

채택된 답변

James Tursa
James Tursa 2016년 7월 27일
편집: James Tursa 2016년 7월 27일
1) Don't use (:,:,k) subscripting on Phi in your derivative function. ode45 only knows about Phi as a 36-element column vector.
2) Don't use Phi for multiple things! You are using it as the dummy variable in your anonymous function, and as the derivative function name, and as the input variable in your derivative function, and as the return variable from ode45. Use different names for these. Partly because some of these will cause errors, and partly for readability.
3) Make sure your derivative function reshapes the column vector input into a matrix, and then returns a column vector.
E.g.,
[t1, PhiResult] = ode45(@(t,Phi) PhiFun(A(:,:,k),Phi,t),tspan,Phi_0,opts);
:
function [ Phi_dot ] = PhiFun( A, Phi, t )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
Phi_dot = A*reshape(Phi,6,6);
Phi_dot = Phi_dot(:);
end
Then of course you will probably want to add some code to save the desired results from each iteration (the way it is now the results of each iteration overwrite the previous iteration).
  댓글 수: 2
Salar
Salar 2016년 7월 27일
Thank you for your response. My only problem with this is that for my k value, this is taking forever! Would you able to suggest a faster approach maybe I could try? Thank you!
James Tursa
James Tursa 2016년 7월 27일
Do you mean i is large?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by