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

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.

댓글을 달려면 로그인하십시오.

 채택된 답변

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개)

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

질문:

2016년 2월 23일

댓글:

Jan
2016년 2월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by