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

1 view (last 30 days)
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 Comments
Jos (10584)
Jos (10584) on 24 Feb 2016
Did you check the dimensions of Ytrans(1,:), solution and the value of iter? You can check them using debugging (dbstop on error)
Jan
Jan on 24 Feb 2016
@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.

Sign in to comment.

Accepted Answer

Torsten
Torsten on 24 Feb 2016
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.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks 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!