why can't I assign value to a matrix in a loop?

1 view (last 30 days)
tout = [0,2]; %%%output times
u0 = [30;1;30]; %%%initial data
M=zeros(10,3);
N=zeros(9,3);
y=zeros(9,1);
x=zeros(9,1);
for i=1:10
n=2^i; %%% the number of equal sized steps
tic %%%start timer
[t,u] = oderk4(@chemcon,tout,u0,n); %%%run the solver
t2=toc; %%%stop timer
M(i,:)=u(end,:);
end
oderk4 is a function for solving ode, and the output of oderk4 are a vector t and a matrix u. I want to use the last row of every matrix u, so I let M to represent them. However, M doesn't have value after the loop. The workspace shows M is NaN. What's wrong with it?

Accepted Answer

Jan
Jan on 8 Mar 2015
If M contains only NaNs after the loop, this is the reply of oderk4. Therefore I suggest to use the debugger to finde out, where insice the ODE solver the NaNs are produced. There is no problem in the shown code.
  1 Comment
smilerukia
smilerukia on 9 Mar 2015
Thanks! I figure it out. It's the problem of the step size that I chose.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!