How to insert a pre-generated set of random numbers into a simulink simulation
3 views (last 30 days)
Show older comments
Dear Matlab Experts,
I am curious if this case is possible, and what would be the solution.
- I generate a set of 10 random numbers A, consisting of numbers between 0-100 into the workspace
- I generate another set of 10 random numbers B, consisting of integers between 1 and 2 into the workspace
- I insert the generated random numbers in my simevents model, specificly into an entity server, in the events actions space, where it will use these generated numbers from set A and set B for simulation purposes
- How the entity servers takes the numbers from set A and set B will be one by one (ex. 1st number from set A is 89 and 1st number from set B is 1. and the next loop it takes 2nd number generated from each set) for each loop (a single loop takes 7 time steps, and I have 70 time steps in total, so 10 loops)
Hope my question is clear, but kindly let me know if you need further information regarding my simulation.
Thank you in advance!
0 Comments
Answers (1)
Jaimin
on 2 Sep 2024
As per my understanding you want to generate two sets of random numbers: "Set A" with 10 numbers between 0-100, and "Set B" with 10 integers between 1-2. These numbers should be used in an entity server within the model, where each loop of 7 timesteps uses corresponding numbers from "Sets A and B" sequentially over 10 loops (totalling 70 time steps).
To accomplish this, we can utilise the "MATLAB Function" Block in Simulink. I've included a sample function to help illustrate the process.
function [outputA, outputB] = getNumbers(loopIndex)
%#codegen
persistent A B
if isempty(A) || isempty(B)
A = evalin('base', 'A'); % Access A from the base workspace
B = evalin('base', 'B'); % Access B from the base workspace
end
% Ensure loopIndex is within bounds
index = mod(loopIndex - 1, 10) + 1;
outputA = A(index);
outputB = B(index);
end
Since we have 70 timesteps and each loop takes 7 timesteps, you can iterate through the indices by incrementing the loop index every 7 steps.
For more information, please refer to this MathWorks documentation.
Implement MATLAB Functions in Simulink with MATLAB Function Blocks:
I hope this will be helpful.
See Also
Categories
Find more on Discrete-Event Simulation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!