Model and Validate a System
You model each component within the system structure to represent the physical or functional behavior of that component. You verify the basic component behavior by simulating them using test data.
Open the System Layout
A big-picture view of the whole system layout is useful when modeling individual components. Start by loading the layout model. At the MATLAB® command line, enter:
open_system('system_layout.slx')
Model the Components
A Simulink® model of a component is based on several starting points:
An explicit mathematical relationship between the output and the input of a physical component — You can compute the outputs of the component from the inputs, directly or indirectly, through algebraic computations and integration of differential equations. For example, computation of the water level in a tank given the inflow rate is an explicit relationship. Each Simulink block executes based on the definition of the computations from its inputs to its outputs.
An implicit mathematical relationship between model variables of a physical component — Because variables are interdependent, assigning an input and an output to the component is not straightforward. For example, the voltage at the
+
end of a motor connected in a circuit and the voltage at the-
end have an implicit relationship. To model such a relationship in Simulink, you can either use physical modeling tools such as Simscape™ or model these variables as part of a larger component that allows input/output definition. Sometimes, closer inspection of modeling goals and component definitions helps to define input/output relationships.Data obtained from an actual system — You have measured input/output data from the actual component, but a fully defined mathematical relationship does not exist. Many devices have unmodeled components that fit this description. For example, the heat dissipated by a television. You can use the System Identification Toolbox™ to define the input/output relationship for such a system.
An explicit functional definition — You define the outputs of a functional component from the inputs through algebraic and logical computations. For example, the switching logic of a thermostat. You can model most functional relationships as Simulink blocks and subsystems.
This tutorial models physical and functional components with explicit input/output relationships. In this tutorial, you will:
Use the system equations to create a Simulink model.
Add and connect Simulink blocks in the Simulink Editor. Blocks represent coefficients and variables from the equations.
Build the model for each component separately. The most effective way to build a model of a system is to first consider components independently.
Start by building simple models using approximations of the system. Identify assumptions that can affect the accuracy of your model. Iteratively add detail until the level of complexity satisfies the modeling and accuracy requirements.
Model the Physical Components
Describe the relationships between components, for example, data, energy, and force transfer. Use the system equations to build a graphical model of the system in Simulink.
Some questions to ask before you begin to model a component:
What are the constants for each component? What values do not change unless you change them?
What are the variables for each component? What values change over time?
How many state variables does a component have?
Derive the equations for each component using scientific principles. Many system equations fall into three categories:
For continuous systems, differential equations describe the rate of change for variables with the equations defined for all values of time. For example, a first-order differential equation gives the velocity of a car:
For discrete systems, difference equations describe the rate of change for variables, but the equations are defined only at specific times. For example, the control signal from a discrete proportional-derivative controller:
Equations without derivatives are algebraic equations. For example, an algebraic equation gives the total current in a parallel circuit with two components:
Wheels and Linear Motion. There are two forces that act on a wheel:
Force applied by the motor — The force F acts in the direction of velocity change and is an input to the wheel subsystems.
Drag force — The force Fdrag acts against the direction of velocity change and is a function of velocity.
Acceleration is proportional to the sum of these forces:
Where kdrag is the drag coefficient and m is the mass of the robot. Each wheel carries half of this mass.
Build the wheel model:
In the
system_layout
model, double-click the subsystem namedRight Wheel
to display the empty subsystem.To model velocity and acceleration, add an Integrator block. Leave the initial condition set to
0
. The input of this block is the acceleration Vdot and the output is the velocity V.To model the drag force, add a MATLAB Function block from the User-Defined Functions library. The MATLAB Function block provides a quick way to implement mathematical expressions in your model. To edit the function, double-click the block to open the MATLAB Function Block Editor.
In the MATLAB Function Block Editor, enter the MATLAB® code to calculate the drag force.
function Fdrag=get_fdrag(V,k_drag) Fdrag=k_drag*V*abs(V);
Define arguments for the MATLAB Function block. In the MATLAB Function Block Editor, click Edit Data . Click k_drag, set Scope to Parameter, and click Apply.
Subtract the drag force from the motor force using the Subtract block. Complete the force-acceleration equation using a Gain block with the Gain parameter specified as
1/(m/2)
.To reverse the direction of the MATLAB Function block, select the block. Then, in the Simulink Toolstrip, on the Format tab, click Flip left-right . Connect the blocks.
The dynamics of the two wheels are the same. Make a copy of the subsystem named
Right Wheel
that you just created and paste it in the subsystem namedLeft Wheel
.To view the top level of the model, click Navigate Up To Parent .
Rotational Motion. When the two wheels turn in opposite directions, they move in a circle of radius r, causing rotational motion of the robot. When the wheels turn in the same direction, there is no rotation. Assuming that the wheel velocities are always equal in magnitude, you can model rotational motion as dependent on the difference of the two wheel velocities VR and VL:
Build the rotation dynamics model:
In the top level of the
system_layout
model, double-click the subsystem namedRotation
to display the empty subsystem. Delete the connection between the Inport and the Outport blocks.To model angular speed and position, add an Integrator block. Leave the initial condition set to
0
. The output of this block is the rotational position theta and the input is the angular speed theta_dot.Compute angular speed from tangential speed. Add a Gain block and set the Gain parameter for the block to
1/(2*r)
.Connect the blocks.
To view the top level of the model, click Navigate Up To Parent .
Model the Functional Components
Describe the function from the input of a function to its output. This description can include algebraic equations and logical constructs, which you can use to build a graphical model of the system in Simulink.
Coordinate Transformation. The velocity of the robot in the x and y coordinates, VX and VY, is related to the linear speed VN and the angle theta:
Build the coordinate transformation model:
In the top level of the
system_layout
model, double-click the subsystem namedCoordinate Transform
to display the empty subsystem.To model trigonometric functions, add a SinCos block from the Math Operations library.
To model multiplication, add two Product blocks from the Math Operations library.
Connect the blocks.
To view the top level of the model, click Navigate Up To Parent .
Set Model Parameters
You can determine appropriate values for parameters in your model using several sources, including:
Written specifications such as standard property tables or manufacturer data sheets
Direct measurements on an existing system
Estimations using system input/output
This model uses these parameters:
Parameter | Symbol | Value |
---|---|---|
Mass | m | 2.5 kg |
Rolling resistance | k_drag | 30 Ns2/m |
Robot radius | r | 0.15 m |
A Simulink model can access parameter values defined using variables in the MATLAB workspace. Define these variables by entering the commands in the MATLAB Command Window.
m = 2.5; k_drag = 30; r = 0.15;
Validate Components Using Simulation
Validate components by supplying an input and observing the output. Even such a simple validation can point out immediate ways to improve the model. This example validates these behaviors:
When a force is applied continuously to a wheel, the velocity increases until it reaches a steady-state velocity.
When the wheels turn in opposite directions, the rotation angle increases at a constant rate.
Validate Wheel Component
Create and run a test model for the wheel component:
Create a new model. In the Simulation tab, click New . Copy the Subsystem block named
Right Wheel
into the new model.To create a test input, add a Step block from the Sources library and connect it to the input of the subsystem named
Right Wheel
. Leave the Step time parameter set to1
.Add a scope viewer to the output. Right-click the output port of the subsystem block named
Right Wheel
and select Create & Connect Viewer > Simulink > Scope.Simulate the model. In the Simulation tab, click Run .
The simulation results show that the model exhibits the general expected behavior. No motion occurs until force is applied at the step time. When force is applied, the speed starts to increase and then settles at a constant when the applied force and the drag force reach an equilibrium. In addition to validation, this simulation also shows the maximum speed of the wheel for the given force.
Validate Rotation Component
Create and run a test model for the rotation model:
To create a new model, click New . Copy the subsystem block named
Rotation
into the new model.To create a test input in the new model, add a Step block from the Sources library. Leave the Step time parameter set to
1
. Connect the output of the Step block to the input of the subsystem namedRotation
. This input represents the difference of the wheel velocities when the wheels rotate in opposite directions.Add a scope viewer to the output of the subsystem named
Rotation
. Right-click the output port of the subsystem and select Create & Connect Viewer > Simulink > Scope.Simulate the model. In the Simulation tab, click Run .
This simulation shows that the angle increases steadily when the wheels are turning with the same speed in opposite directions. You improve the model to make the angle output easier to interpret. For example, you can:
Convert the output signal units from radians to degrees by adding a Gain block with a gain of
180/pi
.Display the output value in cycles of 360 degrees by adding a Math Function block with function
mod
.
MATLAB trigonometric functions take inputs in radians.
Validate the Model
After you validate individual components, you can perform a similar validation on the complete model. This example validates these behaviors:
When the same force is applied to both wheels in the same direction, the robot moves in a line.
When the same force is applied to both wheels in opposite directions, the robot rotates in place.
In the
system_layout
model, double-click the subsystem namedInputs
to display the empty subsystem.Create a test input by adding a Step block. Leave the Step time parameter set to
1
. Connect the Step block output to both Outport blocks.At the top level of the model, connect both output signals to the same scope viewer.
Simulate the model.
The yellow line in the scope viewer is the X direction, and the blue line is the Y direction. Since the angle is zero and is not changing, the vehicle moves only in the X direction, as expected.
Double-click the subsystem named
Inputs
. To reverse the direction for the left wheel, add a Gain block between the source and the second output and set the Gain parameter to-1
.Add a scope viewer to the angle output.
Simulate the model.
The scope viewer connected to the x and y speed signals shows that no motion occurs in the X-Y plane.
The scope viewer connected to the angle signal shows steady angular motion.
You can use this final model to answer many questions about the model by changing the input.
What happens when the initial angle is not zero?
How long does it take for the motion to stop when the force drops to zero?
What happens when the robot is heavier?
What happens when the robot moves on a smoother surface with a smaller drag coefficient?