perform a Monte Carlo analysis of a MapleSim model - MapleSim Help

 Calling Sequence MonteCarlo(C, params, options)

Parameters

 C - module; output of GetCompiledProc params - set or list of equations; model parameters to vary options - (optional) equation(s) of the form option = value; specify options for MonteCarlo

Description

 • The MonteCarlo command performs a Monte Carlo analysis of a MapleSim model. A MapleSim model is repeatedly simulated with selected model parameters varied using specified random variables.
 • The C parameter is a module, the output of GetCompiledProc. See the Advanced Usage section in the Examples for further details.
 • The params parameter specifies the model parameters that are varied. It is either a set or list of equations. The left side of each equation is the name of the parameter, the right side is a random variable.
 • By default, the result is a list of records, one for each simulation. If the include_nominal option is set to true, the first record corresponds to the output of the model with nominal parameter values.  Each record has two fields:
 – params: the set of equations specifying the value of each parameter;
 – data: the Matrix whose first column is the time samples of a simulation and whose remaining columns are the values of the outputs of C.

Examples

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

Assign model the filename of the MapleSim model we want to analyze.

 > $\mathrm{model}:=\mathrm{cat}\left(\mathrm{kernelopts}\left('\mathrm{toolboxdir}'=\mathrm{MapleSim}\right),"/data/examples/RLCcircuit.msim"\right):$

 > $A:=\mathrm{MapleSim}:-\mathrm{LinkModel}\left(\mathrm{filename}=\mathrm{model}\right):$

Inspect the available parameters.

 > $A:-\mathrm{GetParameters}\left(\mathrm{allparams}\right)$
 $\left[{C}{=}{1}{,}{L}{=}{1}{,}{\mathrm{R1_T_ref}}{=}\frac{{6003}}{{20}}{,}{\mathrm{R1_alpha}}{=}{0}{,}{R}{=}{1}{,}{\mathrm{S1_amplitude}}{=}{1}{,}{\mathrm{S1_freqHz}}{=}{1}{,}{\mathrm{S1_offset}}{=}{0}{,}{\mathrm{S1_phase}}{=}{0}\right]$ (1)

Use the GetCompiledProc export of A to assign the module that will be passed to MonteCarlo. Assign nominal values for the two model parameters of interest.

 > $\mathrm{C1}:=A:-\mathrm{GetCompiledProc}\left(\mathrm{params}=\left[L=4,C=5\right]\right):$

Assign a simple procedure that generates a random variable uniformly distributed around a nominal value.

 > $\mathrm{rv}:=\left(\mathrm{nom},\mathrm{tol}\right)→\mathrm{RandomVariable}\left(\mathrm{UniformDistribution}\left(\mathrm{nom}\left(1-\mathrm{tol}\right),\mathrm{nom}\left(1+\mathrm{tol}\right)\right)\right):$

Call MonteCarlo, using C1 and assigning random variables for the L and C model parameters. The C_opts option specifies that the final time of the simulation is 0.5 seconds. The include_nominal option means the first simulation corresponds to the given nominal values of the model parameters. The num_sims option specifies that 100 randomized simulations are made.

 > $\mathrm{results}:=\mathrm{MapleSim}:-\mathrm{Analysis}:-\mathrm{MonteCarlo}\left(\mathrm{C1},\left\{L=\mathrm{rv}\left(4,0.1\right),C=\mathrm{rv}\left(5,0.1\right)\right\},\mathrm{C_opts}=\left\{\mathrm{tf}=0.5\right\},\mathrm{include_nominal},\mathrm{num_sims}=100\right):$

Verify that the values of the parameters for the first simulation are the nominal values. The subexpression results[1] accesses the first element of the list results. That element is a record.  The full expression results[1]:-params specifies the params field of that record.  The params field, as seen below, is a set of equations that express the sampled values of the parameters.

 > ${\mathrm{results}}_{1}:-\mathrm{params}$
 $\left\{{C}{=}{5.}{,}{L}{=}{4.}\right\}$ (2)

Display the data for the final two sample times for the first (nominal) simulation. The notation results[1] accesses the first record stored in the list results; results[1]:-data references the data field (a Matrix) in that record; the [-2..-1,..] indices extract a sub-block of the Matrix (the last two rows, all columns), see rtable_indexing.

 > results[1]:-data[-2..-1,..];
 $\left[\begin{array}{cc}{0.402010050251256}& {0.0558329537333880}\\ {0.452261306532663}& {0.0596460995707140}\end{array}\right]$ (3)

The first column is the time, the remaining column(s) corresponds to the output(s) of C1:

 > $\mathrm{C1}:-\mathrm{GetOutputs}\left(\right)$
 $\left[{\mathrm{Vout}}{}\left({t}\right)\right]$ (4)

Plot the output versus time for all records. To do that, generate a set of two-column matrices, the first column being the time, the second being the output of interest. The seq command is used to sequentially access the Matrices in results. The expression rec:-data[..,[1,2]] corresponds to the appropriate sub-block of the Matrix in the data field; in this case, all rows (..) and the first and second columns, ([1,2]). Because each Matrix is already two-columns, we could have used the simpler notation rec:-data, however, if the system has more than one output (probe), we would then use the notation rec:-data[..,[1,k]], where k is the column of interest.

 > plot({seq(rec:-data[..,[1,2]], rec=results)});

Generate a histogram of the final output point by creating a Vector of the data and passing it to Statistics[Histogram]. The expression rec:-data[-1,2] evaluates to the final row (-1), second column, of the data Matrix of a record in the results list.

 > X := Vector([seq(rec:-data[-1,2], rec=results)]):
 > $\mathrm{Histogram}\left(X\right)$

 • While the C parameter is typically the output of GetCompiledProc, it can be created directly, as a module with two exports: ModuleApply and GetParameters. This is useful for performing a MonteCarlo analysis of a function of the result of a simulation.
 • The following example performs a MonteCarlo analysis on the square of the output of the previous example (C1).  It does so by assigning a module with a ModuleApply export that squares the second column of the Matrix returned by C1. The GetParameters export calls C1:-GetParameters.
 > C2 := module() export ModuleApply,GetParameters;    ModuleApply := proc()    local M := C1(_passed);        M[..,2] := map(x->x^2, M[..,2]);        M;    end proc:    GetParameters := C1:-GetParameters: end module:
 • Pass this module to MonteCarlo and plot the results.
 > $\mathrm{results2}:=\mathrm{MapleSim}:-\mathrm{Analysis}:-\mathrm{MonteCarlo}\left(\mathrm{C2},\left\{L=\mathrm{rv}\left(4,0.1\right),C=\mathrm{rv}\left(5,0.1\right)\right\},\mathrm{C_opts}=\left\{\mathrm{tf}=0.5\right\},\mathrm{include_nominal}\right):$
 > plot({seq(rec:-data[..,[1,2]], rec=results2)});