System Identification in MATLAB - matlab

In Matlab I have created a single-output multiple-input fitted model as an idpoly object. This is by using the system identification toolbox and the model is a Box-Jenkins or Transfer Function model. I now want to see what the model does if I put in different inputs (same amount) and no output (model should estimate the output given the input). Yet I did not find a method in Matlab that can do so.
Is there any way I can use an idpoly object (model) in Matlab and use only input to obtain an output? I have tried the command "sim" but it does not do the job.

Use the command "forecast" and specify the input values for the forecast.

Related

Set initial values in simulink idmodel block for an identified process model

I used system identification tool to obtain a state space (order 2) and a process model (2 inputs, 1 output, 2nd order transfer function + delay each) of some data. The models show very nice fit to experimental data in the system identification model output window [Figure 1] but when I use the idmodel block in simulink to simulate the same data it does not look at all like it was on the tool.
I have used exactly the same block diagram with both fitted models. With the state-space fitted model the results are coherent with the system identification tool [Figures 2-3], meanwhile the process model it's totally different [Figures 4-5].
The state-space model only works well with certain experimental conditions, while the process model gives a good fit for all my experiments, that's the reason why I try to use it. The pictures below correspond to only 1 experiment.
I think the problem resides in that I can't set initial conditions to the idmodel block when using a process model instead of a state space model. I can get the initial conditions for the process model using findstates(model, data), but I don't know how to apply them. Any hints on how to set initial conditions for identified transfer function/process models in simulink? Maybe a possible workaround without simulink? I'm open to any solution or ideas.
Thank you.
Figure 1. System identification tool output for both state-space and process model
Figure 2. Simulink output of state-space model
Figure 3. idmodel block with state-space model: initial conditions parameter available
Figure 4. idmodel block with process model: initial conditions parameter NOT available
Figure 5. Simulink output of state-space model
After reaching matlab central and pointing me in the right direction I came up with a solution. Thanks to Rajiv Singh.
I first needed to convert the process model from idproc to idss using idss() instead of ss() -See this article-, then use compare() instead of findstates() to obtain the initial conditions and feed the initial conditions to the idmodel block in simulink. Graphically:
model=idss(T3s_2d);
[y,fit,x0]=compare(run_data_s{8}, model);
%T3s_2d is the identified process model (idproc) from system identification toolbox
%run_data_s is the iddata object with the experimental runs

How to load non timeseries data into simulink for use in a deep neural network

I trained a neural network in python and want to load the weights into simulink to pass into a function that will build the network. I thought I could use from workspace but it seems it does not handle non time series data for a structure. I get the error.
Invalid structure-format variable specified as workspace input in 'PMSM_FCS_MPC/From Workspace'. If
the input signal is a bus signal, the variable must be a structure of MATLAB timeseries objects.
Otherwise, the variable must include 'time' and 'signals' fields, and the 'signals' field must be a
structure with a 'values' field.
How can I pass a bunch of arrays into a simulink function?
Everything I have seen is about time series data. Nothing on a group of matrices such as would be used in a deep neural network.
The From Workspace block is designed to work with time-series data (as per the error message you show.) There is no need to use it if you have constant data.
If you have non-time series data and you need it as a Simulink signal then use the name of the MATLAB variable as the parameter in a Constant block. The signal coming out of the block will have the value of your data.
If you are using a MATLAB Function block then you can also input the data as a parameter argument to the function. See Add Parameter Arguments for the steps to do this.
Not sure how you want to use the data. But if you have those weights as an array in the main workspace, you can simply reference to that variable in Simulink. For example if you have K = [1 2] in your workspace, you can use any block and type in K(1) or K(2). You can do also matrix operations and gather signals with Mux block.

How to iterate over values of models in Simulink Matlab?

I have designed a model in simulink. Generally, I generate a plot by setting the values of blocks(eg.gain) in the model and simulating the model and opening the scope block. But I need to generate different grpahs corresponding to different values of blocks(eg.gain). Basically, for different values of Gain value, I want different graphs but all in the same plot. The different values I give to my gain should be from an array. This is my model
I am using MATLAB for the first time. Please answer this in a beginner's approach
Setting The Gain Value
The values of the gain blocks can be can be set as variables rather than constants i.e. you can give a gain block the value of K in the settings panel.
You can then create a script that gives K a value e.g;
%script to set gain and run model
K=2;
sim('Model Name Here');
This will set the value for your gain block and run the model.
Saving The Output
In the sinks section of the simulink library browser is a block called To Workspace, this allows you to send any output value to the MATLAB workspace in multiple formats with a name that you define.
Your simulink model will now look something like this;
Now you can create a script that sets a gain value for your model, runs the model and saves the output to your workspace. With a couple of for loops and you can produce an array of inputs and outputs for your system.
From here you should be able to plot the inputs and outputs on the same graph using the well documented plot function.

How do I integrate an MPC, PID and System models together to simulate on Matlab

I have a system with a Model Predictive Controller and PID Controller.
Assuming I have models for each controller and can express them in discrete time, please how do I integrate them together to simulate properties of the system in matlab?
Thanks
... continuing from the comments.
This is what Simulink is made for. Of course there are ways to do it without Simulink, but often you still use Simulink tools and functions just without the graphical Interface.
I assume you have your transfer functions "on paper". So you need the tf function to define your system model.
G = tf(num,den)
num and den are the coefficient vectors of your transfer function of numerator and denumerator. In Simulink you use the Transfer Fcn block and you define it with
G.num{1} %Numerator coefficients
G.den{1} %Denumerator coefficients
Your PID-controller cannot be defined using this block, as Simulink requires a higher or equal order for the denumerator. Instead use the PID controller Block. You need to calculate the Proportional, Integral and Differential gain before.
Then read the documentation about the MPC toolbox - I'm not familiar with it and can't help you on that - it is explained how you can create an mpc object regarding all your constraints (see your other question).
Then you have various options to transform your mpc object into something Simulink can deal with. I'd recommend the ss - the state space model - which can be implemented using the state space block. There is also a MPC Controller block, I don't have the toolbox - but you'll be able to find out how it can be used.
Finally you find source blocks, like a step to generate a test signal. And there are Sinks, in the easiest case scope to display your results. You can also save them to workspace or whatever...

Dense output of SimMechanics?

I have set up a model using SimMechanics. It outputs data at the times where the solver steps to. Is there any possibility to have some kind of dense output such that it is possible to interpolate these data to get the solution at arbitrary points without losing the high order of the integrator?
In Matlab this is easily possible using the function deval after the integration of one of the built-in ODE integrators.
In SimMechanics I can select these integrators, too. Is there some kind of analouge way to deval?
Yes, it's possible, although it's a Simulink functionality, not specific to SimMechanics. In the Configuration Parameters of the model, you can set the model to Produce Specified Output Only (see http://www.mathworks.co.uk/help/simulink/gui/data-import-export-pane.html#bq9_fhw-1), under Data Import/Export. This way, only the outputs you specify will be produced regardless of the time steps taken by the solver.