State space system formation from SISO transfer functions - matlab

I want to form a MIMO state-space system from SIMO Transfer functions. Lets say that the system has 2 inputs (U1 and U2) and 2 states (X1 and X2).
If I apply U1 to the LTI system, I get X1 and X2 in a SIMO operation. And, I can extract 2 transfer functions: T11 (X1/U1) and T21 (X2/U1).
Similarly, if I apply U2 input, I can get T12 (X1/U2) and T22 (X2/U2).
So, I have 4 SISO transfer functions of the system.
I want to use them to generate a state space matrix of the system. How can I do that?
Thanks in advance.

In general, there are infinitely many choices for state-space representations of your matrix transfer function (T). It is sensible to choose the one with lowest order (smallest number of states) often called the "minimal realization."
There are many approaches to computing the minimal realization. Some are algorithmic starting with T and arriving at the minimal A*,B*,C*,D* immediately. Others suppose that you already found some non-minimal A,B,C,D by inspection, and then provide the procedure for transforming that non-minimal representation into the minimal one.Typically it is a matrix transformation of A and B into some canonical form that exposes uncontrollable (sometimes called "unreachable") states.
http://www.egr.msu.edu/classes/me851/jchoi/lecture/Lect_20.pdf
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-241j-dynamic-systems-and-control-spring-2011/readings/MIT6_241JS11_chap25.pdf
https://www.youtube.com/watch?v=cnbY2AUtGAY&t=2m14s
If you are less concerned with manual implementation, in MatLab use the function tf2ss.

Related

Application of tf function for large systems

I'm looking at the transfer functions (transfer matrix) of a Multiple-input single-output (MISO) system. The system has 32 dynamic states, four inputs, and one output. The system A, B, C, and matrices are calculated in a Matlab code, and the state space model is created as sys=ss(A,B,C,D).
My question is that why are the transfer functions obtained by applying the "tf" function on "sys" (a 1*4 structure) different from those obtained by applying the "tf" function on individual system models "sys(1)", "sys(2)", "sys(3)", and "sys(4)", whereas the system matrices obtained by individuals "sys(1)" to "sys(4) completely match with the corresponding matrices and matrices' columns of the "sys"?
I tried the same thing for a simple 4th-order system, and they completely match. I also tried it for a 32th-state system (of same dimension of my original system), where all system matrices are generated by randn function. Then, I tried to find the transfer function coefficients by using cell2mat(T.den) and cell2mat(T.num) for sys and for sys(1) to sys(4). All the denominator coefficients match. Also, except for one of the transfer functions, the numerator coefficients match as well.
It should be mentioned that in the original example, the matrix A is singular, but in the synthetic example 2 (of dimension 32), the condition number of system matrix is around 120.
You can find the code below.
Your help is highly appreciated.
clear all;
clc;
%% Building the system matrices
A=randn(32,32);
B=randn(32,4);
C=randn(1,32);
D=randn(1,4);
sys=ss(A,B,C,D); % creating the state space model
TFF=tf(sys); % calculating the tranfer matrix
%% extracting the numerator and denominator coefficients of 4 transfer
%functions
for i=1:4
Ti=TFF(i);
Tin(i,:)=cell2mat(Ti.num); % numerator coefficients
Tid(i,:)=cell2mat(Ti.den); % denominator coefficients
clear Ti
end
%% calculatingthe numerator and denominator coefficients based on individual
%transfer functions
TF1=tf(sys(1));
T1n=cell2mat(TF1.num);
T1d=cell2mat(TF1.den);
TF2=tf(sys(2));
T2n=cell2mat(TF2.num);
T2d=cell2mat(TF2.den);
TF3=tf(sys(3));
T3n=cell2mat(TF3.num);
T3d=cell2mat(TF3.den);
TF4=tf(sys(4));
T4n=cell2mat(TF4.num);
T4d=cell2mat(TF4.den);
num2str([T1n.'-Tin(1,:).']) % the error between the numerator coefficients
% of the TF1 by 2 aproaches
num2str([T2n.'-Tin(2,:).'])
num2str([T3n.'-Tin(3,:).'])
num2str([T4n.'-Tin(4,:).'])
num2str([T1d.'-Tid(1,:).'])
num2str([T2d.'-Tid(2,:).'])
num2str([T3d.'-Tid(3,:).'])
num2str([T4d.'-Tid(4,:).'])
It is a mixture of a few things; because the resulting model is not guaranteed to be minimal per se after model switches you get some discrepancies but also a subsystem of the MIMO system is not guaranteed to agree with a SISO part of the system which can remove some of the poles of the other modes if input is not acting on them.
However, beyond 5,6 transfer matrices are numerically terrible to perform operations with. Hence try to avoid them. Check other properties of the subsystems such as Bode plots for comparison

Matlab: Solving a logarithmic equation

