Main Content

Deploy Shallow Neural Network Simulink Diagrams

The gensim function generates block descriptions of networks that you can simulate using Simulink®.

gensim(net,st)

The second argument to gensim determines the sample time, which is normally chosen to be some positive real value.

If a network has no delays associated with its input weights or layer weights, you can set this value to -1. A value of -1 causes gensim to generate a network with continuous sampling.

Example

Define a set of inputs p and the corresponding targets t.

p = [1 2 3 4 5];
t = [1 3 5 7 9];

Design a linear layer to solve this problem.

net = newlind(p,t)

Test the network on your original inputs with sim.

y = sim(net,p)

The results show that the network has solved the problem.

y =
      1.0000    3.0000    5.0000    7.0000    9.0000

Call gensim to generate a Simulink version of the network.

gensim(net,-1)

The second argument is -1, so the resulting network block samples continuously.

The call to gensim opens this model in the Simulink Editor, that shows a system consisting of the linear network connected to a sample input and a scope.

To test the network, double-click the input Constant x1 block on the left.

The input block is actually a standard Constant block. Change the constant value from the initial randomly generated value to 2, and then click OK.

Select the menu option Simulation > Run.

When the simulation is complete, double-click the output y1 block on the right to see the network response.

The output is equal to 3, which is the correct output for an input of 2.

Suggested Exercises

Here are a couple exercises you can try.

Change the Input Signal

Replace the constant input block with a signal generator from the standard Simulink Sources blockset. Simulate the system and view the network response.

Use a Discrete Sample Time

Recreate the network, but with a discrete sample time of 0.5, instead of continuous sampling.

gensim(net,0.5)

Again, replace the constant input with a signal generator. Simulate the system and view the network response.

Generate Functions and Objects

For information on simulating and deploying shallow neural networks with MATLAB® functions, see Deploy Shallow Neural Network Functions.