Contents Previous Next Index
2 Creating and Exporting Mathematical Models in Maple
In Maple, you can use commands from the DynamicSystems package to create a system from first principles. Maple contains a data structure called a system object that encapsulates the properties of a dynamic system. This data structure contains information, for example, the description of the system, and the description of the inputs. Five different types of systems can be created.
Differential equation or difference equation
Transfer function as an expression
Transfer function as a list of numerator and denominator coefficients
State-space
Zero, pole, gain
You can create a DynamicSystems object in a new worksheet and use commands from the MapleSimConnector package to generate source code programmatically and save it as a MATLAB® .m file.
2.1 Creating and Exporting a DynamicSystems Object Programmatically
First, load the DynamicSystems and MapleSimConnector packages in the Maple worksheet.
withDynamicSystems:
withMapleSimConnector:
To create a system object from the transfer function 1s2+a⋅s+b, use the following command:
sys≔TransferFunction1s2+a⋅s+b
sys:=Transfer Functioncontinuous1 output(s); 1 input(s)inputvariable = u1⁡soutputvariable = y1⁡s
To view the details of the system, use the PrintSystem command.
PrintSystemsys
Transfer Functioncontinuous1 output(s); 1 input(s)inputvariable = u1⁡soutputvariable = y1⁡stf1,1 = 1s2+a⁢s+b
The default values for the input names (u1) and output names y1 have been used. Alternatively, during creation of the system, different input and output names can be specified.
To define parameters values, use the following command:
par≔a=1,b=1
par:=a=1,b=1
Finally, use the SBlock command to generate the source code and the SaveCode command to save the code as a .c file and MATLAB® .m file.
script≔SBlocksys,sys:-inputvariable,sys:-outputvariable,MyTransferFunction, parameters=par:
SaveCodeMyTransferFunction,extension=c,script1,interactive=true:
SaveCodeMyTransferFunction,extension=m,script2,interactive=true:
2.2 Example: DC Motor
Consider the classic example of the simplified DC motor. Using the built-in functionality of the DynamicSystems package in Maple, you can define the system model, and then visualize and simulate it before saving the code.
This example demonstrates how to define, analyze, and export a system programmatically.
To define, visualize and simulate a DC motor:
1. In a new Maple worksheet, define the system model.
Differential Equation Model:
eq1≔L ⅆⅆt⁢i⁡t+R⁢it=vt−K⁢ⅆⅆt⁢θ⁡t:
eq2≔J⁢ⅆ2ⅆt2⁢θ⁡t+b⁢ⅆⅆt⁢θ⁡t+Ks⁢θ⁡t=K⁢i⁡t:
Transfer Function Model:
sys_de ≔ DynamicSystems:-DiffEquation eq1,eq2, vt,θt,it :sys_tf ≔ DynamicSystems:-TransferFunctionsys_de: sys_tf:-tf1,1;sys_tf:-tf2,1;
KJ⁢L⁢s3+b⁢L+J⁢R⁢s2+Ks⁢L+K2+b⁢R⁢s+Ks⁢R
J⁢s2+b⁢s+KsJ⁢L⁢s3+b⁢L+J⁢R⁢s2+Ks⁢L+K2+b⁢R⁢s+Ks⁢R
In place of the above commands, you could use the PrintSystem command to display each part of the model.
2. Specify the parameters in the model.
Description
(Initial) Value
Units
Input Variables
Applied voltage
v=0
V
Output Variables
Motor shaft angular position
θ=0
rad
Motor current
i=0
A
Parameters
Moment of inertia of the motor
J=0.1
kg⋅m2
Damping of the mechanical system
b=0.1
N⋅m⋅s
Electromotive force constant
K=0.1
N⋅mA
Motor coil resistance
R=1
Ω
Motor coil inductance
L=0.5
H
External Spring Load Constant
Ks=0
N⋅m
params≔ J=0.1,b=0.1, K=0.1, R=1,L=0.5,Ks=0 :
ics ≔ i0=0, θ0=0, Dθ0=0:
3. Generate and save the source code as a .c file and MATLAB® .m file.
cSFcn, MBlock≔MapleSimConnector:-SBlocksys_tf,sys_tf:-inputvariable, sys_tf:-outputvariable, MyTransferFunction, parameters=params, initialconditions=ics :
MapleSimConnector:-SaveCodeMyTransferFunction,cSFcn,extension=c,interactive=true:
MapleSimConnector:-SaveCodeMyTransferFunction,MBlock,extension=m,interactive=true:
With the basic tools shown in this guide, you are now ready to use the MapleSim Connector to solve many system design problems. For more information about the commands used in this guide, enter DynamicSystems and MapleSimConnector in the Maple help system.
Download Help Document