Professional Documents
Culture Documents
What is Matlab?
MATrix LABoratory This is an interactive software package for numeric processing oriented to matrix manipulation.
*Test
Toolboxes
Sets of programs developed for a specific field. Different Toolboxes provided by MathWorks include: - Simulink - Image Processing - Signal Processing - Control Systems - Symbolic Maths - Neural Networks - Fuzzy Logic
A first try...
>> 3*4 ans =
(on-line commands)
12 >> d=[1 2 3 ; 4 5 6 ; 7 8 9] d = 1 2 3 4 5 6 7 8 9
-0.6536 0.7539
>> matrix_product = h * k ??? Error using ==> * Inner matrix dimensions must agree. >> matrix_product = k * h matrix_product = 69 78 87 132 150 178
Element-by-element operations:
>> elements_product = k .* h ??? Error using ==> .* Matrix dimensions must agree. >> elements_product = k .* m elements_product = -0.8322 -2.9697 -2.6144 1.4180 5.7606 5.2773
>> numbers3 = numbers2(1:2:5) numbers3 = 2 4 6 >> 2 : .5 : 4 ans = 2 2.5 3 3.5 >> 6 : -.5 : 4 ans = 6 5.5 5 4.5
4 4
Representation of polynomials
We can represent 3x2-2x+1 as: >> p = [3 -2 1]; and get its roots: >> r = roots(p); >> r r = 0.3333 + 0.4714i 0.3333 - 0.4714i There are many built-in functions for manipulation of polynomials (e.g. polyval).
Programs (Scripts)
Open the built-in Matlab editor (menu: File - New - M File) to type the following program or type it in any text editor. power = 3 x = 6 : 15; y = x.^power; [x;y] Save as .m file (for example: nothing.m ) If you saved your file in the directory: d::\user then type the following line in the command window: >> cd d:\user Call your program from the command window: >> nothing power = 3 ans = Columns 1 through 6 6 7 8 9 216 343 512 729 1000 1331 Columns 7 through 10 12 13 14 15 1728 2197 2744 3375 You can also run your programs from the menu.
10
11
Basic functions
Open a new file in the text editor and type: function y = cosgen(x,a,f,p) %COSGEN Generation of a cosine wave % y = cosgen(x,a,f,p) % y - cosine of x % a - amplitude % f - frequency [hertz] % p - phase [radians] y = a*cos( 2*pi*f*(x + p/(2*pi*f)) ); save as cosgen.m Something nice: >> help cosgen COSGEN Generation of a cosine wave y = cosgen(x,a,f,p) y - cosine of x a - amplitude f - frequency [hertz] p - phase [radians] Create a vector X for your function: >> xx = 0 : 1/75 : 1; Call your function from the command window : >> yy = cosgen(xx,8,5,pi/2)
Basic plotting
>> plot(xx,yy) Lets try a 3D plot: >> plot3(xx,yy,2*yy) >> grid
More functions
A function in Matlab can return a matrix. Lets redefine our cosgen function: function [tt,xx]=cosgen2(a,f,p,d,s) %COSGEN Generation of a cosine wave % [tt,xx]=cosgen(a,f,p,d,s) % tt - time [seconds] % xx - cosine of tt % a - amplitude % f - frequency [hertz] % p - phase [radians] % d - duration [seconds] % s - samples per period tt= [ 0 : 1/(s*f) : d ]; % Here we define the time vector xx= a*cos( 2*pi*f*(tt + p/(2*pi*f)) ); % Cosine of tt save as cosgen2.m Try this: >> cosgen(11,5,-pi/2,.5,15) ans = >> [x,y]=cosgen(11,5,-pi/2,.5,15);
Now, plot the vectors you just created: >> figure >> plot(xx,yy) >> grid
index=1; acc_area=0; while( index < length(x1) ) base = x1(index+1) - x1(index); height = y1(index); acc_area = acc_area + base*height; index = index + 1 ; end ar = acc_area; The function must be saved as area.m
Define two vectors of coordinates for your function (e.g. time and a function of time) and calculate the area between the signal defined and the X axis: >> [a,b]=cosgen(8,10,0,.04,30); >> the_area = area(a,b) the_area = What was this calculation? How can it be improved?
Create a new pair of vectors of coordinates: >> [a2,b2]=cosgen(8,10,0,.04, >> the_area2 = area(a2,b2) the_area2 = );
figure subplot(3,2,1) %divide window in 6 plots (3 rows, 2 cols) % and choose first subplot plot(t,x1,w); % draw in white grid subplot(3,2,2) % choose subplot 2 plot(t,x2,y); % draw in yellow grid subplot(3,2,3) % choose subplot 3 semilogx(t,x3,g); %draw in green
title(This is a plot with only one logarithmic scale); grid subplot(3,2,4) plot(t,x4,ro); %draw in red with circular marks grid subplot(3,2,5) loglog(t,x5,b-); %draw in blue with a dashed line title(This is a plot with two logarithmic scales); grid subplot(3,2,6) plot(t,x6,m:); %draw in magenta with a dotted line grid title(This is the last plot...);
Call your program from the command window: >> rfile Check the contents of the variable: >> read1
Simulink
We can simulate a dynamic system by drawing its block-diagram. Consider, for example, the RC filter:
V R = iR
Vin C Vout = Vc
dVc RC + Vc = Vin dt
which can be expressed as:
dVc 1 = (Vin Vc ) dt RC
The block-diagram is:
Vin
dVc dt
1/RC
1/s
~ Create a new model from the Simulink window: File Menu -> New... ~ Double-click on the groups to see the blocks inside ~ Drag to the new window all the blocks you need (with the mouse) ~ Draw the lines with the mouse
Auto-Scale Graph1 + Signal Generator Sum 1/(2*pi) Gain 1/s Integrator Auto-Scale Graph
~ Set the parameters inside the different blocks (double-click on them to see the parameters) ~ Run! Menu Simulation -> Start Menu Simulation -> Stop