How do I make make my simulation run for longer than 2085 loops. Index out of bounds because numel ()=1 prevents my model for running for the full amount of time.

2 次查看(过去 30 天)
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;
%Counter j=1;
%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 Model
sim('Model');
%Return values from the To Workspace sink blocks
xdis=Displacement.signals.values;
vvel=Velocity.signals.values;
aacc=Acceleration.signals.values;
ttime=Displacement.time;
%Assign Values
xstore(j+1)=xdis(2);
vstore(j+1)=vvel(2);
astore(j+1)=aacc(2);
timestore(j+1)=ttime(2);
%Print values
time
%Setup start values for next loop
x=xstore(j+1);
v=vstore(j+1);
time=timestore(j+1);
%Advance time step
Start=Start+Step;
Finish=Finish+Step;
%Advance counter
j=j+1;
end
plot(timestore,xstore)
end
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.

采纳的回答

Niklas Nylén
Niklas Nylén 2014-4-4
The issue seems to be that your step time (0.005) can not be exactly represented by a double precision floating point number:
fprintf('%0.20f\n',0.005)
0.00500000000000000010
Since you run your model just one step at a time, with the start time set to the previous stop time, what happens is that at time 10.42 the small rounding causes the model to stop before the step is actually made, i.e. only the start calculation is performed. This means that Displacement.signals.values will only contain one value for that time step.
There are two possible solutions that I can think of:
Consider if it is possible to change your implementation so that you do not need to initiate each time step from the script (in the attached model there is no reason to step the model from a script).
Set Step = 1 and TotalTime = round(15/0.005) and then multiply the signal values by 0.005 to adjust for the change in step time.
  3 个评论
Hugh McQueen
Hugh McQueen 2014-4-4
The rounding error problem makes sense so thank you both for pointing this out.
Niklas, how would go about changing the model so that it does not step from the script? Do you mean that I should get rid of the loop in the script completely?
I tried your second suggestion of scaling the time so that Step = 1 and TotalTime=3000 and then multiplying the signals.values by 0.005. The problem with this is that time is not scaled, and as the Simulink model integrates with respect to time, the values quickly become very very large. Is there a way around this?
Ilham, could you please expand on what you mean by 'why use mfile'?
Thanks
Niklas Nylén
Niklas Nylén 2014-4-7
1. Does it not work to just press play in simulink?
2. You could use gain block(s) with value 0.005 in the model to make the scaling online.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Configure and View Diagnostics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by