Creating a matrix per iteration

7 ビュー (過去 30 日間)
Martin Matin
Martin Matin 2015 年 3 月 6 日
コメント済み: Geoff Hayes 2015 年 3 月 8 日
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 Center および File ExchangeOrdinary Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by