Realtime simulation in MATLAB Simulink (how to run a MATLAB Simulink model in realtime) (2023)


One viewer asked how to change the simulation time. I decided to record a short video on how to run a MATLAB Simulink model in real time because it has a real application in practice. In this video, I also briefly discuss why we run real-time simulations and why looping hardware simulation offers some advantages. Real-time simulation work (for system protection and for motor drive and controller) is something we (usually my colleagues) do on a regular basis to further design and analyze the system.

N=10, fraction=226
Available budget (N):
Storage limit:


Hi, in this video I'm going to show you how to make a real-time simulation in Matlab Simulink.

To demonstrate this, I have a very simple circuit here.

This is the RC circuit I'm going to charge.

So I have a voltage source that is currently 100 volts.

But I connected it to this button that I can move later during the simulation, I have a switch here.

I'm actually disconnecting.

So I put infinity here.

The switch is turned on by this electrical impulse.

Thus, a pulse is sent in one second.

And the duration of the transition is very long.

So during the simulation, the capsule stays on when turned on, and now we have a resistor and a capacitor.

It's the resistance of thousands.

The ohmic capacitor has a capacitance of 5 millifarads, which gives us a time constant rc of 5 seconds.

And we expect that from 5 times the time constant, which is 25 seconds.

In that case, the voltage across this capacitor will reach its final value, now I'm going to measure the voltage at the input here and also at the output.

Let me run the simulation.

So we see that everything is as we expect it to be.

So, in one second, pause, turn on, the capacitors will start charging.

And after about 25 seconds the end value is reached.

Of course, I should consider 26 seconds here because it starts at one second.


Now that everything is clear, I will repeat.

You will notice here that this simulation lasts 50 seconds in real time.

When we run the simulation, it only takes a fraction of a second to run it in real time for 50 seconds.

Now the circuit will not be so simple in many cases.

The circuit will be extremely complex.

For example, if you want to model part of an electrical network, the system is very complex.

For example, if you want to model a second in real time and you're using a regular computer, it can take tens of minutes to run that one second.

Now, if you want to run in real time for one second within one second of simulation time, you obviously need a very powerful computer.

Sometimes you need a supercomputer, why real-time digital RTDS simulator, these are very expensive.


Before I explain how to run this simulation in real time.

Let me explain why we want to run a real-time simulation at all? Suppose you want to design a system.

And of course, if it were very quick and very cost effective to build this system, let's say very cheap, then maybe the best way is to build the hardware.

And we test in real life.

We can actually see what is happening with the hardware.

But in many cases this is not possible.

For example, if you want to build something like a really big, really big system, say a high voltage HVDC mesh network.

This system will therefore cost billions of euros and take many years to build.

So you can't really build hardware and start experimenting with it.

That's not how it works.


So we have all the offline simulations done, basically we model the components, we try to simulate the offline and design the system within that offline simulation, see how it works and come up with the best possible design.

So if the system you want to design is very sensitive it will be very expensive.

This offline simulation may not always be convincing as it applies to every physical part you have.

Every time you make a model, that model is still an approximation that you can't.

You cannot describe all the phenomena of real life.

For example, if you have a steering wheel and you connect it to an electric machine.

So in the computer you obviously already have an algorithm that controls this controller and everything, but maybe in this algorithm you don't have that.

Think about the delay these impulses take to reach the machine and all the other little things the model might not be able to account for.

So the offline simulation is very good, excellent.

But it can't show everything in real life, for example.

What can we do now? We can actually improve this offline simulation by running the simulation in real time.

In this case, in principle, a large part of the system will still be modeled.

So in the computer we create a model of it.

But part of the real system.

In fact, we take the hardware and plug it into this computer model.

So since we have hardware here, and it's connected to this computer model, obviously the computer model has to be real-time, because this hardware is real-time.

So the simulation has to run in real time too, otherwise they can't talk to each other.

So basically we're saying we have hardware in a loop.

This is real-time loop simulation hardware.

In this case, we are more confident because at least some of the actual system design is in real hardware.

Basically, we can run a lot of these simulations and change the state of the system a bit to see how this hardware will behave.

Sometimes we like different sensitivity analyzes and different scenarios.

So ultimately this hardware simulation in a loop will give you more confidence that your design will actually work.

