Vous êtes sur la page 1sur 17

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 Roll No. 09ES18

Lab Instructor Signature: ___________ Date: _________

INTRODUCTION TO MATLAB

OBJECTIVE: The objectives of this lab session are to:


(i) Become familiar with MATLAB environment
(ii) Become familiar with Basic MATLAB commands
(iii) Write small programs using MATLAB

BACKGROUND:
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.
In MATLAB, there are two modes of executing a program; the first one is command line
execution and the second one is M-File or DOT M (.m) file execution. For command line
execution commands are entered in the command window where (.m) file, complete program is
first written in a file and saved with DOT M (.m) extension. The complete program is then RUN
like any other programming language. We will use DOT M files throughout this lab.

BASIC TERMINOLOGY:
MATLAB has some basic terms and predefined variables you should get familiar with
before going any further. One of these is the variable ans. When you type most commands to
MATLAB, they will return values. You can assign these values to variables by typing in
equations. For example, if you type

>>x = 5

MATLAB will print

x=

And assign the number five to the variable x. MATLAB uses ans for any expression you don't
assign to a variable. For instance, if you type

>> 5

MATLAB will return

ans = 5
And assign the value 5 to the variable ans. Thus, ans will always be assigned to the most
recently calculated value you didn't assign to anything else.

If you terminate a command with a semi-colon, MATLAB will suppress the printing of
the variable name and value resulting from the calculation. For example, if you type

>>x = 5;

MATLAB will assign the value five to the variable x, but rather than tell you it did that, it will
just return another << prompt. MATLAB works with two basic types of data objects: scalars and
matrices. MATLAB also has vectors. Vectors are a special case of matrices which are only 1
row by any number of columns. To assign x to be a matrix by explicitly entering the elements,
you type the list of elements separated by blanks or commas surrounded by [ and ], and use semi-
colons to separate the rows. For example, typing

>>x = [2 4 6 8 ; 1 3 5 7]

Results in

x= 2 4 6 8

1 3 5 7

The MATLAB workspace is defined as the collection of all the variables you have defined
during the current MATLAB session.

INDEXING:
At times, you will want to deal with just a part of a vector or matrix. To do this, you need
to use MATLAB's indexing facility. To get the nth element of the vector x, you type x(n).
MATLAB does not use ZERO-BASED indexing (as in C/C++ or JAVA). MATLAB starts
counting from one when numbering vector elements, so the first element of x is x(1) and not
x(0). You can also use indices on matrices. The element in the row, jth column of x is x(i , j).

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

>> x (3)

ans =

>> x(0)

??? Attempted to access x(0); index must be a positive integer or logical.

Comments:
Above commands clears the concept that values stored in any variable can be
accessed by its index no. But it should be remembered that 0 Index is in valid, as clear from
output of the Program.
BASIC ARITHMETIC:
MATLAB uses a straightforward notation for basic arithmetic on scalars. The symbol +
is used to add scalars, so x = 1 + 5 will give x the value 6. Similarly, MATLAB uses – for
subtraction, * for multiplication, / for division, and ^ for exponentiation. All of these work for
two scalars. In addition, you can add, subtract, multiply or divide all the elements of a vector or
matrix by a scalar. For example, if x is a matrix or vector, then x+1 will add one to each element
of x, and x/2 will divide each element of x by 2. all of the basic operations ( =, -, *, /, and ^)
are defined to work with complex scalars.

Another useful operator is the colon. You can use the colon to specify a range of numbers.
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.25 : 1.25

Record the output. 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 7 6 5

Input:

>> x=0 : 0.25 :1.25

Output:

x = 0 0.2500 0.5000 0.7500 1.0000 1.2500


Comments:
In above commands x = 8: -1 : 5, The -1 specifies that the out values will be written
in reverse order. As clear from output that values are from 8 to 5, that is values decrement by 1.
So -1 is use to decrement the values from upper to lower specified data.

In second case, x= 0: 0.25: 1.25, the middle value increase the values in output by 0.25 unless it
reaches to 1.25 which is specified upper value i.e 1.25.

MATLAB HELP:
MATLAB has a fairly good help facility. The help function knows about all tcommands
listed in this manual. Typing help function will tell you the syntax for the function, i.e. what
arguments it expects. It will also give you a short description of claims you are in error, try
looking at the help for the functions you are using.