I have the following equation that I want to solve with respect to a:
x = (a-b-c+d)/log((a-b)/(c-d))
where x, b, c, and d are known. I used Wolfram Alpha to solve the equation, and the result is:
a = b-x*W(-((c-d)*exp(d/x-c/x))/x)
where W is the is the product log function (Lambert W function). It might be easier to see it at the Wolfram Alpha page.
I used the Matlab's built-in lambertW function to solve the equation. This is rather slow, and is the bottleneck in my script. Is there another, quicker, way to do this? It doesn't have to be accurate down to the 10th decimal place.
EDIT:
I had no idea that this equation is so hard to solve. Here is a picture illustrating my problem. The temperatures b-d plus LMTD varies in each time step, but are known. Heat is transferred from red line (CO2) to blue line (water). I need to find temperature "a". I didn't know that this was so hard to calculate! :P
Another option is based on the simpler Wright ω function:
a = b - x.*wrightOmega(log(-(c-d)./x) - (c-d)./x);
provided that d ~= c + x.*wrightOmega(log(-(c-d)./x) - (c-d)./x) (i.e., d ~= c+b-a, x is 0/0 in this case). This is equivalent to the principal branch of the Lambert W function, W0, which I think is the solution branch you want.
Just as with lambertW, there's a wrightOmega function in the Symbolic Math toolbox. Unfortunately, this will probably also be slow for a large number of inputs. However, you can use my wrightOmegaq on GitHub for complex-valued floating-point (double- or single-precison) inputs. The function is more accurate, fully-vectorized, and can be three to four orders of magnitude faster than using the built-in wrightOmega for floating-point inputs.
For those interested, wrightOmegaq is based on this excellent paper:
Piers W. Lawrence, Robert M. Corless, and David J. Jeffrey, "Algorithm 917: Complex Double-Precision Evaluation of the Wright omega Function," ACM Transactions on Mathematical Software, Vol. 38, No. 3, Article 20, pp. 1-17, Apr. 2012.
This algorithm goes beyond the cubic convergence of the Halley's method used in Cleve Moler's Lambert_W and uses a root-finding method with fourth-order convergence (Fritsch, Shafer, & Crowley, 1973) to converge in no more than two iterations.
Also, to further speed up Moler's Lambert_W using series expansions, see my answer at Math.StackExchange.
Two (combinable) options:
Is your script already vectorized? Evaluate the function for more than a single argument. Executing for i = 1:100, a(i)=lambertw(rhs(i)); end is slower than a=lambertw(rhs).
If you are dealing with the real valued branch of LambertW (i.e. your arguments are in the interval [-1/e, inf) ), you can use the implementation of Lambert_W submitted by Cleve Moler on the File Exchange.
Do you know the mass flow rates at both sides of the heat exchanger at each time-step?
If yes, temperature 'a' can be solved by the 'effectiveness-NTU' approach which does not need any iteration, rather than the LMTD approach. Reference: e.g. http://ceng.tu.edu.iq/ched/images/lectures/chem-lec/st3/c2/Lec23.pdf

Multivariate Hidden Markov Model

How can I combine multiple emmision spectra about the same markov states?
Let's use the classical HMM example:
% states
S = {sunny, rainy, foggy}
% discrete observations
x = {umbrella, no umbrella}
Now what if I had multiple observation sequences. E.g.:
% sequence 1
x1 = {umbrella, no umbrella}
% sequence 2
x2 = {wearing a coat, not wearing a coat}
How can I combine these two observation sequences into one HMM?
Note: I would like a way to combine x1 and x2 such that their inter-dependencies are also modelled. Therefore simply saying x={x1 x2} would (IMO) not be a good solution.
Specifically, I want to train a HMM based on Matlab's hmmtrain:
[ESTTR,ESTEMIT] = hmmtrain(seq,TRGUESS,EMITGUESS)
This only allows me to insert one seq.
Now let's say I have 5 different emmision spectra which all say something about the states of the HMM. How can I handle this multivariate case?
How about taking the Cartesian product of the possible observations from each set. That is, your new discrete emission model will be:
umbrella and wearing-a-coat
umbrella and not-wearing-a-coat
no-umbrella and wearing-a-coat
no-umbrella and not-wearing-a-coat
What about creating preconditions to choose a special HMM? Instead of a huge HMM you can create several small HMMs und you choose only the relevant HMM. For example: if (umbrella=true) then apply HMM_1 else apply HMM_2. Then, you also have fewer emission symbols in a HMM. Nice side effect: You save training and testing time.

local inverse of a neural network