And if you're 100% sure about it, that's the best we've got.

And then we can basically go ahead and design the same system in reality and test it in real life.


So that's enough.

So let's move on to Matlab and I'll show you how to do this simulation in real time.

Now I run it again, you can see that it runs very fast.

So it's not real time.


So what you do is click on the library.

And you're basically looking for real-time synchronization for that block.

So you take a block and add it to your model.

Now, if you double click, two boxes will appear here.

One of them is the sampling time.

That is, every 0.1 second.

And the simulation time is monitored in real time.

If they fit, that's fine.

If they don't match, it will count as one violation.

So here in the second part, put the maximum errors of 10 times as 10 times in a row, in this case the simulation time does not correspond to the real time.

Then your simulation actually stops.


Now give me 0.1.

The other one is really big, so let's go.

This stand.

For example, 10 10 milliseconds.

For example, if you want to conduct a power system survey and check system security, this time scale can be much less than 50 microseconds 10 points.

These should be smaller numbers.

And for that you need a very powerful computer.

Let me include those errors as well, because it simply doesn't matter here.

So I bet hundreds, maybe thousands.

So I accept even if the time is wrong thousands of times.

I ran the simulation.

I don't stop.

And then I click here too.

So if you click here, the plugin will appear here.

Then I double click on the oscilloscope and add another port.

I already had two, now I've added two three, basically I can associate it with a serious range.

In this case, we can see all the cases where the simulation time does not match the actual real time.


Now let me handle it.

Now you see that this simulation is now running in real time.

And here you can see that in some cases when I move the mouse there are some mismatches.

So you notice that now there is no mismatch because the CPU is completely used to solving this circuit.

But when I move the mouse because now my CPU is using a bit more power to process that movement and all.

So you see we're wasting more time.

And if I stop the simulation now, I'll stop it for a few seconds, two, three seconds.

And then I do it.

You can see this as real time continues but then the simulation stops.

At this point, there are many cases where the simulation does not fit.

And then because this simulation is faster than real time.

Eventually it catches up and it actually arrives.

Okay, let me do it again.

And now in this case, for example, if I change the voltage with the button, you can see that if I make a change, the voltage naturally drops, and so on, and that's it, it doesn't matter.

But you notice that when I do an action, the mismatch rate will actually be higher because the CPU is being used by other activities.


Now we basically have a simple real-time simulation where, for example, if it's a real system, we can, for example, connect the voltage of this capacitor to external hardware and start the process when the voltage of the capacitor reaches certain values, for example.

And so on, good and that's all in this video bye.


How do I make Simulink model run in real-time? ›

After you open a Simulink model and start the target computer, you can enter the simulation parameters. In the MATLAB Command Window, type slrt_ex_osc_ucf . In Simulink Editor, from the Apps tab, click Simulink Real-Time.

How do I increase Simulink simulation time? ›

Speeding up Simulink simulation
  1. Avoid unnecessary continuous signals.
  2. Avoid simulating dead-times.
  3. Avoid unnecessary variable PWM phases.
  4. Use Simulink's accelerator.
  5. Close unnecessary scopes.

How to run Simulink simulation from MATLAB? ›

Simulate a model interactively by clicking the Run button in the Simulink Toolstrip, or programmatically using functions like sim and set_param in the MATLAB Command Window or a MATLAB script. For information about running parallel and batch simulations, see Run Multiple Simulations.

How do I run Simulink model multiple times? ›

Run multiple Simulink® simulations corresponding to different test cases in the Signal Editor block using SimulationInput objects and the parsim command. The parsim command uses Parallel Computing Toolbox™, if it is available, to run simulations in parallel, otherwise the simulations are run in serial.

What is the difference between Simulink model and real-time model? ›

SLDRT runs as a single process on a single core whereas SLRT can utilize multi-core execution. There are some other differences such as that SLRT target machines can be run in a standalone mode and thus can be made portable to be deployed with a physical system.

What is Simulink real-time in MATLAB? ›

Simulink Real-Time™ and Speedgoat take you from simulation to rapid control prototyping (RCP) and hardware-in-the-loop (HIL) testing in a single click. The products connect to electronic control units and physical systems with MATLAB® and Simulink®.

Why is Simulink running so slow? ›

