17 views (last 30 days)
Paul on 9 Oct 2023
Commented: Paul on 10 Oct 2023
I'm interested in vectorizing a Simulink model so that it'll be more flexible and computationally efficient. I'm analyzing traffic flow - the position and velocity of a leading car influence how the car behind it is going to react. I've shown this for the first three following cars below.
Needless to say, this gets difficult to implement for higher numbers of cars - my team's current solution is hardcoded to run this simulation for 175 cars, each one represented by subsystems like the ones shown above (it's a big system). Being able to represent n cars with a vectorized solution would be better, but I'm not sure how to implement it. Any thoughts? I'd also want to know how to initialize each car using vectors (e.g. a vector for the initial position, velocity of every car)
TL;DR - I want to express all of these Simulink subsytems as a single subsystem that outputs vectors representing the important outputs (Position, Velocity, Acceleration, etc.) whose lengths are based on the number of cars we decide to simulate. Solutions using Vectorization would be preferred, but any solution would be appreciated!

Fangjun Jiang on 9 Oct 2023
Simulink supports vector, so if designed carefully, a single Simulink subsystem could process N velocity and postion to generate N Energy and Power. The "Gap" might need special attention but overall, it is possible.
If the subsystem is designed to process single vehicle, then you can use the For Each Subsystem
Fangjun Jiang on 10 Oct 2023
Feeding the value of 1 to the Gain of 2, you got 2. Feeding the Value of [1 2 3] to the same Gain block, you got [2,4,6]. That is how Simulink works. Almost every block supports that.
Try this on your single vehilce model. Feed three velocities and three positions instead of one. See how the model simulates. It might not be difficult to make your single vehicle model work for multiple vehicles.
Paul on 10 Oct 2023
I believe that @Fangjun Jiang is refering to a feature called Scalar Expansion of Inputs and Parameters. Some blocks support that feature, like the Integrator, but others do not, like the Transfer Fcn. So if you go down this path, keep in mind that even if the model can be implemented this way today, it might limit what blocks you can use in the future if you need to modify your model for some reason. Unfortunately, the doc pages for each block in the Simulink libraries are lacking clarity, IMO, as to whether or not a block supports that feature, so there may be a bit of trial and error involved.
If you want to use different parameters for each car, start reading Parameter Interfaces for Reusable Components and links therefrom. Typically done using a masked subsystem from a custom library, or a Model reference block (which itself comes with parameterization options).
I think we've already determined that the For Each block won't work, at least not without adding an artificial time delay on the signals that flow from one vehicle the next.
Another option may be to use a Dynamic Masked Subystem. In this approach, we'd have a masked subsystem that has N as a block parameter and then the mask initiialization code can be used to add N instances of the IIDCar subsystem, connect them all up and create the output signals (speaking of which, it might be better to collect all of the output signals from the cars in vectors or a bus, rather than using all of those Goto blocks), and set the parameters for each car, if needed, based on a parameters of the top level mask. However, if you're new to Simulink this might be a long bridge to cross. Also, there may be a cost to pay in terms of rebuilding the model every time you change the N parameter. Disclaimer: I don't have too much experience with Dynamic Masks so don't know for sure if this will work, but offhand it seems like it should be doable.
Another, and possibly the simplest, option would be to bite the bullet and implement the maximum number of cars you'd ever possibly need, with the outputs of all cars combined into vectors and possibly a bus (or buses), but only use the outputs from the subset of cars you care about in any particular run. Wrapping the car subsystem inside an Enabled Subsystem and using copies of the enabled subsystem may be of interest to improve run time if that becomes an issue.

### Categories

Find more on Subsystems in Help Center and File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!