0

My code gave me an error message that Y appears to change size on every loop.

yo = [0.5;0.5;1];
% computing the trajectory
dt = 0.01;
tspan = (dt:dt:100); % number of time steps
Y(:,1)= yo;
yinput = yo;
for i = 1: tspan(end)/dt
    time = i*dt;
    youtput = rk4angelstepb(@rosslerb,dt,yinput,a,b,c);
    *Y = [Y,youtput]*;
    yinput = youtput;   
end

My code ran but I have two issues: fisrtly, the line stared gave a warning that says Y appears to change on every loop. Secondly, i tried to create a big Y, where the first is the initial condition. but I found out that Y is not equal to my initial condition yo. what am I doing wrong.

Emi
  • 1
  • 4
  • You posted two `for` loops. Which one is the right one? `for i = 1: tspan(end)/dt` doesn't make sense. Should it be `for i = 1:length(tspan)/dt`? What is the output size of `rk4angelstepb`? is it a scalar? The "error message" is not an error, it's just an optimization warning. – Rotem Jun 27 '19 at 17:39
  • 1
    In one loop you show `Y(i) = [Y;youtput];`, which doesn't make sense. This loop is also missing an `end`. Is this a copy-paste error? Please [edit] your question to correct the code! – Cris Luengo Jun 27 '19 at 19:05

1 Answers1

0

Matlab runs more efficiently if you allocate memory fully in advance. Statements like Y = [Y;youtput]; expand Y incrementally, which is less efficient. It looks like you were partway to a solution. Here's something which allocates space first and then sets one element at a time inside the loop.

Y = zeros(tspan(end)/dt,1);
for i = 1: tspan(end)/dt
    time = i*dt;
    youtput = rk4angelstepb(@rosslerb,dt,yinput,a,b,c);
    Y(i) = youtput;
    yinput = youtput;
end
nhowe
  • 909
  • 6
  • 14
  • Thank you. I have tried it. but got a different error code: Unable to perform assignment because the left and right sides have a different number of elements. Error in simulateROSSLERb (line 16) Y(i) = youtput; – Emi Jun 27 '19 at 20:46
  • 1
    @Angela: If you want a better answer than this one, you need to edit your question to include a [mre]. We don't know what `rk4angelstepb` is, or what it returns. We don't know the purpose of your code. Please read [ask] and the links therein. – Cris Luengo Jun 27 '19 at 22:09