Getting subscripted dimension error while saving a variable in a loop. Now sure what my mistake is!

I am getting the subscripted dimension mismatch error. The error occurs after first iteration is complete. Not sure where I am going wrong. Below is my main code:
clear all
y0 = 20000; % initial conditions
iter = 0;
years = 3;
for p = 1:1:years
display(iter)
iter = iter+1;
ll = 273;
ul = 273 + 91;
wl = (ul-ll).*rand(1,1) + ll;
yearlength = wl+0.1;
finaltime = p*yearlength;
t = 0:finaltime;
mugen;
Kgen;
d1gen;
n = 2;
deltat = 1;
tspan = 0:deltat:finaltime;
options = odeset('RelTol',1e-10,'AbsTol',1e-10);
sol = ode23s(@(t,y)para_1d(t,y,n,mug,Kg,d1g),tspan,y0,options);
X = sol.x;
Y = (sol.y)';
Ytrans = Y';
solution(iter,:) = Ytrans(1,:); % This is where it gives me error at iter=2;
display(solution(iter,end))
clearvars -except solution iter
y0 = solution(end,end);
display(y0)
end
This is the function where my ode is:
function dy = para_1d(t, y,n, mug, Kg, d1g)
count = ceil(t)+1;
dy(1,1) = (mug(count).*(y(1).^n)/(Kg(count).^n+y(1).^n)) - d1g(count).*y(1);
and the parameter files are attached to the question

2 commentaires

Did you check the dimensions of Ytrans(1,:), solution and the value of iter? You can check them using debugging (dbstop on error)
@Rose: Depending on your Matlab version, clear all might remove the break condition set by dbstop on error. So better omit this useless and time consuming brute clearing of everything.

Connectez-vous pour commenter.

 Réponse acceptée

The length of tspan for iter=1 is half the length of tspan for iter=2.
Thus the length of the "sol" vector for iter=1 is half the length of the "sol" vector for iter=2.
Thus the free dimension (:) in
solution(iter,:) = Ytrans(1,:);
is different for iter=1 and iter=2. This is not allowed.
Best wishes
Torsten.

Plus de réponses (0)

Catégories

En savoir plus sur Numerical Integration and Differential Equations dans Centre d'aide et File Exchange

Question posée :

le 23 Fév 2016

Commenté :

Jan
le 24 Fév 2016

Community Treasure Hunt

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

Start Hunting!

Translated by