I'm using a Simulink model along with a MATLAB script. The program is modelling a simple spring mass damper system. I have used a 'for' loop in the script to carry out the iterations.
In the MATLAB script I have defined several 'storage vectors' where the 'To Workspace' (a type of Simulink sink block) saves the various values for displacement(x), velocity(v), acceleration(a), force(Q) and time(t). After one loop has run, the time step is advanced by the defined 'Step' value (0.005 seconds), and the new x,v,a,time values are defined for the Simulink model to run the next iteration/loop.
The model runs perfectly for the first 10.415 seconds (2085 loops) and then throws up the error:Attempted to access xdis(2); index out of bounds because numel(xdis)=1. Error in Trial (line 53) , xstore(j+1)=xdis(2);
Why does this happen after this exact number of loops?
My Simulink model saves the data in 'Structure With Time' format. The solver is Fixed-Step ode4 Runge-Kutta solver. The code is as follows:
%Define initial constants M=24.518; C=55.601; K=8756.342;
%Initial Velocity U=0.127;
%Initial values v=0; x=0;
%Time and time step Step=0.005; TotalTime=15;
%Initial time values Start=0; Finish=Step;
%Define storage vectors xstore=zeros((TotalTime/Step),1); %Displacement vstore=zeros((TotalTime/Step),1); %Velocity astore=zeros((TotalTime/Step),1); %Acceleration Qstore=zeros((TotalTime/Step),1); %Force timestore=zeros((TotalTime/Step),1); %Time
%Initial Values Qstore(j)=0; %Initial force value time(j)=Start;
%Setup Loop for i=0:Step:TotalTime
%Run Simulink Modelsim('Model');%Return values from the To Workspace sink blocksxdis=Displacement.signals.values;vvel=Velocity.signals.values;aacc=Acceleration.signals.values;ttime=Displacement.time;%Assign Valuesxstore(j+1)=xdis(2);vstore(j+1)=vvel(2);astore(j+1)=aacc(2);timestore(j+1)=ttime(2);%Print valuestime%Setup start values for next loopx=xstore(j+1);v=vstore(j+1);time=timestore(j+1);%Advance time stepStart=Start+Step;Finish=Finish+Step;%Advance counterj=j+1;
A picture of my Simulink model can be found here: </matlabcentral/answers/uploaded_files/10889/Simulink%20Model.jpg>
I have tried the following things: – I have checked that the memory is not full. My storage vectors only use 0.5MB of space so I presume this isn't the problem (for a computer with 4GB of RAM). – I have set the Sink Block (To Workspace) parameters for the Limit data points to 'inf' so that there is no limit on the number of data points. (I'm using save format of 'Structure with Time').
Does anyone have any ideas of how to stop the model stopping early so that it runs for the full amount of time?
(This spring-mass damper model is a simplified model of another model I'm working on which experiences the exact same problem as this).
I'm using MATLAB R2013a.