# Why do I receive incorrect result from my model when I use the Inport block to specify my time and signal vector in Simulink?

5 views (last 30 days)
MathWorks Support Team on 8 Feb 2012
In the model attached, I use the Inport blocks and divide the first input over the second. Using the following code, I initialize my time and signal vectors and simulate my model:
dt = 1/10;
time = [0:dt:5*dt]';
portvalues1 = [0 0 0 0 1 0]';
portvalues2 = [0 0 0 0 1 0]';
t = time;
u = [portvalues1, portvalues2];
sim('test', [t(1) t(end)], [],[t u]);
correct_output = u(:,1)./u(:,2)
Where “dt” represents the step size of the simulation, “t” is my time vector and “u” is my input vector. After simulating it using the SIM command, I display both the correct output and Simulink output.
After running the code, I notice that I receive the following incorrect result when I compare the two outputs:
correct_output =
NaN
NaN
NaN
NaN
1
NaN
NaN
NaN
NaN
1
1
NaN
This is the behavior in Simulink when time vector is defined using the following technique:
time = startTime : stepSize: stopTime

MathWorks Support Team on 8 Feb 2012
This is caused by roundoff error. When time vectors are defined using the above technique, depending on the value of the step size used, there will be times when the time steps will not match Simulink’s time-step. To take a closer look at the difference between the time step that was provided and the time steps that Simulink generates, use the hexadecimal representation through the following command:
format hex
You can then compare the time steps specified using the colon operator technique mentioned above, and the time steps that Simulink generated as follows:
>> t
t =
0000000000000000
3fb999999999999a
3fc999999999999a
3fd3333333333333
3fd999999999999a
3fe0000000000000
>> tout
tout =
0000000000000000
3fb999999999999a
3fc999999999999a
3fd3333333333334
3fd999999999999a
3fe0000000000000
Where “t” represents the user time steps and “tout” represents the time steps generated by Simulink. In particular, notice that the fourth element of these two variables is different by one bit. This causes the Inport block to use the Lagrange interpolation to interpolate the input data and match the Simulink’s time steps. In return, certain operations on the input data results in an unexpected behavior.
The best way to represent evenly spaced time steps such that the time steps will match Simulink’s, is to use the following technique:
time = stepSize * [0:N]'
Where “N” represents the number of time steps, similar to:
N = (stopTime - startTime) / stepSize;
Using the attached model and the following code:
dt = 1/10;
time = dt*(0:5)';
portvalues1 = [0 0 0 0 1 0]';
portvalues2 = [0 0 0 0 1 0]';
t = time;
u = [portvalues1, portvalues2];
sim('test', [t(1) t(end)], [],[t u]);
correct_output = u(:,1)./u(:,2)
The correct result is achieved:
correct_output =
NaN
NaN
NaN
NaN
1
NaN
NaN
NaN
NaN
NaN
1
NaN
Additional information has been updated in the documentation on this subject. You can find this information under "Importing Data to Test a Discrete Algorithm" at the documentation link here;

MathWorks Support

### Categories

Find more on Reporting and Database Access in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!