Creating a matrix per iteration
조회 수: 4 (최근 30일)
이전 댓글 표시
Hello,
I'm trying to create a new matrix for each iteration of my loop. Here's the problem, I have to test 3 differential equations solvers, i.e. ode45, ode23, ode15s. I'd like to save the result of each loop in a matrix.
Here's the code :
for i=1:5
for j=1:5
options(i,j) = odeset ('RelTol',10^(-i),'AbsTol',10^(-j));
[T1,X]=ode45(@odefunction,[0 5],[0 5],options(i,j));
[T2,Y]=ode23(@odefunction,[0 5],[0 5],options(i,j));
[T3,Z]=ode15s(@odefunction,[0 5],[0 5],options(i,j));
end
I don't know the size of the matrix since the result of each ode gives a different size.
Thank you.
댓글 수: 0
채택된 답변
Geoff Hayes
2015년 3월 7일
Martin - save your output to a cell array which allows for each element to be of a different type or size which is perfect for your example. For example, you could have one cell array where each element is a structure that has fields for X, Y, Z, T1, T2, and T3. Something like
data = cell(5,5);
for i=1:5
for j=1:5
options(i,j) = odeset ('RelTol',10^(-i),'AbsTol',10^(-j));
[T1,X]=ode45(@odefunction,[0 5],[0 5],options(i,j));
[T2,Y]=ode23(@odefunction,[0 5],[0 5],options(i,j));
[T3,Z]=ode15s(@odefunction,[0 5],[0 5],options(i,j));
data{i,j}.T1 = T1;
data{i,j}.X = X;
data{i,j}.T2 = T2;
data{i,j}.Y = Y;
data{i,j}.T3 = T3;
data{i,j}.Z = Z;
end
Try the above and see what happens. As an aside, you may want to use indexing variables other than i and j which MATLAB uses to represent the imaginary number.
댓글 수: 1
Geoff Hayes
2015년 3월 8일
Martin's answer moved here
Thank you very much, I thought I had to use the cell command but I didn't know how to use it properly. Your code works perfectly, this is exactly what I wanted.
By the way, thanks for the advice, I'll rename the two variables used for the loops.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!