Creating a matrix per iteration

7 次查看(过去 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.

采纳的回答

Geoff Hayes
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
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 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