Machine precision in simulink

5 views (last 30 days)
MARCO LORENZI
MARCO LORENZI on 14 Jul 2021
Edited: dpb on 16 Jul 2021
Good morning everyone.
I am currently working on simulink model.
One of the equations is
"L = qf*(XL0+(XL1*alpha_quadrant))"
where "alpha_quadrant" is constantly zero. "XL0" and "qf" are both constants.
The result of the equation, when graphed, should be constantly -11.08, but as it can be seen in the picture the result keeps fluttering.
Is it because of the machine precision?
  3 Comments
dpb
dpb on 15 Jul 2021
"All that noise" is also in the 14th decimal place in the output. How can that make any real difference?
Just set
ylim([-10 -12])
on the plot so the tiny little jiggle isn't so amplified and you'd never have noticed it.

Sign in to comment.

Accepted Answer

Andy Bartlett
Andy Bartlett on 14 Jul 2021
To determine what is happening, a good approach is to log the key signals involved.
Then use MATLAB to investigate the logged values.
I suspect you'll find that either alpha_quadrant isn't always zero or qf is not constant or XL0 is not constant.
The differences in values from what you expect might be very small so you could use some techniques like the following.
format long g % will shows higher precision than short, but can still hide some differences
a = 0.05;
b = a + eps(a);
c = a + 2*eps(a);
v = [c,a,b] % the differences will not be visible in the command window
% converting to a string with 17 digits will be enough to see differences
mat2str(v,17)
% subtracting values is another way to make the differences visible in the
% command window
dv = diff(v)
max(dv)
min(dv)
The output of this code is:
v =
0.05 0.05 0.05
ans =
'[0.050000000000000017 0.050000000000000003 0.05000000000000001]'
dv =
-1.38777878078145e-17 6.93889390390723e-18
ans =
6.93889390390723e-18
ans =
-1.38777878078145e-17
Notice that differences in v are hidden even by format long g.
  4 Comments
dpb
dpb on 15 Jul 2021
That really isn't what Andy said -- he said, in fact, the obverse -- if you want smaller jiggles on the near constant, you have to actually add more precision, not reduce it.
You can loosen tolerances to get fewer significant digits although if you go too far, solutions may,in fact, diverge from the real solution completely.
Before that point, while your simulation may run faster and will have lower absolute accuracy, it won't be by effectively "zero-ing out" more digits of precsion; the simulation will still be calculated in double precision floating point and so will have the full mantissa; what you'll get will be even more jiggles in your constant up to that level of accuracy you've specified for the solver.
There simply is no such thing as you're thinking of it as being N digits of numeric precision and everything past that identically zero -- in fact, that is in essence infinite precision because you've set a requirement on every digit from the first to as many as you want to name.
You could, I guess, insert an s-function as the output of every block that rounds the result before passing it to the next, but it's futile and pointless to do so...just use the simulation results you get. As noted above, if you were to change the scale on the plot to something realistic about the magnitude of the value itself, you would not even see the jitter and would not have even recognized it as being there.
It just doesn't matter in a practical sense.

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by