- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
ODE modeling in 3- dimension and looping
조회 수: 1 (최근 30일)
이전 댓글 표시
I simulating an ODE using function ode15s. I set up the output as:
Classes = struct('S', zeros(length(tspan), n, soc), 'E', zeros(length(tspan), n, soc), ...
'A', zeros(length(tspan), n, soc), 'J', zeros(length(tspan), n, soc), ...
'Q', zeros(length(tspan), n, soc), 'I', zeros(length(tspan), n, soc), ...
'H', zeros(length(tspan), n, soc), 'R', zeros(length(tspan), n, soc), ...
'D', zeros(length(tspan), n, soc), 'Ir', zeros(length(tspan), n, soc), 't', []);
The initail condition is a 10 by 21 matrix with I looped over to have a 1 by 21, so that I can simulate for each group n =10.
After I extracted the updated population as, which is inside a loop:
Classes.S(:, :, i) = pop(:, 1:n);
Classes.E(:, :, i) = pop(:, n+1:2*n);
Classes.A(:, :, i) = pop(:, 2*n+1:3*n);
Classes.J(:, :, i) = pop(:, 3*n+1:4*n);
Classes.Q(:, :, i) = pop(:, 4*n+1:5*n);
Classes.I(:, :, i) = pop(:, 5*n+1:6*n);
Classes.H(:, :, i) = pop(:, 6*n+1:7*n);
Classes.R(:, :, i) = pop(:, 7*n+1:8*n);
Classes.D(:, :, i) = pop(:, 8*n+1:9*n);
Classes.Ir(:, :,i) = pop(:, 9*n+1:10*n);
I now extract the solution at each time t
Sk = zeros(n,soc); Ek = zeros(n,soc); Ak = zeros(n,soc); Jk = zeros(n,soc);
Qk = zeros(n,soc); Ik = zeros(n,soc); Hk = zeros(n,soc); Rk = zeros(n,soc);
Dk = zeros(n,soc); Irk = zeros(n,soc);
for j =1:soc
S(:,:,j) = reshape(pop(1:1:n), n, 1);
Sk(:,j) = S(:,:,j);
E(:,:,j) = reshape(pop(n+1:1:2*n), n, 1);
Ek(:,j) = E(:,:,j);
A(:,:,j) = reshape(pop((2*n)+1:1:3*n), n, 1);
Ak(:,j) = A(:,:,j);
J(:,:,j) = reshape(pop((3*n)+1:1:4*n), n, 1);
Jk(:,j) = J(:,:,j);
Q(:,:,j) = reshape(pop((4*n)+1:1:5*n), n, 1);
Qk(:,j) = Q(:,:,j);
I(:,:,j) = reshape(pop((5*n)+1:1:6*n), n, 1);
Ik(:,j) = I(:,:,j);
H(:,:,j) = reshape(pop((6*n)+1:1:7*n), n, 1);
Hk(:,j) = H(:,:,j);
R(:,:,j) = reshape(pop((7*n)+1:1:8*n), n, 1);
Rk(:,j) = R(:,:,j);
D(:,:,j) = reshape(pop((8*n)+1:1:9*n), n, 1);
Dk(:,j) = D(:,:,j);
Ir(:,:,j) = reshape(pop((9*n)+1:1:10*n), n, 1);
Irk(:,j) = Ir(:,:,j);
end
What I want to do with Sk (10 by 21) is to be able to subset Sk, such that Sk(1 by 21) is the vector of group 1 by 21 and Sk(2 by 21) is group 2 by 21. however the code line seems to be repeating the same thing for 1 to 10.
댓글 수: 0
채택된 답변
Hassaan
2024년 1월 10일
To organize the data as you described, you can simplify the extraction process like this:
% Assuming 'pop' is updated within your ODE loop
for i = 1:length(tspan)
% Update the 'pop' variable with the ODE solver output at time tspan(i)
% ...
% Assuming 'pop' is a matrix with dimensions [time, compartments*groups]
% Extract each compartment for each group at this time
for j = 1:soc
idx_start = (j-1)*n + 1;
idx_end = j*n;
Sk(:, j) = pop(i, idx_start:idx_end); % Extract group 'j' for compartment 'S'
% And similarly for the other compartments
end
end
% Now 'Sk' should be an [n, soc] matrix where each column represents one group over time
This loop will extract the population of each group at each time point for each compartment and store them in the respective matrices. The resulting Sk will have a dimension of n rows (number of groups) and soc columns (number of scenarios or time points), where each column j corresponds to the population of the entire group at a specific time point.
If Sk is supposed to be [10, 21] where 10 represents the number of groups and 21 represents the time points, then ensure that pop has the appropriate dimensions so that when you extract segments of pop, you're getting the correct group's data for each time point. The reshaping in your original code should be adjusted to match the actual structure of your data in pop. If pop is already organized with one row per time point and sequential groups of compartments, you can simplify the assignment without reshaping:
Sk(:, j) = pop(i, (j-1)*n+1:j*n);
This line assumes pop has one row per time point, with each group's compartments laid out sequentially in columns. The loop variable i should correspond to the current time point being processed, and j should iterate over the number of groups (soc).
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
Feel free to contact me.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!