Lab No. 1: Swedish College of Engineering & Technology, Wah Cantt

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Swedish College of Engineering & Technology, Wah Cantt.

(EE-313) Fall 2017 (Sept. 20, 2017)


Signals & Systems Lab (Semester V) Elect. / 2k15

Roll No.:________________________
Teacher’s Initials: ________________
Marks Obtained: _________________

Lab No. 1
Introduction of MATLAB
MATLAB is an interactive software which has been used recently in various area of
engineering and scientific applications. It is not a computer language in the normal
sense but it does most of the work of a computer language. Writing a computer code
is not a straightforward job. Typically boring and time consuming for beginners. One
attractive aspect of MATLAB is that relatively easy to learn. It is written on an
intuitive basis and it does not require in depth knowledge of operational principles
of computer programming like compiling and linking in most programming
languages. This could be regarded as a disadvantage since it prevents users from
understanding the basic principles in computer programming. The interactive mode
of MATLAB may reduce computational speed in some applications.

The power of MATLAB is represented by the length and simplicity of the


code. For example, one page of MATLAB code may be equivalent to many pages
of other computer language source codes. Numerical calculation in MATLAB uses
collections of well-written scientific/mathematical subroutines such as LINPACK

1|Page SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
and EISPACK. MATLAB provides Graphical User Interface (GUI) as well as three-
dimensional graphical animation.

In general, MATLAB is a useful tool for vector and matrix manipulations.


Since the majority of the engineering systems are represented by matrix and vector
equations, we can relieve our workload to a significant extent by using MATLAB.
The finite element is a well-defined candidate for which MATLAB can be very
useful as a solution tool. Matrix and vector manipulations are essential parts in the
method. MATLAB provides a help menu so that we can type the help command
when we need help to figure out a command. The help utility is quite convenient for
both beginners and experts.

USING MATLAB:-
Either MATLAB must be installed on your computer or you must have access to a
network where it is available. Throughout this Lab Fall 2017. The latest version at
the time of writing is assumed (Version 2007)

To start from Windows, double-click the MATKAB icon on your Windows


desktop. The MATLAB desktop open as shown in Figure 1.1. The window in the
desktop that concerns us for now is the command window, where the special >>
prompt appears. This prompt means that MATLAB is waiting for a command. You
can quit at any time with one of the following ways:

 Select Exit MATLAB form the desktop File menu.


 Enter quit or exit at the command window prompt.

Do not click on the X (close box) in the top right corner of the desktop. This does
not allow MATLAB to terminate properly and, on rare occasions, may cause
problems with your operating software.

2|Page SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
Once you have stated MATLAB, experiment with it in the command window. If
necessary, make the command window active by clicking anywhere inside its
border.

Figure 1.1: MATLAB Desktop

Arithmetic:-
Since we have experience doing arithmetic, we want to examine if MATLAB does
it correctly. This is a required step to gain confidence in any tool and in our ability
to use it.

Type 2+3 after >> prompt, followed by Enter (press the Enter key) as indicated by
<Enter>

3|Page SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
>>2+3 <Enter>

Answer:

>> 3-2 <Enter>

Answer:

>> 3*2 <Enter>

Answer:

>> 3/2 <Enter>

Answer:

>> 3\2 <Enter>

Answer:

What about *, / & \ mean?

4|Page SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
Yes, they are multiplication, division.

There are some hints on creating and editing command lines:

 The line with the >> prompt is called the command line.
 You can edit a MATLAB command before pressing Enter by using various
combinations of the Backspace, Left Arrow, Right Arrow and Del Keys. This
helpful features is called command line editing.
 You can select (and edit) commands you have entered using Up-Arrow and
Down Arrow. Remember to press Enter to have the command carried out
(i.e., to run or to execute the command).
 MATLAB has a useful editing features called smart recall. Just type the first
few characters of the command you want to recall. For Example, Type the
character 2* and press the Up Arrow key—this recalls the most recent
command starting with 2*.