Check MATLAB help for PLOT, FOR, SUBPLOT, ROOTS

>>help plot

>>help for

>>help subplot

>>help roots

Try the following commands

>>doc plot

>>doc for

>>doc subplot

>>doc roots
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.

Where as when Doc is used, a new Help window is opened which contains detailed information.

For example command “>>doc for” will have output as.


Comments:
Both commands Doc and Help are useful to learn about different function and there
use in Matlab. The usefulness of these commands have already been discussed above.

POLYNOMIAL OPERATIONS:
Vectors are used to represent polynomials. If you want to represent an Nth-order
polynomial, you use a length N+1 vector where the elements are the coefficients of the
polynomial arranged in descending order of exponent. So, to define a polynomial (y = x2 -5x +
6) you would type:

>> y = [1 -5 6] ;

The MATLAB roots function will calculate the roots of a polynomial for you. Execute the
following command

>> roots(y)

Record the output of above command; compare it with the roots of equation calculated manually.

OUTPUT & COMMENTS:

Input:

>> % Ashfaq Cheema 09ES18

>> y = [1 -5 6]

Output:

>>roots (y)

ans =
3.0000

2.0000
Comments:
Answer is the same as calculated manually.

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

-2.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.8627

1.8341

0.1145 + 1.1051i

0.1145 - 1.1051i

-0.9258
y = 745x9 – 384x4 + 7

OUTPUT & COMMENTS:

Input:

>> % Ashfaq Cheema 09ES18

>> y = [745, 0, 0, 0, 0, -384, 0, 0, 0, 7]

Output:

>>roots (y)

ans =

0.8702

0.2752 + 0.8299i

0.2752 - 0.8299i

-0.7103 + 0.5198i

-0.7103 - 0.5198i

0.3687

-0.0012 + 0.3674i

-0.0012 - 0.3674i

-0.3663

Comments:
Above commands and programs were very useful to solve complex
equations, which in usual practice require lot of time.
CONTROL STRUCTURES:
MATLAB includes several control structures to allow you to write programs. The "for"
command allows you to make a command or series of commands be executed several times. It is
functionally very similar to the for function in C or java.

FOR-LOOP:

>>for i = 1 : 5

>> i

>>end

Note down the output of above command. From the help find the syntax for "while" loop,
Translate the above program into equivalent while loop and write down your code:

OUTPUT & COMMENTS:


Input:

>> % Ashfaq Cheema 09ES18

>> For i = 1 :5

end

Ouput:

i=1

i=2

i=3

i=4

i=5
\
Using While:

Input:

>> % Ashfaq Cheema 09ES18

>> i=1:5;

>> while i<1

end

Output:

i=1 2 3 4 5

You can have nested for loops.

>>for m= 1 : 3

>> for n= 1 : 3

>> x(m,n) = m+n*i

>>end

>>end

Record the output of above program, what do you observe?

OUTPUT & COMMENTS:


IF-STATEMENT:

The "if" command lets you have programs that make decisions about what commands to
execute. The basic command looks like

>>a=5;

>>if a>0

>> x=a^2

>>end

This command will assign x to be the value of a squared, if a is positive. Again, note that it has
to have an end to indicate which commands are actually parts of the if. In addition, you can
define an else clause which is executed if the condition you gave the if is not true. We could
expand our example above to be

>>a= -5;

>>if a>0

>> x = a^2

>>else

>> x = -a^2

>>end
Try different values of a and note down your observation.

OUTPUT & COMMENTS:

Input:

>> % Ashfaq Cheema 09ES18

>> a=-2;

>> if a <0

a^2

else

x = -a^2

end

Output:

ans =

We can also have "if-else if" in MATLAB

>>if a>0

>> x = a^2

>>else if a = = 0
>> x= i

>>else

>> x = -a^2

>>end

>>end

Try different values of a and note down your observation. Why we are using two end
statements?

OUTPUT & COMMENTS:


COMMENTS:

Like any other programming language MATLAB also has provision to write comments.
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.

CONCLUSIONS:

Describe your conclusions based on the objectives of this lab session:

 This Lab session was very useful as a beginner for Matlab


 Lab provides basics of Matlab
 Using different features and the usefulness is discussed.

END

Vous aimerez peut-être aussi