Escolar Documentos
Profissional Documentos
Cultura Documentos
In Matlab, starting a line by % means that this line is treated as a comment that will not be executed as a command. Function 1: (poly)
%poly %takes in vector of roots %outputs vector of coefficients %example %f=(s+5)(s+6)(s+2) %roots are s=-5, s=-6, s=-2 roots_f = [-2 -5 -6]; poly(roots_f)
Function 2: (roots)
%roots %takes in vector of coefficients %outputs vector of roots %example %f=s^3 + 13s^2 + 52s +60 f = [1 13 52 60]; roots(f)
Function 3: (conv)
%conv %used to multiply two functions %example %f1= s^2 + 2 %f2= s^3 + 3s + 1 %f3= f1*f2 = s^5 + 5s^3 + s^2 + 6s +2 f1 = [1 0 2]; f2 = [1 0 3 1]; f3 = conv(f1,f2)
Function 4: (residue)
%residue (performs partial fraction expansion) %takes in vector of num and den coefficients %outputs vectors of residues, poles and constants (obtained if order of %num >= order of den) %example %num = s^3 + 1 %den = s^2 + 3s +1 num = [1 0 0 1]; den = [1 3 1]; [r,p,k] = residue(num,den)
Function 5: (tf)
%tf %takes in vectors of coefficients of num, den %outputs transfer function %example num = [1 2 1]; den = [1 3 2 14]; h = tf(num,den)
Function 6: (zpk)
%zpk %takes in vectors containing roots of num, den and gain %outputs tranfer function (not simplified) %example
num = [1 2 1]; den = [1 3 2 14]; h = tf(num,den) num_rts = roots(num) den_rts = roots(den) g = zpk(num_rts, den_rts, 1)
%example syms s p1 p2 k f = k/((s+p1)*(s+p2)); pretty(f) c = ilaplace(f); %now you can substitute values for p1, p2 and k and get the final result by %using subs() p1=2; p2=6; k=13; fi=subs(c); pretty(fi)
Step response and Impulse response of first-order and second-order systems using matlab codes:
%impulse response L = tf(1, [1 5]) impulse(L)
You can add a grid to the graph by typing: grid on or by right-clicking on graph and clicking grid.
%P1 %first-order system step response num1 = 10; den1 = [1 20]; g1 = tf(num1,den1) step(g1)
Note that the final value is 0.5 when the input is unit step, which agrees with the final value theorem applied to our transfer function.
SIMULINK Simulink is a useful interface that can allow the use of block diagrams instead of a matlab code to model the system under study. To access Simulink, click on the red icon shown below:
You can drag blocks from the library browser onto the blank model.
Transfer function can be found under continuous Scope is found under sinks Step input is found under sources The mux is available under commonly uses blocks. It allows the scope to plot the input and output together in one plot.
Each block settings can be accessed by double-clicking on it. Double click on step and change the step time to 0. Change the run time shown above from 10 to 1s and click on run.
You can also export your output function to the Matlab workspace and use it there as a variable. To do that drag the to workspace icon from the sinks library and connect it to the variable you wish to export.
Double click on to workspace and choose a variable name and change the save format to array as shown.
Now when you run a variable named output, which has the output signal stored as a vector, will appear in the Matlab workspace.