How do you think MATLAB would handle 0/1 and 1/0? Try it. MATLAB is sensible
about anticipating some errors; it warns you in case you didn’t realize you were
dividing by zero, but still gives the answer Inf. If you insist on using ∞ in a
calculation, which you may legitimately wish to do, type the symbol Inf. (short for
infinity). Try 13+Inf. and 29/Inf.

Another special value that you may meet NaN, which stands for Not a Number. It
is the answer to calculation like 0/0.

Variables:-
Now we will assign values to variables to do arithmetic operations with the variables.
First enter the command (statement in programming jargon) a=2. The MATLAB
command line should look like this:

5|Page SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
>> a = 2 <Enter>

The a is variable. This statement assigns the value of 2 to it. (Note that this value is
displayed immediately after the statement is executed). Now try entering the
statement a = a + 7 followed on a new line by a = a*10. Do you agree with the final
value of a? Do we agree that it is 90?

Now Enter the statement:

>> b = 3; <Enter>

The semicolon (;) prevents the value of b from being displayed. However, b still has
the value 3, as you can see by entering without a semicolon:

>> b (Enter)

Assign any value you like to two variables x and y. Now see if you can assign the
sum of x and y to a third variable z in a single statement. One way of doing this is

>> x = ________ ; & y = ________ ;

>> z = x + y

Answer:

Notice that, in addition to doing the arithmetic with variables with assigned values,
several commands separated by semi colons (or commas) can be put on one line.

 a = ______ , b = ______ , c = a+b


 a = ______ ; b = ______ ; c = a+b

6|Page SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
Variables are fundamental to programming. In a sense, the art of programming is
this:

Getting the right values in the right variables at the right time

A Variable name (like the variable balance that we used as in Lab 1) must comply
with the following two rules:

 It may consist only of the letters a-z, the digits 0-9 and the Underscore ( _ )
 It must start with a letter.

Mathematical Functions:
MATLAB has all of the usual mathematical function found on a scientific electronic
calculator, like Pi, exp (exponential), x^ (power), sqrt(square root) and log etc.,

>>pi

Answer:

>> exp(2)

Answer:

>> a=2; x^2

Answer:

7|Page SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
>> Hamza = 9; sqrt(Hamza) Remember That (MATLAB is case Sensitive)

If you write H in place h Than MATLAB gives you error. Check for answer if hamza
than what happen?

Answer:

Note that clear executed by itself clear all local variables in workspace.

>> clear all

Answer:

What happen when you enter clc and clear pi

Comments on its answer

Answer:

ARRAYS: VECTORS AND MATRICES


A vector is a special type of matrix, having only one row or one column, Vectors are
called lists or arrays in other programming languages. If you haven’t come across
vectors officially yet, don’t worry – just think of them as lists of numbers

8|Page SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
MATLAB handles vectors and matrices in the same way, but since vectors are easier
to think about matrices, we will look at them first. This will enhance your
understanding and appreciation of many aspects of MATLAB. As mentioned above,
MATLAB refers to scalars, vectors, and matrices generally as arrays. We will also
use the term array generally, with vector and matrix referring to the one-dimensional
(1D) and two-dimensional (2D) array forms.

Vectors:
Variables such as a and b above are called scalars, they are single-valued. MATLAB
also handles vectors (generally referred to as arrays), which are the key to many of
its power features. The easiest way of defining a vector where the elements
(components) increase by the same amount is with a statement like

>> a = [1 2 3 4 5]

Answer:

>> a = [1,2,3,4,5]

Answer:

>> a = [1;2;3;4;5]

Answer:

9|Page SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
If we write this command, than

>> a = [1:5]

Answer:

>> a= [5:1]

Answer:

>> a=[1:1:5]

Answer:

>> s = [1:2:5];

Answer:

>> s = [1:3:5]

Answer:

10 | P a g e SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
That is a colon ( : ) between the 1:5. There’s no need to leave a space on either side
of it, except to make it more readable. Enter a or s to check that it is a vector. It is
row vector consisting of one row and five column.

Type the following command to verify that this the case

>> size (a) if a = 10; Press < Enter >

Part of the real power of MATLAB is illustrated by the fact that other vectors can
now be defined (or created) in terms of the just defined vector a. Try

