L1 Intro Basic Operations
L1 Intro Basic Operations
EE 201
9/3/2024 C1 OE 1
Engineering problems and
Computer solutions Overview
• Engineers APPLY math and science for
the betterment of society through:
- Design - Manufacturing
- Research & Development - Management
- Continual Improvement
9/3/2024 C1 OE 2
Engineers have been involved in almost
everything you see, touch, or rely upon
9/3/2024 3
C1 OE
Engineering problems and
Computer solutions Overview
Engineers must analyze and solve a
wide range of technical problems.
Ranging from simple single-solution
problems, to open-ended ones
OEP will likely require a team of
engineers from several disciplines.
Some problems may have no clear
solution.
9/3/2024 C1 OE 4
Engineering problems and
Computer solutions Overview
SOME SAY ENGINEERING PROBLEM SOLVING
REQUIRES A COMBINATION OF SCIENCE AND ART:
SCIENCE ART
• Math • Judgment
• Physics • Experience
• Chemistry • Common-Sense
• Mechanics • Know-How
• Etc. • Etc.
9/3/2024 C1 OE 5
THE ENGINEERING METHOD
RECOGNIZE AND UNDERSTAND THE
PROBLEM
GATHER DATA (AND VERIFY ITS ACCURACY)
SELECT GUIDING THEORIES AND
PRINCIPLES
MAKE ASSUMPTIONS WHEN NECESSARY
WRITE AN ALGORITHM TO SOLVE THE
PROBLEM
SOLVE THE PROBLEM
VERIFY THE RESULTS
PRESENT THE SOLUTION
9/3/2024 C1 OE 6
Your problem
◼ Suppose you wanted to explain to
someone who can only understand
simple instructions:
◼ How to compute the gas mileage for a
car each time they filled up their tank ?.
9/3/2024 C1 OE 7
Instructions as simple as possible:
1. Read the current odometer value.
2. Retrieve the previous value from the glove
compartment.
3. Subtract the value obtained in step 2 from the value
obtained in step 1.
4. Fill up the tank.
5. Read the number of gallons pumped.
6. Divide the number obtained in step 3 by the number
obtained in step 5.
7. Display the number obtained in step 5 as the mileage.
8. Write the odometer value obtained in step 1 on a piece
of paper.
9. Store the paper from step 8 in the glove compartment.
10. Stop.
9/3/2024 C1 OE 8
A better way might be:
1. Read the current odometer value.
2. Write the odometer value obtained in step 1 on a piece of
paper.
3. Is there a previous odometer reading in the glove
compartment?
(a) If the answer is no,
(i) Store the paper from step 2 in the glove
compartment.
(ii) Stop.
(b) If the answer is yes, retrieve the previous value
from the glove compartment.
4. Store the paper from step 2 in the glove compartment.
5. Subtract the value obtained in step 3(b) from the value
obtained in step 1.
6. Fill up the tank.
7. Read the number of gallons pumped.
8. Divide the number obtained in step 5 by the number
obtained in step 7.
9. Display the number obtained in step 8 as the mileage.
10. Stop.
9/3/2024 C1 OE 9
What we did ?
first figure out the information that is available.
know the inputs available, and the outputs
required.
develop the procedure you would use to
calculate the gas mileage, (i.e. algorithm
development).
Finally, break down the procedure into simple
steps, or refine the algorithm so that someone
who understands only very simple instructions
will be able to carry out the procedure.
9/3/2024 C1 OE 10
Conclusion
The reason for selecting a very simple individual as
the recipient of our instructions is that in some
ways the computer is very simple indeed.
The computer is a very fast machine which is
highly accurate and has an extremely large
memory but no “understanding.”
It has a very limited set of instructions it
“understands,” and it follows these instructions
exactly and literally.
Hence, the need for instructions to be very precise.
9/3/2024 C1 OE 11
The previous example represents what
we called:
9/3/2024 C1 OE 12
A better way might be:
1. Read the current odometer value.
2. Write the odometer value obtained in step 1 on a piece of
paper.
3. Is there a previous odometer reading in the glove
compartment?
(a) If the answer is no,
(i) Store the paper from step 2 in the glove
compartment.
(ii) Stop.
(b) If the answer is yes, retrieve the previous value
from the glove compartment.
4. Store the paper from step 2 in the glove compartment.
5. Subtract the value obtained in step 3(b) from the value
obtained in step 1.
6. Fill up the tank.
7. Read the number of gallons pumped.
8. Divide the number obtained in step 5 by the number
obtained in step 7.
9. Display the number obtained in step 8 as the mileage.
10. Stop.
9/3/2024 C1 OE 13
Definition of Algorithm
9/3/2024 C1 OE 15
Use of the Algorithms:
•Form the basis of •solve mathematical
computer programming problems, such as finding
•Used to solve problems the optimal solution to a
ranging from simple and system of linear equations
complex or finding the shortest path
Computer in a graph.
Mathematics
Science
Artificial
•Machine learning & Deep Intelligence Operations •Algorithms are used to
learning & Data Research optimize and make decisions
Science in fields such as
•intelligent systems:
image recognition, transportation, logistics, and
natural language resource allocation
processing, and decision-
making.
Algorithms used also to analyze, process, and extract insights from large
amounts of data in fields such as marketing, finance, and healthcare.
9/3/2024 C1 OE 16
9/3/2024 C1 OE 17
How to express an
Algorithm?
•Here we express the •Here we express the •Form of annotations
Algorithm in the Algorithm by making and informative text
natural English a graphical/pictorial •written in plain
language. It is too representation of it. English
hard to understand It is easier to •similar to the real
the algorithm from it. understand than code but no syntax
Natural Language. like any of the
programming
languages.
•it can’t be compiled
or interpreted.
•Can be understood
by even a some
school-level
knowledge.
9/3/2024 C1 OE 18
How to Design an Algorithm?
9/3/2024 C1 OE 20
How to Design an Algorithm?
Example: Consider the example to add three
numbers and print the sum.
Step 2: Designing the algorithm
Now let’s design the algorithm with the help of the above pre-
requisites:
Algorithm to add 3 numbers and print their sum:
START
Declare 3 integer variables num1, num2, and num3.
Take the three numbers, to be added, as inputs in variables num1,
num2, and num3 respectively.
Declare an integer variable sum to store the resultant sum of the 3
numbers.
Add the 3 numbers and store the result in the variable sum.
Print the value of the variable sum
END
9/3/2024 C1 OE 21
How to Design an Algorithm?
Example: Consider the example to add three
numbers and print the sum.
Step 3: Testing the algorithm by implementing it.
To test the algorithm, let’s implement it (write a code) in any
programming language.
C++ , C , Java , Python3 , C# , Javascript , or …
What will be differ from each other is the syntax of each language
The Output screen the user will see of any code written with
any language should be like that :
Enter the 1st number: 0 The user will enter
Enter the 2nd number: 10 the three numbers
Enter the 3rd number: -15 when requested
9/3/2024 C1 OE 22
What is Algorithm complexity and
how to find it?
An algorithm is defined as complex based on
the amount of Space and Time it consumes.
Hence the Complexity of an algorithm refers to
the measure of the time that it will need to
execute and get the expected output, and the
Space it will need to store all the data (input,
temporary data, and output).
Hence these two factors define the efficiency of
an algorithm.
9/3/2024 C1 OE 23
MATLAB
9/3/2024 C1 OE 24
The Desktop of MATLAB
9/3/2024 C1 OE 25
The default MATLAB
Desktop
9/3/2024 C1 OE 26
The screen of MATLAB consist of four
windows. These are the :
-command window
-command history window
-current directory
-workspace
9/3/2024 C1 OE 27
Command window:
-used to type instructions of various
types called commands, functions and
statements.
-MATLAB displays the prompt >> to
indicate that it is ready to receive
instructions .
9/3/2024 C1 OE 28
Command history window:
-This window shows all the previous
keystrokes you entered in the
command window
-It is useful for keeping track of what you
typed
9/3/2024 C1 OE 29
Current Directory window:
-use it to access files
-Double clicking on a file name with the
extension .m will open that file in the
MATLAB editor
9/3/2024 C1 OE 30
Workspace window:
-displays the variables created in the
command window
9/3/2024 C1 OE 31
An Example interactive
(Calculator) Session
9/3/2024 C1 OE 32
Entering Commands and
Expressions
MATLAB retains your previous keystrokes.
Use the up-arrow ↑ key to scroll back & back
through the commands. Press the ↑ key once to
see the previous entry, and so on.
Use the down-arrow ↓ key to scroll forward.
Edit a line using the left ← - and right → -arrow
keys the Backspace key, and the Delete key.
Press the Enter ↵ key to execute the command.
You can see the previous keystrokes displayed
in the Command History window. Execute by
double click.
You can copy (highlighting & ctrl+c) a line from
Command history window, to Command window.
9/3/2024 C1 OE 33
The MATLAB Help System
To explore the more advanced features of MATLAB not covered in this course, you
will need to know how to use effectively the MATLAB.
The MATLAB has Help System to get help for using Math Works products.
MATLAB Help System consists of :
1-Help Browser
2-Help Functions
3-Other Resources
9/3/2024 C1 OE 34
Help Browser
9/3/2024 C1 OE 35
9/3/2024 C1 OE 36
The Help Browser contains two window “panes” :
a-Help Navigator
b-Display Pane.
lookfor topic: Displays in the Command window a brief description for all
functions whose description includes the specified key word topic.
doc funcname: Opens the Help Browser to the reference page for the
specified function funcname, providing a description, additional remarks,
and examples.
9/3/2024 C1 OE 38
9/3/2024 C1 OE 39
Scalar Arithmetic Operations
y = 2 x + 5 z − 400
9/3/2024 C1 OE 41
Solution:
y=2*x+5*z^0.5-400
OR
y=2*x+5*z^0.5-4e2
OR
y=2*x+5*z^0.5-4*10^2
9/3/2024 C1 OE 42
Example :
Number Exponent MATLAB
form form
789.34 7.8934×102 7.8934e2
0.0001 1×10-4 1e-4
4 4×100 4e0
400000000000 4×1011 4e11
9/3/2024 C1 OE 43
Order of precedence
Order of Operation
precedence
First Parentheses ( ), evaluated starting
with the innermost pair.
Second Exponentiation (power ) ^ , evaluated
from left to right.
Third Multiplication * and division / with equal
precedence, evaluated from left to right.
Fourth Addition + and subtraction - with equal
precedence, evaluated from left to right.
9/3/2024 C1 OE 44
Example
State the order of evaluation of the
operators in each of the following
MATLAB statement and show the value
of x after statement is performed
x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 4-1 ) ) ) ) ?
9/3/2024 C1 OE 45
Solution:
1) x=(3*9*(3+ ( 9 * 3 / ( 4-1 ) ) ) )
2) x=(3*9*(3+ (9*3/3)))
3) x=(3*9*(3+ ( 27 / 3 ) ) )
4) x=(3*9*(3+ 9))
5) x = ( 3 * 9 * 12 )
6) x = ( 27 * 12)
7) x=324
9/3/2024 C1 OE 46
The Assignment Operator
(Replacement)=
When you type x=3 in MATLAB, you tell MATLAB to assign
the value 3 to the variable x.
We can then type x = x + 2 in MATLAB . This assigns the
value 3 + 2 = 5 to x. But in algebra this implies that 0 =
2 ?!!!.
In algebra we can write x + 2 = 20, but in
MATLAB we cannot ?!!!.
In MATLAB the left side of the = operator must be a single
variable.
The right side must be a computable value.
z+5=y wrong
y=3;
z=5+y correct
9/3/2024 C1 OE 47
Variable Names
9/3/2024 C1 OE 48
Variable Names
Rules for declaring a variables in MATLAB:
a-Variable names must begin with a letter
c-MATLAB is case-sensitive
◼ Thus the following names represent 4 different variables:
speed Speed
SPEED Speed_1
9/3/2024 C1 OE 49
Note:
Avoid Using Function Names for Variables
When naming a variable, make sure you are not using a name
that is already used as a function name, either one of your
own M-file functions or one of the functions (Command) in
the MATLAB language. If you define a variable with a
function name, you will not be able to call that function until
you remove the variable from memory with the clear
function,
For example, if you enter the following command, you will
not be able to use the MATLAB help command until you
clear the variable with clear help`
help = 50;
9/3/2024 C1 OE 50
9/3/2024 C1 OE 51
Example
9/3/2024 C1 OE 53
Special Variables and
Constants
Command Description
9/3/2024 C1 OE 54
Some Commonly Used Mathematical Functions
Function in calculus MATLAB Syntax Note: The MATLAB
ex exp(x) trigonometric
functions use
x sqrt(x) radian measure
In x log(x)
To convert from
log10 x log10(x) Degree → Radian
use :
x abs(x)
180º → π
cos x cos(x)
Or use sind( )
sin x sin(x)
tan x tan(x) x = x^ y
y
cos-1 x acos(x)
sin-1 x asin(x)
tan -1 x atan(x)
9/3/2024 55
C1 OE
Examples
b- y= cos ( 4.12
2
) +100sin(90º)
6
9/3/2024 C1 OE 56
Solution:
( −2.1)3
x=e + 3.47 log 10 14 + 4 287
a- x=exp((-2.1)^3)
+3.47*log10(14)+(287)^(1/4)
4.12 2
y= cos ( 6
) +100sin(90º)
b- y=cos((4.12*pi/6)^2)+100*sin(pi/2)
9/3/2024 C1 OE 57
Expressing Function Arguments
y=sinx2 y=sin(x^2)
w=sin2 x w=(sin (x))^2 or w= sin(x)^2
z=sin(sqrt (x)+1)
9/3/2024 C1 OE 58
Some common mathematical functions
–2.5 –2 –2 –3
–1.75 –1 –1 –2
–1.25 –1 –1 –2
–0.5 0 0 –1
0.5 1 0 0
1.25 2 1 1
1.75 2 1 1
2.5 3 2 2
9/3/2024 C1 OE 60
Example:
9/3/2024 C1 OE 61
rand( )- To generate random real number(s)
9/3/2024 C1 OE 62
Dr. O. Elnokity
x= rand x= 0.6679
0.6035 0.7297
x= rand(2) x=
0.5261 0.7073
x= rand*(20-10)+10 x= 14.3570
13.1110 11.8482
x= rand(2)*(20-10)+10 x=
19.2338 12.5806
x= randi(100) x= 79
100 2
29 56
x= randi(100,2) x=
70 40
11 9
x= randi([5,20],3,2) x= 13 17
6 5
4. 2i 3
The solution
1. (3+6i)*(-7-9i) or (3+6*i)*(-7- 9*i)
2. (5+4i)/(5-4i) or (5+4*i)/(5-4*i)
3. 3/2*i or 3i/2
4. 3/(2*i) or 3/2i
9/3/2024 C1 OE 65
note that i should not be defined as a
variable, if so then don’t give *
before i.
for example, if you define i=5 then
x=3+6i will be written as x=3+6*i
(3+6*5=33) (not x=3+6i, it will give
you x = 3.00 + 6.00 i).
9/3/2024 C1 OE 66