2 Creating and Exporting Mathematical Models in Maple - MapleSim Connector Help

# Online Help

###### All Products    Maple    MapleSim

Home : Support : Online Help : MapleSim Toolboxes : MapleSim Connector : Getting Started Guide : MapleSimConnectorGSG/Chapter02

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.

 > $\mathrm{with}\left(\mathrm{DynamicSystems}\right):$
 > $\mathrm{with}\left(\mathrm{MapleSimConnector}\right):$

To create a system object from the transfer function $\frac{1}{{s}^{2}\mathit{+}a\mathit{\cdot }s\mathit{+}b}$,  use the following command:

 > $\mathrm{sys}≔\mathrm{TransferFunction}\left(\frac{1}{{s}^{2}+a\cdot s+b}\right)$
 ${\mathrm{sys}}{:=}\left[\begin{array}{c}{\mathbf{Transfer Function}}\\ {\mathrm{continuous}}\\ {\mathrm{1 output\left(s\right); 1 input\left(s\right)}}\\ {\mathrm{inputvariable =}}\left[{\mathrm{u1}}{}\left({s}\right)\right]\\ {\mathrm{outputvariable =}}\left[{\mathrm{y1}}{}\left({s}\right)\right]\end{array}\right$ (1.1.1)

To view the details of the system, use the PrintSystem command.

 > $\mathrm{PrintSystem}\left(\mathrm{sys}\right)$
 $\left[\begin{array}{c}{\mathbf{Transfer Function}}\\ {\mathrm{continuous}}\\ {\mathrm{1 output\left(s\right); 1 input\left(s\right)}}\\ {\mathrm{inputvariable =}}\left[{\mathrm{u1}}{}\left({s}\right)\right]\\ {\mathrm{outputvariable =}}\left[{\mathrm{y1}}{}\left({s}\right)\right]\\ {{\mathrm{tf}}}_{{1}{,}{1}}{\mathrm{=}}\frac{{1}}{{{s}}^{{2}}{+}{a}{s}{+}{b}}\end{array}\right$ (1.1.2)

The default values for the input names ($\mathrm{u1})$ and output names 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:

 > $\mathrm{par}≔\left[a=1,b=1\right]$
 ${\mathrm{par}}{:=}\left[{a}{=}{1}{,}{b}{=}{1}\right]$ (1.1.3)

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.

 >
 > $\mathrm{SaveCode}\left("MyTransferFunction",\mathrm{extension}="c",\mathrm{script}\left[1\right],\mathrm{interactive}=\mathrm{true}\right):$

 > $\mathrm{SaveCode}\left("MyTransferFunction",\mathrm{extension}="m",\mathrm{script}\left[2\right],\mathrm{interactive}=\mathrm{true}\right):\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}$

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:

 >
 > $\mathrm{eq2}≔J\left(\frac{{ⅆ}^{2}}{ⅆ{t}^{2}}\mathrm{\theta }\left(t\right)\right)+b\left(\frac{ⅆ}{ⅆt}\mathrm{\theta }\left(t\right)\right)+\mathrm{Ks}\mathrm{\theta }\left(t\right)=Ki\left(t\right):$

Transfer Function Model:

 >
 $\frac{{K}}{{J}{L}{{s}}^{{3}}{+}\left({b}{L}{+}{J}{R}\right){{s}}^{{2}}{+}\left({\mathrm{Ks}}{L}{+}{{K}}^{{2}}{+}{b}{R}\right){s}{+}{\mathrm{Ks}}{R}}$
 $\frac{{J}{{s}}^{{2}}{+}{b}{s}{+}{\mathrm{Ks}}}{{J}{L}{{s}}^{{3}}{+}\left({b}{L}{+}{J}{R}\right){{s}}^{{2}}{+}\left({\mathrm{Ks}}{L}{+}{{K}}^{{2}}{+}{b}{R}\right){s}{+}{\mathrm{Ks}}{R}}$ (1.2.1)

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 $\mathrm{θ}=0$ $⟦\mathrm{rad}⟧$ Motor current $i=0$ $⟦A⟧$ Parameters Moment of inertia of the motor $J=0.1$ $⟦\mathrm{kg}\cdot {m}^{2}⟧$ Damping of the mechanical system $b=0.1$ $⟦N\cdot m\cdot s⟧$ Electromotive force constant $K=0.1$ $⟦\frac{N\cdot m}{A}⟧$ Motor coil resistance $R=1$ $⟦\mathrm{Ω}⟧$ Motor coil inductance $L=0.5$ $⟦H⟧$ External Spring Load Constant $\mathrm{Ks}=0$ $⟦N\cdot m⟧$

 >
 >

3. Generate and save the source code as a .c file and MATLAB® .m file.

 >
 > $\mathrm{MapleSimConnector}:-\mathrm{SaveCode}\left("MyTransferFunction",\mathrm{cSFcn},\mathrm{extension}="c",\mathrm{interactive}=\mathrm{true}\right):$
 > $\mathrm{MapleSimConnector}:-\mathrm{SaveCode}\left("MyTransferFunction",\mathrm{MBlock},\mathrm{extension}="m",\mathrm{interactive}=\mathrm{true}\right):$

With the basic tools shown in this guide, you are now ready to use the MapleSim Connector to solve many system design problems. Enter ?DynamicSystems and ?MapleSimConnector at a prompt in a Maple worksheet for more information about the commands used in this guide.



## Was this information helpful?

 Please add your Comment (Optional) E-mail Address (Optional) What is ? This question helps us to combat spam