The Islamia University of Bahawalpur

University College of Engineering & Technology Electromagnetic Field Theory EE-224 Electronics Engineering (4th Semester) LAB EXPERIMENT NO:01

Name:Ashfaq Cheema Lab Instructor Signature: ___________

Roll No. 09ES18 Date: _________


OBJECTIVE: The objectives of this lab session are to:
(i) (ii) (iii) Become familiar with MATLAB environment Become familiar with Basic MATLAB commands Write small programs using MATLAB

MATLAB is a high level computer language used for the programming of complex engineering problems. MATLAB stands for MATrix LABoratory. As the name suggests, MATLAB a plethora of functions used for different operations on matrices, where a matrix can be scalar (single element), a vector (one dimensional) or an n x m Matrix (two dimensional).

In MATLAB we have many tool boxes each containing functions related to specific operations, just as we have libraries in JAVA or C language. Communications, Control System, Image processing and Image acquisition are some of the most commonly used tool boxes. MATLAB has extensive capabilities of two and three dimensional plotting. We will use these plotting facilities of MATLAB to aid in the physical interpretation of equations, particularly those related to fields and potentials.

For example.m) extension. For command line execution commands are entered in the command window where (. complete program is first written in a file and saved with DOT M (. they will return values. We will use DOT M files throughout this lab. if you type >>x = 5 MATLAB will print x= 5 And assign the number five to the variable x. BASIC TERMINOLOGY: MATLAB has some basic terms and predefined variables you should get familiar with before going any further. if you type >> 5 MATLAB will return ans = 5 . When you type most commands to MATLAB. For instance.m) file execution. the first one is command line execution and the second one is M-File or DOT M (. You can assign these values to variables by typing in equations. The complete program is then RUN like any other programming language. One of these is the variable ans. MATLAB uses ans for any expression you don't assign to a variable. there are two modes of executing a program.In MATLAB.m) file.

And assign the value 5 to the variable ans. it will just return another << prompt. MATLAB will assign the value five to the variable x. you type the list of elements separated by blanks or commas surrounded by [ and ]. ans will always be assigned to the most recently calculated value you didn't assign to anything else. For example. If you terminate a command with a semi-colon. if you type >>x = 5. MATLAB does not use ZERO-BASED indexing (as in C/C++ or JAVA). To assign x to be a matrix by explicitly entering the elements. INDEXING: At times. MATLAB starts . To do this. MATLAB will suppress the printing of the variable name and value resulting from the calculation. but rather than tell you it did that. and use semicolons to separate the rows. 1 3 5 7] Results in x= 2 1 4 3 6 5 8 7 The MATLAB workspace is defined as the collection of all the variables you have defined during the current MATLAB session. MATLAB also has vectors. MATLAB works with two basic types of data objects: scalars and matrices. Vectors are a special case of matrices which are only 1 row by any number of columns. you will want to deal with just a part of a vector or matrix. For example. you type x(n). To get the nth element of the vector x. typing >>x = [2 4 6 8 . you need to use MATLAB's indexing facility. Thus.

The element in the row. You can also use indices on matrices. But it should be remembered that 0 Index is in valid. index must be a positive integer or logical. j). as clear from output of the Program. >>x = [ 2 4 6 8 ] What is the output of following commands? >>x (1) >>x (3) >>x (0) Input: >>x = [2 4 6 8] Outputs: >>x (1) ans = 2 >> x (3) ans = 6 >> x(0) ??? Attempted to access x(0).counting from one when numbering vector elements. Comments: Above commands clears the concept that values stored in any variable can be accessed by its index no. . jth column of x is x(i . so the first element of x is x(1) and not x(0).

2500 0. You can use the colon to specify a range of numbers. / for division. multiply or divide all the elements of a vector or matrix by a scalar. and x/2 will divide each element of x by 2. so x = 1 + 5 will give x the value 6. All of these work for two scalars.5000 0.25 : 1. all of the basic operations ( =.25 Output: x = 0 0. if x is a matrix or vector. then x+1 will add one to each element of x.25 :1. * for multiplication.2500 7 6 5 . What is the function of "-1" and "0.25" in above commands? OUTPUT AND COMMENTS: Input: >> % Ashfaq Cheema 09ES18 >> x= 8: -1 : 5 Output: x = 8 Input: >> x=0 : 0.0000 1.BASIC ARITHMETIC: MATLAB uses a straightforward notation for basic arithmetic on scalars. and ^) are defined to work with complex scalars. The symbol + is used to add scalars. Typing >>x = 1 : 4 Will return X=1 2 3 4 You can optionally use a third number in between the following commands: >>x = 8 : -1 : 5 >>x = 0 : 0. and ^ for exponentiation. /. Similarly. For example. *. you can add. MATLAB uses ± for subtraction. subtract. Another useful operator is the colon.25 Record the output. -.7500 1. In addition.

25.e.25 which is specified upper value i. SUBPLOT. ROOTS >>help plot >>help for >>help subplot >>help roots Try the following commands >>doc plot >>doc for >>doc subplot >>doc roots . FOR. So -1 is use to decrement the values from upper to lower specified data.25. The help function knows about all tcommands listed in this manual. As clear from output that values are from 8 to 5. Typing help function will tell you the syntax for the function.25: 1. The -1 specifies that the out values will be written in reverse order. It will also give you a short description of claims you are in error.Comments: In above commands x = 8: -1 : 5. try looking at the help for the functions you are using. MATLAB HELP: MATLAB has a fairly good help facility. x= 0: 0. what arguments it expects.e 1. the middle value increase the values in output by 0.25 unless it reaches to 1. i. that is values decrement by 1. In second case. Check MATLAB help for PLOT.

For example command >>doc for will have output as. Where as when Doc is used. .What is the difference between help and doc? What do you learn about these commands? Record your observation: OUTPUT & COMMENTS: Difference B/w Help and Doc: When help command is given with some required information for example >>Help for The Matlab gives the brief information about (in this case about For) in the same window where commands are entered. Following picture clears the Idea. a new Help window is opened which contains detailed information.

POLYNOMIAL OPERATIONS: Vectors are used to represent polynomials. The usefulness of these commands have already been discussed above. The MATLAB roots function will calculate the roots of a polynomial for you. you use a length N+1 vector where the elements are the coefficients of the polynomial arranged in descending order of exponent. to define a polynomial (y = x2 -5x + 6) you would type: >> y = [1 -5 6] . OUTPUT & COMMENTS: Input: >> % Ashfaq Cheema 09ES18 >> y = [1 -5 6] Output: >>roots (y) ans = 3. If you want to represent an Nth-order polynomial. compare it with the roots of equation calculated manually.0000 2.0000 .Comments: Both commands Doc and Help are useful to learn about different function and there use in Matlab. Execute the following command >> roots(y) Record the output of above command. So.

Try roots command on following polynomials and record your observations: y = x2 ± 4 OUTPUT & COMMENTS: Input: >> % Ashfaq Cheema 09ES18 >> y = [1 0 -4] Output: >>roots (y) ans = 2.8627 1.1145 .8341 0.9258 .0000 -2.1145 + 1.Comments: Answer is the same as calculated manually.000 y = x5 ± 4x4+3x 3 + 6 OUTPUT & COMMENTS: Input: >> % Ashfaq Cheema 09ES18 >> y = [1 -4 3 0 0 6] Output: >> roots (y) ans = 2.1.1051i 0.1051i -0.

0. 7] Output: >>roots (y) ans = 0.0.3687 -0. .5198i 0.2752 .8299i -0. which in usual practice require lot of time. 0. 0.y = 745x9 ± 384x4 + 7 OUTPUT & COMMENTS: Input: >> % Ashfaq Cheema 09ES18 >> y = [745. -384. 0.3674i -0.3674i -0. 0. 0.0.0012 .2752 + 0.0012 + 0.8702 0.7103 .7103 + 0. 0.3663 Comments: Above commands and programs were very useful to solve complex equations.5198i -0.8299i 0.0.

From the help find the syntax for "while" loop. The "for" command allows you to make a command or series of commands be executed several times. Translate the above program into equivalent while loop and write down your code: OUTPUT & COMMENTS: Input: >> % Ashfaq Cheema 09ES18 >> For i = 1 :5 i end Ouput: i=1 i=2 i=3 i=4 \ i=5 . FOR-LOOP: >>for i = 1 : 5 >> i >>end Note down the output of above command.CONTROL STRUCTURES: MATLAB includes several control structures to allow you to write programs. It is functionally very similar to the for function in C or java.

n) = m+n*i Record the output of above program. >>for m= 1 : 3 >> >> >>end >>end for n= 1 : 3 x(m.Using While: Input: >> % Ashfaq Cheema 09ES18 >> i=1:5. >> while i<1 I end Output: i=1 2 3 4 5 You can have nested for loops. what do you observe? OUTPUT & COMMENTS: .

>>if a>0 >> x = a^2 >>else >> >>end x = -a^2 . note that it has to have an end to indicate which commands are actually parts of the if. The basic command looks like >>a=5.IF-STATEMENT: The "if" command lets you have programs that make decisions about what commands to execute. In addition. you can define an else clause which is executed if the condition you gave the if is not true. Again. We could expand our example above to be >>a= -5. if a is positive. >>if a>0 >> x=a^2 >>end This command will assign x to be the value of a squared.

>> if a <0 a^2 else x = -a^2 end Output: ans = 4 We can also have "if-else if" in MATLAB >>if a>0 >> x = a^2 >>else if a = = 0 .Try different values of a and note down your observation. OUTPUT & COMMENTS: Input: >> % Ashfaq Cheema 09ES18 >> a=-2.

>> >>else >> x= i x = -a^2 >>end >>end Try different values of a and note down your observation. Why we are using two end statements? OUTPUT & COMMENTS: .

CONCLUSIONS: Describe your conclusions based on the objectives of this lab session: y y y This Lab session was very useful as a beginner for Matlab Lab provides basics of Matlab Using different features and the usefulness is discussed. In MATLAB what ever follows % mark in a line is declared as comment % This is a comment While doing your homework assignments make sure that you include essential comments to elaborate your program.COMMENTS: Like any other programming language MATLAB also has provision to write comments. END .

Sign up to vote on this title
UsefulNot useful