# Simulation of First Order System (Matlab & Simulink)

Yesterday, I tried to find constants $k_1$ and $k_2$ for the following system:

$$\frac{\mathrm d RH}{\mathrm dt}=k_1+k_2(RH_{ext}-RH).$$

Now, I have some first computations for $k_1$ and $k_2$ and would like to test how good a simulation with these parameters works. I think I could also write this as state space model:

$$\dot{x}=-k_2x+\left[\begin{matrix} k_1 & k_2\end{matrix}\right]\left[ \begin{matrix} u_1 \\ u_2\end{matrix} \right],$$

$$y = x,$$

where $x = RH$, $u_1=1$ ($const.$) and $u_2=RH_{ext}$. I have measurement data for $RH_{ext}$ in Matlab (vector), a time vector $t$ with the same length and would like to do a simulation for $RH$.

Any suggestions how I could proceed? Simulink is possible, but just a script-based solution would be even better.

• I like "R" because while it is (slightly) klugy, it is free, and it has a stunningly wider variety of toolboxes, called libraries, that are also free. You can make and sell products day 1, without paying thousands of dollars per seat per year for a professional license. Can I give you an "R" friendly answer? Also - my experience with engineering curriculum is that they (sadly) teach you to focus on the mean, and not balance the central tendency with tendency of variation. What does it mean if your parameters are not infinitely precise? :) Jun 10 '15 at 12:24
• Shouldn't there be a plus sign in front of $[k1\; k2]$ instead of the minus sign? Jun 10 '15 at 15:09

Yes, if you have values for k1 and k2, data for RH_ext and t, your suggestion would work although I think you have your matrices wrong. You probably need to define x as RH - k1/k2 to get rid of the constant term:

A = -k2;
B = k2; % x_dot = -k2*x + k2*u
C = 1;
D = 0; % y = x
sys = ss(A,B,C,D);
y = lsim(sys,RH_ext,t);
RH_sim = y + k1/k2;

• I think lsim was the kind of function I was looking for, thanks! Why should I choose $B=k_1+k_2$? I mean only $k_2$ should act on the input $RH_{ext}$. My problem is basically what to do with the constant $k_1$ . Jun 11 '15 at 6:35
• Oops, you're right. I will edit my answer. Jun 11 '15 at 6:41
• Introducing $x$ makes sense, excellent! Jun 11 '15 at 9:27