You are on page 1of 11

A quick introduction to MATLAB

What is Matlab?
MATrix LABoratory This is an interactive software package for numeric processing oriented to matrix manipulation.

What can we do with MATLAB?


- Basic Arithmetic (calculator) - Logical and relational operations - Polynomial Operations - Set operations - Signal Processing - Plotting - Systems (linear, non-linear, control,) : *Design *Simulation - Input/Output data files (text or Matlab format) ... - any other type of numerical processing All this is done by means of: + on-line commands + programs and functions Remember that all these applications are matrix-oriented.

*Test

When you need help...


The on-line help of Matlab is very good. If you don't know where to start: >> help Specific help about a known topic (command, function, directory): >> help fft >> help toolbox\signals Keyword search in the descriptions of the commands: >> lookfor signal >> lookfor processing

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

You can make your own toolbox!

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

Matlab has many built-in functions:


>> sqrt(64) ans = 8 >> sin(pi/2) ans = 1 >> abs(-56) ans = 56 >> e = ones(3,3) e = 1 1 1 1 1 1 1 1 1 >> size(e) ans = 3 3

Matrix manipulation is very easy:


>> f = d + e f = 2 3 4 5 6 7 8 9 10 >> f ans = 2 5 8 3 6 9 4 7 10 >> ans(2,3) ans = 9 >> g = d * e g = 6 6 6 15 15 15 24 24 24 >> h = d + 3*e

h = 4 5 6 7 8 9 10 11 12 >> k = f(1:2,:) k = 2 3 4 5 6 7 >> m = cos(k) m = -0.4161 -0.9899 0.2836 0.9601

-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

You can use complex numbers:


>> c1 = 3 + 4i ; exp(c1) ans = -13.1288 - 15.2008i

Remember that in this case exp( a + bi ) is exp(a)[ cos(b) + i*sin(b) ]


>> a=[ 1+i 2 ] a = 1.0000 + 1.0000i 2.0000 >> b=[ 4-2i 5+10i ]; >> a + b ans = 5.0000 - 1.0000i 7.0000 + 10.0000i

The "colon" (:) operator:


>> numbers1 = 2:2:8 numbers1 = 2 4 6 >> numbers2 = 2:8 numbers2 = 2 3 4 8 5 6 7 8

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

20 15 10 5 0 -5 -10 -15 -20 10 5 0 -5 0.2 -10 0 0.4 0.8 0.6 1

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);

>> x x = >> y y = >> [x;y]

Now, plot the vectors you just created: >> figure >> plot(xx,yy) >> grid

More functions (a tiny bit of signal processing)


Open another text file and type this: function ar % AREA Area % ar = % x1 = % y1 = = area(x1,y1) between a curve and the X axis area(x1,y1) x coordinates of the curve y coordinates of the curve

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 = );

Modify your function to make a better calculation.

More programs and more plotting


Create a new M File with Matlab Editor or any text editor. t= 0:0.1:20; x1= 3*t-2; %definition of a line plot(t,x1); title(Plot of a line) x2= t.^2-20; figure plot(t,x1); hold on plot(t,x2); xlabel(t) ylabel(f(t)) grid x3= x4= x5= x6= x1 + x2; x1 - x2; x1.^2 ; cos(x1.*x2); %definition of a parabola %open a new window for figures or plots %hold past plots on the current window

% definition of 4 new curves

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...);

A program for writing a text file


varname1=input(Variable 1:,s); var1=eval(varname1); varname2=input(Variable 2:,s); var2=eval(varname2); l1=length(var1); file1 = fopen(a:matdata.txt,wt); for index=1:l1 fprintf(file1,%2.2f %f\n', var1(index),var2(index)); end fclose(file1); var3(1:2:2*l1-1)=var1(1:l1); var3(2:2:2*l1)=var2(1:l1); file2 = fopen('a:matdata2.txt','wt'); fprintf(file2,'%2.2f\t%3.5f\n', var3(1:2*l1)); fclose(file2); Call your program from the command window: >> wfile Variable 1:t Variable 2:x1 >> Open the files and check the result.

A program for reading a text file


file1 = fopen('a:\matdata.txt','r') read1=[]; datline=fgetl(file1); while datline ~= -1 read1=[read1,sscanf(datline,'%f')]; datline=fgetl(file1); end fclose(file1);

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 dt Vin = V R + Vc i=C

its differential equation is:

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

Call Simulink from the command window: >> simulink

~ 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

N.B. The parameters may be variables defined in the Matlab environment.

You might also like