Thread Subject: function 'pause' inaccurate

Subject: function 'pause' inaccurate

From: Sonny

Date: 7 Jul, 2011 10:49:09

Message: 1 of 6

Hello,

I'm trying to create a function wait (time) like this:

function wait(time)
    T1 = get_param('model','SimulationTime');
    T2 = T1;
    while((T2-T1)<time)
        T2 = get_param('model','SimulationTime');
        pause(0.00000000001);
    end
    
but I heard about the inaccuracy of Matlab on Windows, and when I want a wait(2), the SimulationTime shows that Simulink wait 2.0211. For a function that is supposed to be executed in less than 5ms, the delay is too much.
Is there a way to do it more accurate?

Thanks

Subject: function 'pause' inaccurate

From: Phil Goddard

Date: 7 Jul, 2011 14:26:07

Message: 2 of 6

Simulink (under Windows, Linux or Mac) is a non-real time simulator, so you basically cannot control how long it will take to execute.
If you have hard real-time requirements then you need to use a real-time OS.

But it seems that you misunderstand what the SimulationTime property represents.
It is not how long the simulation is taking to run (if you were looking at a clock/watch and looking at the seconds tick by).
It is the time that the Simulink solvers are using to step the model through time (which may be faster or slower than real-time depending on the complexity of the model), and may or may not be close together (if you have a discrete model with a step size of 10 for instance then the SimulationTime will increment in steps of 10).

There is no way for you to guarantee that your T1 and T2 will be within a given tolerance of each other.

Phil.

Subject: function 'pause' inaccurate

From: Ben

Date: 7 Jul, 2011 15:03:10

Message: 3 of 6

"Sonny " <mailbox6620@gmail.com> wrote in message <iv42v5$kvf$1@newscl01ah.mathworks.com>...
> pause(0.00000000001);


In the past, I've discovered that for my setup (MATLAB 2007a, Windows7, 4GB ram, intel core i3 cpu), pauses get less and less accurate the lower you go:

>> tic; toc
Elapsed time is 0.000010 seconds.
>> tic; pause(0.1); toc
Elapsed time is 0.099868 seconds.
>> tic; pause(0.01); toc
Elapsed time is 0.019989 seconds.
>> tic; pause(0.001); toc
Elapsed time is 0.004425 seconds.
>> tic; pause(0.0001); toc
Elapsed time is 0.003636 seconds.
>> tic; pause(0.00001); toc
Elapsed time is 0.004403 seconds.

Also, since my tic; toc showed 0.00001, I would hardly expect that your value above (a factor of a million lower) would be accurate.

I agree with the previous poster, if you need this kind of resolution, you'll need to change your system around.

Subject: function 'pause' inaccurate

From: Sonny

Date: 8 Jul, 2011 08:12:10

Message: 4 of 6

"Phil Goddard" <phil@modeling-simulation-visualization.com> wrote in message <iv4flv$s1n$1@newscl01ah.mathworks.com>...
> Simulink (under Windows, Linux or Mac) is a non-real time simulator, so you basically cannot control how long it will take to execute.
> If you have hard real-time requirements then you need to use a real-time OS.
>
> But it seems that you misunderstand what the SimulationTime property represents.
> It is not how long the simulation is taking to run (if you were looking at a clock/watch and looking at the seconds tick by).
> It is the time that the Simulink solvers are using to step the model through time (which may be faster or slower than real-time depending on the complexity of the model), and may or may not be close together (if you have a discrete model with a step size of 10 for instance then the SimulationTime will increment in steps of 10).
>
> There is no way for you to guarantee that your T1 and T2 will be within a given tolerance of each other.
>
> Phil.

Thank you for your reply.
I'm still confused with SimluationTime:
- To me it means the time that Simulink try to simulate. 10s of SimulationTime during the simulation would mean 10s in a real device? But it's different to tic/toc that shows the real time the simulation takes to run(10s of tic/toc means I have waited 10s before the simulation finishes)
- SimulationTime depends on hardware requirements

is it right?

Subject: function 'pause' inaccurate

From: Ian Dougherty

Date: 15 Nov, 2011 21:24:10

Message: 5 of 6

"Sonny " <mailbox6620@gmail.com> wrote in message <iv6e4q$grr$1@newscl01ah.mathworks.com>...
> "Phil Goddard" <phil@modeling-simulation-visualization.com> wrote in message <iv4flv$s1n$1@newscl01ah.mathworks.com>...
> > Simulink (under Windows, Linux or Mac) is a non-real time simulator, so you basically cannot control how long it will take to execute.
> > If you have hard real-time requirements then you need to use a real-time OS.
> >
> > But it seems that you misunderstand what the SimulationTime property represents.
> > It is not how long the simulation is taking to run (if you were looking at a clock/watch and looking at the seconds tick by).
> > It is the time that the Simulink solvers are using to step the model through time (which may be faster or slower than real-time depending on the complexity of the model), and may or may not be close together (if you have a discrete model with a step size of 10 for instance then the SimulationTime will increment in steps of 10).
> >
> > There is no way for you to guarantee that your T1 and T2 will be within a given tolerance of each other.
> >
> > Phil.
>
> Thank you for your reply.
> I'm still confused with SimluationTime:
> - To me it means the time that Simulink try to simulate. 10s of SimulationTime during the simulation would mean 10s in a real device? But it's different to tic/toc that shows the real time the simulation takes to run(10s of tic/toc means I have waited 10s before the simulation finishes)
> - SimulationTime depends on hardware requirements
>
> is it right?

Yes, the pause command is for use in interactive Matlab programs that require the output to have a specific timing. It is not dependent on simulation time for Simulink, but on system time for the platform that Matlab is running on.

Subject: function 'pause' inaccurate

From: ScottB

Date: 15 Nov, 2011 21:56:29

Message: 6 of 6

"Sonny " <mailbox6620@gmail.com> wrote in message <iv42v5$kvf$1@newscl01ah.mathworks.com>...
> Hello,
>
> I'm trying to create a function wait (time) like this:
>
> function wait(time)
> T1 = get_param('model','SimulationTime');
> T2 = T1;
> while((T2-T1)<time)
> T2 = get_param('model','SimulationTime');
> pause(0.00000000001);
> end
>
> but I heard about the inaccuracy of Matlab on Windows, and when I want a wait(2), the SimulationTime shows that Simulink wait 2.0211. For a function that is supposed to be executed in less than 5ms, the delay is too much.
> Is there a way to do it more accurate?
>
> Thanks

This submission is pretty good for accurate relative time measurements:

http://www.mathworks.com/matlabcentral/fileexchange/16534-high-accuracy-timer-for-windows

hth,
ScottB

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
simulationtime Phil Goddard 7 Jul, 2011 10:29:12
simulink Phil Goddard 7 Jul, 2011 10:29:11
accuracy Sonny 7 Jul, 2011 06:49:13
pause Sonny 7 Jul, 2011 06:49:13
wait Sonny 7 Jul, 2011 06:49:13
rssFeed for this Thread

Contact us at files@mathworks.com