On 5th semester of studies I had "Programming in MATLAB" course. One of the tasks during that course was to write program that simulates system response to various inputs (step, pulse, sine).
As I don't have a MATLAB, I tested the code in Octave, but it should work in MATLAB without any problems.
Main program file is called simulation.m. On top of the file system and simulation constants are set. Then input signal type is chosen (by commenting and uncommenting on of 3 lines - it's not elegant, but it meets project requirements).
After that system response is calculated by corresponding function, depending on the system order (which results from system constants).
Then y,u and t vectors' lengths are adjusted (y vector is usually longer than t, which results from adding zeros to the beginning of response (system delay)).
The last thing to do is creating plot, adding legend, title and voila!
I was given transfer function in form of Let's initially ignore the system delay. Let .
After multiplying first equation and substitution we get: .
Taking inverse Laplace Transform yields (1)
In my calculations I used derivative approximation
First derivative is then , second
If we substitute these approximations into eq. (1) we get
Now we transform this equation to get y(k+3) and we place k-3 instead of k:
And that's the equation used to calculate system response. Initially I ignored system delay - it's taken into account after response is calculated by shifting y (output) vector left and adding zeros to the beginning.