




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
introduction to the matlab........................................
Typology: Essays (university)
1 / 28
This page cannot be seen from the preview
Don't miss anything!





















COMPUTER ENGINEERING DEPARTMENT Digital Signal Processing Lab Manual No 01 Functions and Plotting in MATLAB Course Instructor: Dr. Muhammad Majid
COMPUTER ENGINEERING DEPARTMENT Objectives:- The objectives of this session is to explore and understand the basics of MATLAB. MATLAB Introduction:- MATLAB stands for MAT rix LAB oratory. MATLAB is a high-level language and interactive environment for numerical computation, visualization, and programming. Using MATLAB, you can analyze data, develop algorithms, and create models and applications. The language, tools, and built-in math functions enable you to explore multiple approaches and reach a solution faster than with spreadsheets or traditional programming languages, such as C/C++ or Java. You can use MATLAB for a range of applications, including signal processing and communications, image and video processing, control systems, test and measurement, computational finance, and computational biology. More than a million engineers and scientists in industry and academia use MATLAB, the language of technical computing. Key Features:-
COMPUTER ENGINEERING DEPARTMENT Create a few more variables. b = 2 b = 2 c = a + b c = 3 d = cos(a) d =
When you do not specify an output variable, MATLAB uses the variable ans , short for answer, to store the results of your calculation. sin(a) ans =
If you end a statement with a semicolon, MATLAB performs the computation, but suppresses the display of output in the Command Window. e = a*b; You can recall previous commands by pressing the up- and down-arrow keys, โ and โ. Press the arrow keys either at an empty command line or after you type the first few characters of a command. For example, to recall the command b = 2, type b, and then press the up-arrow key. Matrices and Arrays:- MATLAB is an abbreviation for "matrix laboratory." While other programming languages mostly work with numbers one at a time, MATLAB is designed to operate primarily on whole
COMPUTER ENGINEERING DEPARTMENT matrices and arrays. All MATLAB variables are multidimensional arrays, no matter what type of data. A matrix is a two-dimensional array often used for linear algebra. Array Creation:- To create an array with four elements in a single row, separate the elements with either a comma (,) or a space. a = [1 2 3 4] a = 1 2 3 4 This type of array is a row vector. To create a matrix that has multiple rows, separate the rows with semicolons. a = [1 2 3; 4 5 6; 7 8 10] a = 1 2 3 4 5 6 7 8 10 Another way to create a matrix is to use a function, such as ones, zeros, or rand. For example, create a 5-by-1 column vector of zeros. z = zeros(5,1) z = 0 0 0 0 0
COMPUTER ENGINEERING DEPARTMENT 1.0000 0 - 0. 0 1.0000 0 0 0 1. Notice that p is not a matrix of integer values. MATLAB stores numbers as floating-point values, and arithmetic operations are sensitive to small differences between the actual value and its floating-point representation. You can display more decimal digits using the format command: format long p = ainv(a) p = 1.00 0000000000000 0 - 0.00 0000000000000 0 1.00 0000000000000 0 0 0 0. Reset the display to the shorter format using format short format affects only the display of numbers, not the way MATLAB computes or saves them. To perform element-wise multiplication rather than matrix multiplication, use the . operator: p = a.*a p = 1 4 9 16 25 36 49 64 100 The matrix operators for multiplication, division, and power each have a corresponding array operator that operates element-wise. For example, raise each element of a to the third power: a.^
COMPUTER ENGINEERING DEPARTMENT ans = 1 8 27 64 125 216 343 512 1000 Concatenation:- Concatenation is the process of joining arrays to make larger ones. In fact, you made your first array by concatenating its individual elements. The pair of square brackets [] is the concatenation operator. A = [a,a] A = 1 2 3 1 2 3 4 5 6 4 5 6 7 8 10 7 8 10 Concatenating arrays next to one another using commas is called horizontal concatenation. Each array must have the same number of rows. Similarly, when the arrays have the same number of columns, you can concatenate vertically using semicolons. A = [a; a] A = 1 2 3 4 5 6 7 8 10 1 2 3 4 5 6 7 8 10
COMPUTER ENGINEERING DEPARTMENT 14 Less common, but sometimes useful, is to use a single subscript that traverses down each column in order: A(8) ans = 14 Using a single subscript to refer to a particular element in an array is called linear indexing. If you try to refer to elements outside an array on the right side of an assignment statement, MATLAB throws an error. test = A(4,5) Attempted to access A(4,5); index out of bounds because size(A)=[4,4]. However, on the left side of an assignment statement, you can specify elements outside the current dimensions. The size of the array increases to accommodate the newcomers. A(4,5) = 17 A = 16 2 3 13 0 5 11 10 8 0 9 7 6 12 0 4 14 15 1 17 To refer to multiple elements of an array, use the colon operator, which allows you to specify a range of the form start:end. For example, list the elements in the first three rows and the second column of A: A(1:3,2) ans = 2 11 7
COMPUTER ENGINEERING DEPARTMENT The colon alone, without start or end values, specifies all of the elements in that dimension. For example, select all the columns in the third row of A: A(3,:) ans = 9 7 6 12 0 The colon operator also allows you to create an equally spaced vector of values using the more general form start:step:end. B = 0:10: B = 0 10 20 30 40 50 60 70 80 90 100 If you omit the middle step, as in start:end , MATLAB uses the default step value of 1. Workspace Variables:- The workspace contains variables that you create within or import into MATLAB from data files or other programs. For example, these statements create variables A and B in the workspace. A = magic(4); B = rand(3,5,2); You can view the contents of the workspace using whos. whos Name Size Bytes Class Attributes A 4x4 128 double B 3x5x2 240 double The variables also appear in the Workspace pane on the desktop.
COMPUTER ENGINEERING DEPARTMENT Name Size Bytes Class Attributes myText 1x12 24 char You can concatenate strings with square brackets, just as you concatenate numeric arrays. longText = [myText,' - ',otherText] longText = Hello, world - You're right To convert numeric values to strings, use functions, such as num2str or int2str. f = 71; c = (f-32)/1.8; tempText = ['Temperature is ',num2str(c),'C'] tempText = Temperature is 21.6667C Calling Functions:- MATLAB provides a large number of functions that perform computational tasks. Functions are equivalent to subroutines or methods in other programming languages. To call a function, such as max, enclose its input arguments in parentheses: A = [1 3 5]; max(A) ans = 5 If there are multiple input arguments, separate them with commas: B = [10 6 4]; max(A,B) ans =
COMPUTER ENGINEERING DEPARTMENT 10 6 5 Return output from a function by assigning it to a variable: maxA = max(A) maxA = 5 When there are multiple output arguments, enclose them in square brackets: [maxA,location] = max(A) maxA = 5 location = 3 Enclose any character string inputs in single quotes: disp('hello world') hello world To call a function that does not require any inputs and does not return any outputs, type only the function name: clc The clc function clears the Command Window. 2 - D Plotting:- Line Plots To create two-dimensional line plots, use the plot function. For example, plot the value of the sine function from 0 to 2pi: x = 0:pi/100:2pi; y = sin(x); plot(x,y)
COMPUTER ENGINEERING DEPARTMENT By adding a third input argument to the plot function, you can plot the same variables using a red dashed line. plot(x,y,'r--')
COMPUTER ENGINEERING DEPARTMENT The 'r--' string is a line specification. Each specification can include characters for the line color, style, and marker. A marker is a symbol that appears at each plotted data point, such as a +, o, or . For example, 'g:' requests a dotted green line with * markers. Notice that the titles and labels that you defined for the first plot are no longer in the current figure window. By default, MATLAB clears the figure each time you call a plotting function, resetting the axes and other elements to prepare the new plot. To add plots to an existing figure, use hold. x = 0:pi/100:2*pi; y = sin(x); plot(x,y) hold on y2 = cos(x); plot(x,y2,':')
COMPUTER ENGINEERING DEPARTMENT Add axis labels and a title. The FontSize property in the title function increases the size the text used for the title. xlabel('x = 0:2\pi') ylabel('Sine of x') title('Plot of the Sine Function','FontSize',12)
COMPUTER ENGINEERING DEPARTMENT Plotting Multiple Data Sets in One Graph:- Multiple x-y pair arguments create multiple graphs with a single call to plot. MATLAB uses a different color for each line. For example, these statements plot three related functions of x : x = 0:pi/100:2*pi; y = sin(x); y2 = sin(x-.25); y3 = sin(x-.5); plot(x,y,x,y2,x,y3)