Sie sind auf Seite 1von 20

MATLAB Tutorial

By: Shahzaib Raza Osama Zaid

What is MATLAB

Matlab is a high-performance language for technical computing. It integrates computation, programming and visualization in a user-friendly environment where problems and solutions are expressed in an easy-to-understand mathematical notation.

MATLAB Operations
Matlab operations can be classified into the following types of operations: arithmetic and logical operations mathematical functions graphical functions input/output operations

Entering Matrices and Addressing the Elements


A = [1 2 3; 8 6 4; 3 6 9] A =1 2 3 864 369 The n-th element of the m-th column in matrix A from above is A(n,m).

Entering Matrices and Addressing the Elements


The k-th to l-th elements of the m-th to n-th columns can be addressed by A(k:l,m:n), e.g. >>A(2:3,1:2) ans = 36 >>A(1,1:2) addresses the first two elements of the first row 86

Generating Matrices

To create a row vector with 101 equidistant values starting at 0 and ending by pi >> x = linspace(0,pi,101) or >> x = (0:0.01:1)*pi linspace(first value, last value, number of values)
(result on next slide)

Columns 1 through 10 0 0.0314 0.0628 0.0942 0.1257 0.1571 0.1885 0.2199 0.2513 0.2827

Columns 11 through 20 0.3142 0.3456 0.3770 0.4084 0.4398 0.4712 0.5027 0.5341 0.5655 0.5969

Columns 21 through 30 0.6283 0.6597 0.6912 0.7226 0.7540 0.7854 0.8168 0.8482 0.8796 0.9111

Columns 31 through 40 0.9425 0.9739 1.0053 1.0367 1.0681 1.0996 1.1310 1.1624 1.1938 1.2252

Columns 41 through 50 1.2566 1.2881 1.3195 1.3509 1.3823 1.4137 1.4451 1.4765 1.5080 1.5394

Columns 51 through 60 1.5708 1.6022 1.6336 1.6650 1.6965 1.7279 1.7593 1.7907 1.8221 1.8535

Columns 61 through 70 1.8850 1.9164 1.9478 1.9792 2.0106 2.0420 2.0735 2.1049 2.1363 2.1677

Columns 71 through 80 2.1991 2.2305 2.2619 2.2934 2.3248 2.3562 2.3876 2.4190 2.4504 2.4819

Columns 81 through 90 2.5133 2.5447 2.5761 2.6075 2.6389 2.6704 2.7018 2.7332 2.7646 2.7960

Columns 91 through 100 2.8274 2.8588 2.8903 2.9217 2.9531 2.9845 3.0159 3.0473 3.0788 3.1102

Column 101 3.1416

Generating Matrices

The colon notation is very often used in Matlab, therefore a closer look should be taken on it. (first value:increment:last value) creates an array starting at first value, ending at last value with an increment which can be negative as well, e.g. >> v = (10:-2:0) v = 10 8 6 4 2 0

Creating a Plot

If y is a vector, plot(y) produces a piecewise linear graph of the elements of y versus the index of the elements of y. If two vectors are specified as arguments, plot(x,y) produces a graph of y versus x. For example to plot the value of the sine function from zero to 2, use >> x = 0:pi/100:2*pi; >> y = sin(x);

Title and Label

The xlabel, ylabel and zlabel functions are useful to add x-, y- and z-axis labels. The zlabel function is only necessary for three-dimensional plots. The title function adds a title to a graph at the top of the figure and the text function inserts a text in a figure. The following commands create the final appearance of graph >> xlabel(x); >> ylabel(y); >> title(y = sin(x))
Result on next slide

Controlling Axes

Matlab finds the maxima and minima of the data to be plotted by itself and uses them to create an appropriate plot box and axes labeling. The axis function overwrites this default by setting custom axis limits, >> axis([xmin xmax ymin ymax]).

Basic Operations

clear all close all

Clear all variables Close all windows

subplot(211) subplot(m,n,p), or subplot(mnp), breaks the Figure window into an m-by-n matrix of small axes and selects the p-th axes for the current plot. grid on grid

box on bounding box hold on all plots are kept on the screen

Line vector
a0=zeros(1,10) define a line vector with 10 zero elements a1=ones(1,10) define a line vector with 10 one elements

Mathematical Operations on Vectors

f=e.*e square the elements of vector e f=e.^nnth power of elements of vector e

Proper Scaling set(gca,Xlim,[0,2*pi]); sets the entire horizontal range of the plot.

set(gca,Ylim,[-2,2]); set the vertical range of the plot. Limits axis([xmin,xmax,ymin,ymax]) For Example: axis([0,2*pi,-2,2])

Result on next slide regarding Problem 2.3.2

Basic Plotting
Plot two sine functions with frequency f1 = 200 Hz and f2 = 50 Hz and amplitudes A1 = 1 V and A2 = 1.5 V within the time interval t=[0,50] ms in the same figure. Use the sampling period T = 10s.

Plots using subplot and Sampling of continuous waveform

Given is the Matlab routine [s]=waveform(t) which generates the periodical signal s(t) with period T = 20 ms. It is sampled in the time interval t=[0,65]ms with four different sampling frequencies f1 = 100Hz, f2 = 250Hz, f3 = 500Hz and f4 = 1000Hz. Display in four subfigures the continuous waveform and the corresponding sampled waveform, respectively. Use for the sampled waveform the command stem.

Random Functions
rand(x,y) generates a matrix with x rows and y columns, whose elements are uniformly distributed within the range [0,1] hist(a) plots the histogram. The value range of a is divided into 10 intervals. The number of elements in these intervals is plotted. hist(a,N) plots the histogram. The value range of a is divided into N intervals

Amplitude Modulation

Use A = 1, m(t) = cos(wct), and wc = 3ws, where ws is the cut-off frequency of the input signal. 1. Define a time vector t starting from 10, ending by 10 in steps of 0.01. 2. Generate the input signal s(t) = cos(wst) with ws = 2. 3. Create a figure showing s(t) vs. t. Limit the axis as follows: a) x-axis: -5 to 5 and b) y-axis: -2 to 2. 4. Plot the output signal xAM(t) for mAM = 0.75 in the first subplot. Plot the envelopes of xAM(t) into the same subplot. 5. Plot the output signal xAM(t) for mAM = 1.75 in the second subplot. Plot the envelopes of xAM(t) into the same subplot.
Result on next slide

Das könnte Ihnen auch gefallen