- If the memory used by variables stored during the simulation is greater than the amount of RAM on the system, performance will be poor. The solution is to run smaller simulations or add more memory.

How to time a simulation in MATLAB? ›

time = getCurrentTime( obj ) returns the current simulation time in the MATLAB System block. Call this method in the stepImpl method of your System object™.

What is difference between MATLAB and Simulink? ›

The Simulink approach is based on time based and multi rate system. SO that will be useful for HDL code generation. Whereas, MATLAB is for the mathematical based algorithm development and which will not consider the time while in simulation (independent of time). Simulink is graphical and more interactive to the user.

Can Simulink generate MATLAB code? ›

You can generate code that represents a complete algorithm, or standalone utility code that several model blocks share. To get started, see Simulink Coder.

Is Simulink a programming language? ›

Yes Simulink is a graphical programming language because, the source code itself is graphical in nature, though text can be included.

What is the most efficient way to simulate a model multiple times? ›

A method of simulating a model numerous times without re-translating the model, would be to use the translateModel and simulate functions.

What is the step time for Simulink simulation? ›

To capture both outputs, the fixed-step solver must take a time step every 0.25 second (the fundamental sample time for the model). By contrast, the variable-step solver need take a step only when the model actually generates an output.

What is the difference between sample time and simulation time? ›

Sample time should be smaller then the simulation time. Because sample time represents the sampling period after which MATLAB will fetch the data for reading. If your simulation time is lesser then the sampling time then there is no data fetched for the simulation.

Does NASA use Simulink? ›

The Space Launch System (SLS) rocket is designed to carry humans into deep space. Using MATLAB® and Simulink® for simulation and validation, the complex mission management logic is designed to ensure that the SLS can correctly respond to nominal and off-nominal events.

Is Simulink real-time? ›

Simulink Real-Time™ lets you create real-time applications from your Simulink® models. You can run real-time applications on Speedgoat hardware that connects to your device under test, through physical I/O lines, and communication channels.

Does Tesla use Simulink? ›

To meet aggressive technology goals on a strict budget and timeline, the Tesla Motors design team relied on Simulink and MATLAB to model the entire vehicle and its major subsystems.

Can MATLAB run in real-time? ›

When you run the real-time application, you can start and stop execution; observe signals and log signal data; and tune real-time application parameters. You can use the following interfaces to run the application: Simulink® Real-Time™ Explorer and Simulink external mode.

Can MATLAB work in real-time? ›

To run your real-time application in a repeatable manner, build, download, configure, and run your real-time application with MATLAB® language. You can: Perform runs interactively or initiate automated runs and test sequences.

What is the difference between total time and self time in Simulink? ›

Self-time is the time taken by a method for all the lines of code within it and excluding the time taken by any other methods which were called inside it. Total time is the time taken by a method for all the lines of code within it and including the time taken by any other methods called inside it.

How to install Simulink real-time kernel? ›

The Simulink® Desktop Real-Time™ software requires a real-time kernel that interfaces with the operating system.
Install the Kernel Using MATLAB
  1. In the MATLAB® Command Window, type: ...
  2. Type y to continue installing the kernel, or n to cancel installation without changing the installation.

What is Simulink code real-time workshop? ›

What Is Real-Time Workshop? Simulink Code Generator — Automatically generates C code from your Simulink model. Make Process — The Real-Time Workshop user-extensible make process lets you customize compilation and linking of generated code for your own production or rapid prototyping target.

How to set time range in Simulink? ›

Time Scope uses the Time span and Time display offset parameters to determine the time range. To change the signal display settings, select View > Configuration Properties to bring up the Configuration Properties dialog box. Then, modify the values for the Time span and Time display offset parameters on the Time tab.


Top Articles
Latest Posts
Article information

Author: Delena Feil

Last Updated: 06/06/2023

Views: 5795

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Delena Feil

Birthday: 1998-08-29

Address: 747 Lubowitz Run, Sidmouth, HI 90646-5543

Phone: +99513241752844

Job: Design Supervisor

Hobby: Digital arts, Lacemaking, Air sports, Running, Scouting, Shooting, Puzzles

Introduction: My name is Delena Feil, I am a clean, splendid, calm, fancy, jolly, bright, faithful person who loves writing and wants to share my knowledge and understanding with you.