Chapter 2 MATLAB Example

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

ME 481/581

Chapter 2

January 23, 2012

Chapter 2 MATLAB example


Below is a MATLAB script that models and simulates a transfer function. This is very similar to Problem 4 in the homework. Save this le in your MATLAB workspace with lename demo.m (or something like that) and you can try it out. % This script defines first-order discrete system: % % 0.5 % G(z) = ----------% z - 0.5 % % and finds its response to a sinusoidal input using the "lsim" function. T = 0.1; % Set sample time to 0.1 second % This is another way (num, den coeffs)

G = tf([0 0.5],[1 -0.5],T);

f = 1; % Frequency of sinusoid in Hz period = 1/f; % Period of sinusoid in seconds t = 0:T:3*period; % Define a time vector of length 3 periods (should be long enough) t = t; % Make it a column vector (optional, but my preference) u = sin(2*pi*f*t); % Generate input signal plot(t,u,k-o); y = lsim(G,u,t); % Find output plot(t,u,k-o,t,y,r-*); grid; % Plot both input and output plot(t,u,k-o,t,y,r-*); grid; % Plot both input and output xlabel(Time (seconds)); % Annotate time axis (one method) legend(Input,Output); % Put legend on plot You can measure the frequency response characteristics from the plot: amplitude ratio and phase shift. Remember that to get phase shift from time shift t use the relation = t .

You might also like