Difference between signal passed to Simulink and immediately logged

3 views (last 30 days)
I'm working with some Simulink models (2016a) where I pass some Matlab workspace signals to Simulink using the From Workspace block. As a sanity check, I tried to log the very same signal to Matlab using the To Workspace block and I noticed something weird.
Essentially, I get no error between the original Matlab signal and the logged Simulink signal for the first half of the simulation, while for the remaining part of the simulation there is some error building up between the signals. It's not a big error, compared to the signals I'm using, but I'm just wondering if there's any way to get exactly the same result (it doesn't seem to be machine precision).
This is a screenshot of the model (which I also attached):
and this is what I obtain comparing the signals (notice how the difference is of several order of mags, but abruptly goes from 0 to some 1e-13 value):
The code I run
fs = 16e3; % sampling freq
siglen = 1; % 1 second
siglen_samp_rec = siglen*fs;
data = 0.1*rand(1,siglen_samp_rec).';
time_test_ts = (0:1/fs:(siglen_samp_rec-1)/fs);
% Save to time series
test_ts = timeseries(data,time_test_ts);
options = simset('SrcWorkspace','current');
tic
sim('test_diff',[],options)
toc
test_TD = squeeze(test_tsl.signals.values);
figure;
subplot(211); plot(data); hold on; plot(test_TD(1:end-1)); title 'Signals'
subplot(212); plot((data-test_TD(1:end-1))); title 'Error'
norm(abs(data(1:siglen*fs)-test_TD(1:siglen*fs)))

Answers (1)

Mark McBroom
Mark McBroom on 9 Jun 2018
Is the "interpolate data" option set for the "from workspace" block? I am guessing there are slight nuerical differences between Simulink time at a given time step and the time in your timeseries. As a result, Simulink is interpolating your timeseries data to match with current simulink time, causing a slight difference.
  1 Comment
gbernardi
gbernardi on 12 Jun 2018
Yeah, you're right. The "interpolate data" option is indeed set. As a check I tried to plot the timeseries field "time" versus the field "time" from the "To Workspace" block and they're diverging from each other, too.
However, if I untick the "interpolate data" option, the results deviate even more.
I'd suppose the strategy to obtain the same results would be that of removing the numerical differences between the time instants in the timeseries and those in Simulink. Perhaps in the "Model configuration parameters"
I'm asking since for the long signals I'm using these numerical differences build up in a quite substantial manner.

Sign in to comment.

Categories

Find more on Prepare Model Inputs and Outputs in Help Center and File Exchange

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!