How to create a multidimensional array inside a nested function?

Hello, I'm quite new at Matlab and I can't figure out how to solve the following problem:
I created a function to apply the Implicit Euler method to ODEs and inside there is a nested function containing a Newton solver. This is the code:
function [t, u, n_it, num_fval, c] = IEuler_Nw1(f, tspan, u0, Ti)
% u0 is a column vector
N = length(Ti);
u(:,1) = u0;
num_fval = 0;
for k = 2 : N
h = Ti(k) - Ti(k-1);
[u(:,k), n_it(:,k), c] = IE_Nw1_solve(u(:,k-1), f, Ti(k-1), h);
num_fval = sum(n_it);
end
t=Ti;
end
% Newton Solver (Jacobian is analytically computed)
function [vp,n,c] = IE_Nw1_solve(un1, f, t, h)
maxni = min(10^4, 100*length(un1));
tau = 10^(-8);
vp = un1;I=eye(numel(vp));
stop_rule = 1;
ni = 0;
while stop_rule
ni = ni + 1;
[F,Jf]=f(t, vp);
J=I-h*Jf;
s=J\(vp-h*F-un1);
vp = vp-s;
stop_rule = norm(s) > tau & ni < maxni;
c(:,ni) = norm(s);
end
n = ni;
end
To study the convergence, I would like to obtain the local error (in the code indicated as c) at each iteration of the Newton approach, for each time step of the Implicit Euler method. To do this I tried to generate a double array k(number of time steps) x n(number of iterations per single time step), but the issue is that n obviously is not the same for all the time steps, so I got the error "Subscripted assignment dimension mismatch.". If, instead, I leave c without further specifications as in the code written above, what I obtain is an array containing just the local error of the iterations referred to the last time step.
Essentially I really don't know how to proceed. I hope someone can help me to find a solution.

Respuestas (0)

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Preguntada:

el 13 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by