MATLAB: Why can’t I assign value to a matrix in a loop

arrayfor loopode

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?

Best Answer

  • 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.