Você está na página 1de 10

Below are some useful functions in Matlab. To see an illustration for them, copy the code into Matlab.

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)

Functions 7, 8, 9: (syms, ilaplace, subs)


%syms() %allows the use of symbols instead of values in writing programs %after writing the program once using symbols, then, you can compute final %results for by just changing the values your parameters %ilaplace() %finds the inverse laplace of symbolic functions

%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.

%P2 %second-order systems num2 = 20; den2 = [1 3 20]; g2 = tf(num2,den2) step(g2)

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:

This window opens:

Click new model under file to open this:

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.

Connect the diagram as shown:

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.

Click on the binoculars to rescale.

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.

Você também pode gostar