I have a neural network with N input nodes and N output nodes, and possibly multiple hidden layers and recurrences in it but let's forget about those first. The goal of the neural network is to learn an N-dimensional variable Y*, given N-dimensional value X. Let's say the output of the neural network is Y, which should be close to Y* after learning. My question is: is it possible to get the inverse of the neural network for the output Y*? That is, how do I get the value X* that would yield Y* when put in the neural network? (or something close to it)
A major part of the problem is that N is very large, typically in the order of 10000 or 100000, but if anyone knows how to solve this for small networks with no recurrences or hidden layers that might already be helpful. Thank you.
If you can choose the neural network such that the number of nodes in each layer is the same, and the weight matrix is non-singular, and the transfer function is invertible (e.g. leaky relu), then the function will be invertible.
This kind of neural network is simply a composition of matrix multiplication, addition of bias and transfer function. To invert, you'll just need to apply the inverse of each operation in the reverse order. I.e. take the output, apply the inverse transfer function, multiply it by the inverse of the last weight matrix, minus the bias, apply the inverse transfer function, multiply it by the inverse of the second to last weight matrix, and so on and so forth.
This is a task that maybe can be solved with autoencoders. You also might be interested in generative models like Restricted Boltzmann Machines (RBMs) that can be stacked to form Deep Belief Networks (DBNs). RBMs build an internal model h of the data v that can be used to reconstruct v. In DBNs, h of the first layer will be v of the second layer and so on.
zenna is right.
If you are using bijective (invertible) activation functions you can invert layer by layer, subtract the bias and take the pseudoinverse (if you have the same number of neurons per every layer this is also the exact inverse, under some mild regularity conditions).
To repeat the conditions: dim(X)==dim(Y)==dim(layer_i), det(Wi) not = 0
An example:
Y = tanh( W2*tanh( W1*X + b1 ) + b2 )
X = W1p*( tanh^-1( W2p*(tanh^-1(Y) - b2) ) -b1 ), where W2p and W1p represent the pseudoinverse matrices of W2 and W1 respectively.
The following paper is a case study in inverting a function learned from Neural Networks. It is a case study from the industry and looks a good beginning for understanding how to go about setting up the problem.
An alternate way of approaching the task of getting the desired x that yields desired y would be start with random x (or input as seed), then through gradient decent (similar algorithm to back propagation, difference being that instead of finding derivatives of weights and biases, you find derivatives of x. Also, mini batching is not needed.) repeatedly adjust x until it yields a y that is close to the desired y. This approach has an advantage that it allows an input of a seed (starting x, if not randomly selected). Also, I have a hypothesis that the final x will have some similarity to initial x(seed), which would imply that this algorithm has the ability to transpose, depending on the context of the neural network application.

Matlab non-linear, multi-parameter curve fitting issue

I am trying to implement a routine for fitting electrophoretic data from my experiments.
The aim is to derive kinetic parameters for the interaction of biomoecules from the relative areas of peaks in the electropherogram, based on the areas of the peaks in the dataset.
Since all relevant differential equations are known and since the set of equations has an analytical solution, as described here:
Analytical solution manuscript
I set about entering the relevant equations (6, 8, 13, ... from the referenced manuscript) in matlab.
The thus created function works and I can use it to simulate electropherograms of interacting species.
Obviuously, I now would like to use the function to fit experimental data and retrieve the parameters (8 in total, Va, Vc, MUa, MUc, k, A0, C0, baseline noise).
Some of these will obviously be correlated. Example values might be (to give an idea of their magnitude):
params0 = [ ...
8.44E-02; ... % Va
1.25E-01; ... % Vc
5.32E-05; ... % MUa
8.87E-05; ... % MUc
4.48E-03; ... % k
6.06E-01; ... % A0
3.00E-00; ... % C0
4.64E-03 ... % noise
];
My problem is, if I supply experimental data and try something like lsqcurvefit:
[x,resnorm,residual] = lsqcurvefit(#(param,xdata) Electropherogram2(param,xdata,column), params0, time, ydata,lb, ub);
I often get very poor results because I either run out of iterations, I hit some (obviously poorly fitting) local minimum or whatever...
Only if I tinker a lot with the starting values and the allowed intervals (i.e. because I know likely values through other experiments) do I end up with more or less decent fits, but even then, fits are not as good as reported in the original manuscript (fig. 3).
The authors of that manuscript used Excel solver and were kind enough to provide the original data used in Fig. 3 but still I cannot seem to end up with fits as good as theirs without nearly literally supplying the nearly correct starting values.
I am not experienced enough to know what I could tweak to make this process less trial-and-error.
Would something like the global optimization toolbox help me?
Any tips are welcome...
In the mentioned paper ("Analytical solution manuscript") it is implied that the free optimization parameters are five (Va, Vc, MUa, MUc, k) and not eight because the (Aeq/Ceq) ratio can be computed from their representative equations, eq. 8 for Aeq and (obviously) eq. 6 for Ceq.
In my opinion, what's even more troubling is the appearance of the following products in the model, comprised of the free optimization parameters:
k and Va in eq. 12
MUc and Va in the equation for epsilon_A in eq. 12
MUa and Vc in the equation for epsilon_A in eq. 12
In general, non-linear optimization algorithms have a legitimate trouble in optimizing the free parameters when pairs of the latter appear as products in the non-linear model.