Markovian Jump Linear System in Matlab 
Can anyone help me with the code to simulate a Markovian jump linear system? I could not figure out how to write code for a different state matrices $A_i$, $B_i$ and $C_i$. 

I would appreciate any help. Thank you in advance.
 A: Let's describe a fairly general Markov jump linear system (MJLS).
Let $A_{1},\ldots,A_{n}$, $B_{1},\ldots,B_{n}$, and $C_{1},\ldots,C_{n}$ be given matrices.
Let $(r_{n})_{n\geq0}$ be a Markov chain with transition matrix $P$.
Given $x_0$, define processes $(x_{n})_{n\geq 1}$ and $(y_{n})_{n \geq 1}$ by
\begin{align*}
x_{n+1} & =A_{r_{n}}x_{n}+B_{r_{n}}u_{n}+w_{n}\\
y_{n+1} & =C_{r_{n}}x_{n}+v_{n}.
\end{align*}
In the above, $(w_{n})_{n\geq1}$ is a sequences of $\mathcal{N}(0,R_{w})$  random variables where $R_w$ is some given covariance matrix.
$(v_{n})_{n\geq1}$ is defined similarly, with corresponding covariance matrix $R_v$.
Since this system is Markovian, you can (without loss of generality) take the control $u_{n}$ to be of the form $u_{n}\equiv U(r_{n}, x_{n},y_{n})$ where $U$ is some given function.
Remark: It is understood that $\{r_{0},r_{1},w_{1},v_{1},r_{2},w_{2},v_{2},\ldots\}$ is a mutually independent set of random variables.
To implement this in MATLAB, you will need to write a function with signature
function [x, y] = mjls_simulate(P, A, B, C, R_w, R_v, U, r_0, x_0, N)

where the parameter N corresponds to the number of steps you would like to simulate for.
You should look into the following functions:


*

*simulate: for simulating Markov chains

*mvnrnd: for generating samples from a multivariate random normal distribution


You can represent the parameter U as an anonymous function in MATLAB.
