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

Faculty of Engineering Electrical Engineering Department Baze University Abuja

I.T in
engineering
EEE308

Abdullahi Yusuf Sada


Email: Abdullahi.sada@bazeuniversity.edu.ng
Office: D31

By Abdullahi Yusuf Sada


Faculty of Engineering Electrical Engineering Department Baze University Abuja

matlab

GEC214 - Lecture 3 2
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

SImple math
• Just like a calculator, MATLAB can do basic math.
• Consider the following simple example: Mary goes to the office
supply store and buys five pens at 30 cents each, seven notebooks at
60 cents each, and one pair of scissors for 70 cents. How many items
did Mary buy, and how much did they cost?
• Solution
• To solve this problem with a calculator, you enter
Items:5 + 7 + 1 = 13 items
Cost: 5 × 30 + 7 × 60 + 1 × 70 = 640 cents

GEC214 - Lecture 3 3
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

• In MATLAB, this problem can be solved in a number of different ways.


First, the calculator approach can be taken:
>> 5 +7+1
ans =
13
>> 5*30 + 7*60 + 1*70
ans =
640

GEC214 - Lecture 3 4
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

• As an alternative, the problem can be solved by storing information in MATLAB


variables:
>> pens = 5
pens =
5
>> notebooks = 7
notebooks =
7
>> scissors = 1;
>> items = pens + notebooks + scissors
items =
13
>> cost = pens*30 + notebooks*60 + scissors*70
cost =
640
GEC214 - Lecture 3 5
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

• Because MATLAB remembers things, let’s ask what the average cost per
item was:
>> average_cost = cost/items
average_cost =
49.2308
• Since the term average cost is two words and MATLAB variable names
must be one word, an underscore was used to create the single MATLAB
variable average_cost.

GEC214 - Lecture 3 6
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

Naming variables

GEC214 - Lecture 3 7
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

COMMENTS, PUNCTUATION, AND


ABORTING EXECUTION
• Commas tell MATLAB to display results; semicolons suppress printing.
For Example;
>> pens = 6, notebooks = 6; scissors = 2
pens =
6
scissors =
2
Sometimes, expressions or commands are so long that it is convenient
to continue them onto additional lines.
GEC214 - Lecture 3 8
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

• In MATLAB, statement continuation is denoted by three periods in


succession, as shown in the following code:
>> average_cost = cost/items % command as done earlier
average_cost =
44.7059
>> average_cost = cost/ . . . % command with valid continuation
items
average_cost =
44.7059
>> average_cost = cost . . . % command with valid continuation
/items
average_cost =
44.7059
GEC214 - Lecture 3 9
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

>> average_cost = cost . . . command with valid continuation (no % needed)


/items
average_cost =
44.7059
>> average_cost = cost/it . . . % command with Invalid continuation
ems
??? ems
Error: Missing MATLAB operator.

• Finally, MATLAB processing can be interrupted at any time by pressing


Control-C (i.e., pressing the Ctrl and C keys simultaneously).
GEC214 - Lecture 3 10
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

MATHEMATICAL FUNCTIONS
• Lists of the common functions that MATLAB support are shown in the tables at
the end of this chapter. Most of these functions are used in the same way you
would write them mathematically:
>> x = sqrt(3)/3
x=
0.5773
>> y = asin(x)
y=
0.6154
>> y_deg = y*180/pi
y_deg =
35.2608

GEC214 - Lecture 3 11
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

• Note, again, that MATLAB uses radians, not degrees, in trigonometric functions.
Other examples include the following:
>> y = sqrt(3^2 + 4^2) % show 3-4-5 right triangle relationship
y= 5

>> y = rem(23,4) % remainder function, 23/4 has a remainder of 3


y= 3

>> x = 2.6, y1 = fix(x), y2 = floor(x), y3 = ceil(x), y4 = round(x)


x= 2.6000
y1 = 2
y2 = 2
y3 = 3
y4 = 3
GEC214 - Lecture 3 12
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

GEC214 - Lecture 3 13
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