>> who (a) ;

Answer:

>> whos (a)

Vectors: Explicit Lists


These are all examples of the explicating list method of initializing vectors. (You
won’t need reminding about the command prompt >> anymore, so it will no longer
appear unless the context absolutely demands it.)

Enter a statement like

x = [1 3 0 -1 5]

Answer:

11 | P a g e SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
Can you see that you have created a vector (list) with five elements? (Make sure to
leave out the semicolon so that you can see the list. Also, make sure you hit Enter to
execute the command.)

Enter the command disp (x) to see how MATLAB displays a vector.

Enter the command whos. Under the heading Size you will see that x is 1 by 5, which
means 1 row and 5 columns. You will see that the total number of element is 5.

You can use commas instead of spaces between vector elements if you like. Try this:

a = [5,6,7]

Answer:

Don’t forget the commas (or Spaces) between elements; otherwise, you could end
up with something quite different:

x = [130-15]

Answer:

What do you think this gives?

You can use one vector in a list for another one. Type in the following:

a= [1 2 3];

Answer:

12 | P a g e SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
b= [4 5];

Answer:

c= [a -b];

Answer:

Can you work out what c will look like before displaying it?

And what about this?

A= [1 3 7];

Answer:

A=[a 0 -1];

Answer:

Enter the following

13 | P a g e SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
X= []

Answer:

Note in the workspace browser that the size of x is given as 0 by 0 because x is


empty. This means x is defined and can be used where an array is appropriate without
causing an error; however, it has no size or value.

Making x empty is not the same as saying x=0 (in the latter case x has size 1 by 1)
or clear x (which removes x from the workspace, making it undefined).

An empty array may be used to remove elements from an array

Remember the following important rules:

 Elements in the list must be enclosed in square brackets, not parentheses.


 Elements in the list must be separated either by spaces or by commas.

Initializing Vectors: The Colon Operator


A vector can be generated (Initialized) with the colon operator.

Enter the following statements:

x=1:10

(Elements are the integer 1,2,…….10)

Answer:

14 | P a g e SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
x=1:0.5:4

Answer:

(Elements are the value 1, 1.5,……, 4) in increment of 0.5. Note that if the colon
separate three values, the middle value is the increment);

x=10:-1:1

Answer:

(Elements are the integer 10,9,……..,1) since the increment is negative);

x=[1:2:6]

Answer:

(Elements are 1,3,5; Note that when the increment is positive but not equal to 1, the
last element is not allowed to exceed the value after the second colon);

x=[0:-2:-5]

Answer:

15 | P a g e SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
Elements are 0,-2,-4; Note that when the increment is negative but not equal to -1,
the last element is not allowed to be less than the value after the second colon);

x= 1:0

(a complicated way of generating an empty vector!)

Answer:

Transposing Vectors
All of the vectors examined so far are row vectors. Each has one row and several
columns. To generate the column vectors that are often needed in mathematics, you
need to transpose such vectors—that is, you need to interchange their rows and
columns. This is done with the single quote, or apostrophe (‘), which is the nearest
MATLAB can get to the mathematical prime (‘) that is often used to indicate the
transpose.

Enter x= 1:5 and then enter x’ to display the transpose of x. Note that x itself remains
a row vector. Alternatively, or you can create a column vector directly:

y=[1 4 8 0 -1]’

Answer:

Matrices

16 | P a g e SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
A matrix may be thought of as a table consisting of rows and columns. You create a
matrix just as you do a vector, except that a semicolon is used to indicate the end of
a row. For Example, the statement

a=[1 2 3;4 5 6]

and result in

Answer:

A matrix may be transposed. With a initialized as above, the statement a’ result in

Answer:

Linear Equations:
System of linear equations are very important in engineering and scientific analysis.
A simple example is finding the solution to two simultaneous equations:

𝑥 + 2𝑦 = 4

2𝑥 − 𝑦 = 3

Matrix Method: Type the following commands (exactly as they are):

>> a = [1 2; 2 -1]; < Enter >

>> b = [4; 3]; < Enter>

The Result is:

17 | P a g e SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
Answer:

