Professional Documents
Culture Documents
I.T in Engineering: Abdullahi - Sada@bazeuniversity - Edu.ng
I.T in Engineering: Abdullahi - Sada@bazeuniversity - Edu.ng
I.T in
engineering
EEE308
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
GEC214 - Lecture 3 4
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
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
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
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 ≤ 𝑥 ≤ 𝑝.
GEC214 - Lecture 3 17
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja
GEC214 - Lecture 3 18
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja
GEC214 - Lecture 3 19
By Abdullahi Yusuf Sada
Faculty of Engineering Electrical Engineering Department Baze University Abuja
• 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
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
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