GEC214 - Lecture 3 14
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

GEC214 - Lecture 3 15
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

Arrays and Array Operations


• All of the computations considered to this point have involved single
numbers called scalars.
• Operations involving scalars are the basis of mathematics.
• At the same time, when we wish to perform the same operation on
more than one number at a time, performing repeated scalar
operations is time consuming and cumbersome.
• To solve this problem, MATLAB defines operations on data arrays.

GEC214 - Lecture 3 16
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

Simple array
• Consider the problem of computing values of the sine function over
one half of its period, namely, y = sin(x) over 0 ≤ 𝑥 ≤ 𝑝.

• Perhaps you would write the arrays in an organized fashion, as


follows:

GEC214 - Lecture 3 17
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

• MATLAB handles arrays in a straightforward, intuitive way. Creating arrays


is easy—just follow the preceding visual organization to create the
following array:

GEC214 - Lecture 3 18
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

ARRAY ADDRESSING OR INDEXING


• In MATLAB, individual array elements are accessed by using
subscripts; for example, x(1) is the first element in x, x(2) is the
second element in x, and so on.
• The following code is illustrative:

GEC214 - Lecture 3 19
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

• To access a block of elements at one time, MATLAB provides colon notation:

• These are the first through fifth elements in x. The notation 1:5 says, start
with 1 and count up to 5. The code

• starts with the seventh element and continues to the last element. Here, the
word end signifies the last element in the array x.

GEC214 - Lecture 3 20
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

• In the code

• the results contain the third, second, and first elements in reverse order. The
notation 3:–1:1 says, start with 3, count down by 1, and stop at 1. Similarly,
the results in the code

• consists of the second, fourth, and sixth elements in x. The notation 2:2:7
says, start with 2, count up by 2, and stop when you get to 7. (In this case
adding 2 to 6 gives 8, which is greater than 7, and so the eighth element is not
included.)
GEC214 - Lecture 3 21
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

ARRAY CONSTRUCTION
• Earlier, we entered the values of x by typing each individual element
in x. While this is fine when there are only 11 values in x, what if there
were 111 values? Two other ways of entering x are as follows:

GEC214 - Lecture 3 22
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

• In the first case, the colon notation (0:0.1:1) creates an array that starts at 0,
increments (or counts) by 0.1, and ends at 1. Each element in the array is
then multiplied by p to create the desired values in x.
• In the second case, the MATLAB function linspace is used to create x. This
function’s arguments are described by
linspace(first_value,last_value,number_of_values)
GEC214 - Lecture 3 23
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

• Both of these array creation forms are common in MATLAB. The colon
notation form allows you to directly specify the increment between data
points, but not the number of data points. Using linspace, on the other
hand, allows you to directly specify the number of data points, but not the
increment between the data points.
• For the special case where a logarithmically spaced array is desired, MATLAB
provides the logspace function:

GEC214 - Lecture 3 24
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

• Here, we created an array starting at 100 , ending at 102 , and containing 11


values. The function arguments are described by
logspace(first_exponent,last_exponent,number_of_values)

• Although it is common to begin and end at integer powers of 10, logspace


works equally well when nonintegers are used as the first two input
arguments.

GEC214 - Lecture 3 25
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

GEC214 - Lecture 3 26
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

ARRAY ORIENTATION
• In the preceding examples, arrays contained one row and multiple columns.
As a result of this row orientation, the arrays are commonly called row
vectors. It is also possible for an array to be a column vector, having one
column and multiple rows.
• The most straightforward way to create a column vector is to specify it,
element by element, and by using semicolons to separate values:

GEC214 - Lecture 3 27
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

• To create a column vector using the colon notation start:increment:end or


the functions linspace and logspace, you must transpose the resulting row
into a
• column by using the MATLAB transpose operator ('). For example,

GEC214 - Lecture 3 28
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

GEC214 - Lecture 3 29
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja

GEC214 - Lecture 3 30
By Abdullahi Yusuf Sada

You might also like