As we know that from the following relation:

AX=B

X=A-1B

So in MATLAB, we have

x=inv(A)*B

Answer:

i.e., x = ____ , y = ____

Lab Task (1)


Evaluate the following expression yourself (before you use MATLAB to check),
The Numerical answers are in parentheses.

(a). 2 / 2 * 3(3)

(b). 2 / 3 ^ 2(2/9)

(c). (2 / 3) ^ 2(4/9)

(d). 2 + 3 * 4 – 4(10)

(e). 2 ^ 2 * ¾ + 3(6)

(f). 2 ^ 3 ^ 2(64)

18 | P a g e SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
(g). 2 * 3 + 4(10)

(h). 2 ^ 3 ^ 2(64)

(i). -4 ^ 2(-16)

Lab Task (2)


Use MATLAB to evaluate the following expressions?

(a). √2
3+4
(b).
5+6

(c). Find the Sum of 5 and 3 divided by their product.

(d). Find the sum of 15 and 5 divided by their product.


2
(e). 23

(f). Find the square of 2𝜋.

(g). 2𝜋 2

(h). 1 / √2𝜋

(i). Find the cube root of the product of 2.3 and 4.5
2
1−
3+2
(j). 2
1+
3+2

1
(k).
2 √𝜋

(l). 1000 ( 1 + 0.15/12)60

(m). (0.0000123 + 5.678 × 10−15 )

19 | P a g e SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
Lab Task (3)
Set up a vector n with elements 1,2,3,4,5. Use MATLAB array operations on it to
set up the following four vectors, each with five elements:

(a). 2, 4,6,8,10

(b). ½, 2, 3/2, 2, 5/2.

(c). 1,1/2,1/3,1/4,1/5
1 1 1
(d) 1, 1/22 , , ,
32 42 52

Suppose vectors a and b are defined as follow:

𝑎 = [2 − 1 5 0]

𝑏 = [3 2 − 1 4]

Evaluate by hand and in MATLAB the vector c in following statements.

(a). c = a-b

(b). c = b + a -3;

(c). c = 2 * a + a .^b;

(d). c = b ./ a;

(e). c = b . a;

(f). c = a . ^b;

(g). c = 2. ^ b+a;

(h). c = 2*b/3.*a;

(i). c = b*2.*a;

20 | P a g e SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
Lab Task (4)
(a). Water freezes at 32° and boils at 212° on the Fahrenheit scale. If C and F are
Celsius and Fahrenheit temperatures, the formula

F = 9C/5 +32
Coverts from one to the other. Use the MATLAB command line to convert Celsius
37° (normal human temperature) to Fahrenheit?

(b). Engineers often have to convert from one unit of measurement to another, which
can be tricky sometimes. You need to think through the process carefully. For
Example, Convert 5 acres to hectares, given that an arce is 4840 square yards, a yard
is 36 inches. An inch is 2.54 centimeter, and a hectare is 1000 square meter. The best
approach is to develop a formula to convert x acres to hectares.

 One Square Yard = (36 * 2.54)2 cm2


 So one acre = 4840 * (36 * 2.54)2 cm2
= 0.4047 * 108 cm2
= 0.4047 hectares
 So x acres = 0.4047 x hectares

Once you have found the formula, MATLAB can do the rest:

x = 5;

h = 0.4047 * x

disp (h)

Lab Task (5)

21 | P a g e SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)
Develop formula for the following conversions and use some MATLAB statement
to find the answers?

(a). Convert 22 yards (an imperial cricket pitch) to meters.

(b). One Pound (weight) = 454 grams. Convert 75 kilograms to pounds.

(c). Convert 49 meters/seconds (terminal velocity for a falling human-shaped object)


to kilometer per hour.

(d). One atmosphere pressure = 14.7 pounds per square inch (psi) = 101.325 kilo
Pasclas (kPa). Convert 40 psi to kPa.

(e). One calorie = 4.184 joules. Convert 6.25 kilojoules to calories

22 | P a g e SCET, Wah Cant.


Semester V S &S Lab (WM) EED (Fall 2017)

You might also like