%% Generate Input for Simulating Multi-Input System
% To simulate a multi-input system with lsim, you provide the input signals as a matrix whose columns represent the signal applied to each input. In other words, u(:,j) is the signal applied to the jth input at each time step. To use gensig to generate such an input matrix, create the signals for each input together and stack them together in a matrix.
% For instance, create a signal for simulating a two-input system that injects a square wave of period 2 s into the first input, and a pulse every 1.5 s into the second input. Specify a duration and sample time so that the two vectors have the same length, which is necessary for combining them into a matrix.

Tf = 8;
Ts = 0.02;
[uSq,t] = gensig("square",2,Tf,Ts);
[uPu,~]  = gensig("pulse",1.5,Tf,Ts);
u = [uSq uPu];
size(u)

% Each row u(i,:) of u is the signal (u1,u2) applied to the inputs at the corresponding time t(i).
%
% You can combine signals that are not created with gensig provided they have the same length. For instance, suppose that you want to simulate a three-input system by applying uSq to the first input and uPu to second input. You also want to apply a ramp to the third input that starts at 0 and increases to 1 at the final time Tf = 8. Ensure that the signal is a column vector with the same length as uSq and uPu. Then combine it with the other signals to create the input matrix.

uRa = linspace(0,1,401)'; 
u = [uSq uPu uRa];
size(u)
plot(t,u)

%
% You can now use u and t to simulate a three-input model. Generate a three-input, two-output state-space model, and simulate the response at its two outputs to u applied at the inputs.

rng('default')
sys = rss(3,2,3);
lsim(sys,u,t)

%
% Copyright 2020 The MathWorks, Inc.
%