You are on page 1of 8

QUICK MATLAB TUTORIAL Prepared by: Dr. Frank M.

Candocia In Matlab, you assign values to a variable and then you can perform operations on these variables. You can also plot the values contained in any variable. ASSIGNMENT Example: Assign a to have a value of 2. >> a = 2 NOTE: >> is the Matlab command prompt and adding a semicolon following a statement suppresses output to the screen, e.g. >> a = 2; will not show a = 2 below the statement you typed. Example: Assign b (a 1D array) to have values 3.2, -1, 4.7 >> b = [3.2, -1, 4.7]; % the values are separated by a comma NOTE: Anything after the % sign in a line of code is ignored by Matlab (its used for commenting your code) Example: Assign c to have numbers between 0 and 2, spaced in increments of 0.5 >> c = 0:0.5:2; Example: Assign d to have 5 values equally spaced in between 0 and 1 (including 0 and 1) >> d = linspace(0,1,5); PLOTTING You can plot one set of values vs. another set using the plot command. Example: Plot sin(t) for values of t in between 0 and 2. >> t = linspace(0,2*pi,100); % t is a vector holding 100 equally spaced values of time between 0 and 2 >> plot(t,sin(t)); The result looks like:

1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1

NOTE: sin( ) is a built-in function in Matlab that computes the sine of a number >> grid; % this draws grid lines on the plot to make it prettier

Now, the plot looks like:


1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1

>> xlabel(t (sec)); % puts a label on the x-axis of the plot >> ylabel(sin(t)); % puts a label on the y-axis of the plot >> title(Plot of sin(t) vs. t); % puts a title on the plot

The final plot looks nice and is complete and would be:
Plot of sin(t) vs. t 1 0.8 0.6 0.4 0.2 sin(t) 0 -0.2 -0.4 -0.6 -0.8 -1

3 t (sec)

NOTE: Matlab connects a line between plotted points so when plotting a continuous function, make sure you have a sufficient number of samples for which you are evaluating the function, if not the plot of the continuous function wont look good. Example: Evaluate cos(t) at 6 equally spaced values of t between 0 and 2 >> t = linspace(0,1,6); >> plot(t, cos(t)); >> grid; >> xlabel(t (sec)); >> ylabel(cos(t)); >> title(cos(t) evaluated at 6 points in the interval [0,2pi]);

cos(t) evaluated at 6 points in the interval [0,2pi] 1 0.8 0.6 0.4 0.2 cos(t) 0 -0.2 -0.4 -0.6 -0.8 -1

3 t (sec)

You can superimpose two plots (or more) on top of each other. Example: >> t1 = linspace(0,2*pi,100); >> x = sin(t1); >> t2 = linspace(-1,1,100); >> y = t2; >> plot(t1,x,b,t2,y,r); % plot function x(t) = sin(t) in the interval [0,2] with a blue color and y(t) = t in the interval [-1,1] with a red color. >> grid; >> xlabel(t (sec)); >> ylabel(x(t) = sin(t) (blue) and y(t) = t (red)); >> title(Displaying two functions on one plot);

Displaying two functions on one plot 1 0.8 x(t) = sin(t) (blue) and y(t) = t (red) 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 -1

3 t (sec)

OPERATIONS Matlab has a number of built-in functions. Some of these are:

You can add, subtract, multiply, divide or exponentiate the elements of two vectors: Example:

>> a = [6,8,10]; >> b = [3,2,2]; >> c = a + b; % now c = [9,10,12]; >> d = a b; % now d = [3,6,8]; >> e = a.*b; % you need to put .* to get e = [18,16,20]; >> f = a./b; % you need to put ./ to get f = [2,4,5]; >> g = a.^2; % you need to put .^ to square the elements in vector a to get g = [36,64,100] >> h = a.^(0.5); % this takes the square root of the elements in a. We get h = [1.7321, 1.4142, 1.4142] The operator precedence (and it goes from left to right) in Matlab is 1. parenthesis ( ) 2. multiplication and division (they have equal precendence) 3. addition and subtraction (they have equal precedence) Example: >> a = [1,2,3]; >> b = [4,5,6]; >> c = a.*(b-a) + b; % we see c = [7,11,15]; Example: >> a = [1,2,3]; >> b = sum(a); % we see b = 6 >> b = cos(a); % we see b now equals [0.5403 -0.4161 -0.9900] (we have overwritten the old value of b which was 6) >> b = exp(a); % we see b now equals [2.7183 7.3891 20.0855] CREATING YOUR OWN FUNCTIONS You can create your own functions for you to evaluate. For example, lets create a unit step function and save it in a directory called MyFunctions. Open up a text editor (such as Notepad from windows) or Matlabs built in editor and type the following two lines (with or without an empty line in between them): function y = u(t) y = (t >= 0); Now, save this file and call it u.m in directory c:\MyFunctions. If the directory doesnt exist, create this new folder in Windows. The .m tells Matlab its an m-file with Matlab commands in it.

(NOTE: This file must be saved in ASCII or text format for Matlab to be able to read it. Saving it in WORD format or any other format is no good as Matlab wont understand it.) Now, we add this function to Matlabs search path so it knows to look in directory c:\MyFunctions for a file called u.m. Do this by typing at the Matlab prompt: >> addpath(c:\MyFunctions); Now, well test if our function works. >> t = linspace(-1,1,8); % t is a vector with 8 elements; t(1) is the first element and t(8) the 8th element >> y = u(t); % we see y = [0,0,0,0,1,1,1,1], i.e. the unit step function evaluated at the values of time given in vector t. Example: Create a decaying exponential e-atu(t) function. Lets create a file called decexp.m with our text editor: function y = decexp(a,t) % a is the parameter we must pass in, t is the vector with values of time, % exp( ) is the built-in Matlab exponential function and u(t) is the function that we % previously created. y = exp(-a*t).*u(t); Save this file and from the command prompt type: >> t = linspace(-1,1,100); >> y = decexp(1,t); % the value of a is 1 >> plot(t,y); >> grid; >> xlabel(t (sec)); >> ylabel(x(t) = exp(-t)u(t)); >> title(Plot of decaying exponential with parameter a = 1);

Plot of decaying exponential with parameter a = 1 1 0.9 0.8 0.7 x(t) = exp(-t)u(t) 0.6 0.5 0.4 0.3 0.2 0.1 0 -1

-0.8

-0.6

-0.4

-0.2

0 t (sec)

0.2

0.4

0.6

0.8

You might also like