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

1 次查看(过去 30 天)
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 个评论
Jos (10584)
Jos (10584) 2016-2-24
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 2016-2-24
@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.

请先登录,再进行评论。

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by