0% found this document useful (0 votes)
18 views66 pages

L1 Intro Basic Operations

Uploaded by

turkisaad65
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views66 pages

L1 Intro Basic Operations

Uploaded by

turkisaad65
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

Using computers in engineering

problems solutions, and Matlab


basic operations

Lecture notes, based on “Introduction to MATLAB


for Engineers”, William Palm, 3rd edition, and other
textbooks & resources

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

• Above all, engineers are problem solvers


who make things work better, more
efficiently, quicker and cheaper.

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

◼ Share opinions ???!!!

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

The word Algorithm means ” A set of


finite rules or instructions to be followed
in calculations or other problem-solving
operations ”
Or
” A procedure for solving a
mathematical problem in a finite
number of steps that frequently involves
recursive operations”.
9/3/2024 C1 OE 14
refers to a
sequence of finite
steps to solve a
particular problem.

Algorithms can be simple


and complex depending on
what you want to achieve.

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.

Natural Flow Pseudo


Language chart code

9/3/2024 C1 OE 18
How to Design an Algorithm?

To write an algorithm, the following things are needed as a


pre-requisite:
 The problem that is to be solved by this algorithm i.e.
clear problem definition.
 The constraints of the problem must be considered
while solving the problem.
 The input to be taken to solve the problem.
 The output is to be expected when the problem is
solved.
 The solution to this problem is within the given
constraints.
 Then the algorithm is written with the help of the above
parameters such that it solves the problem.
9/3/2024 C1 OE 19
How to Design an Algorithm?
Example: Consider the example to add three
numbers and print the sum.
Step 1: Fulfilling the pre-requisites
As discussed above, to write an algorithm, its prerequisites must be
fulfilled.
The problem that is to be solved by this algorithm: Add 3 numbers
and print their sum.
The constraints of the problem that must be considered while
solving the problem: The numbers must contain only digits and no other
characters.
The input to be taken to solve the problem: The three numbers to be
added.
The output to be expected when the problem is solved: The sum of
the three numbers taken as the input i.e. a single integer value.
The solution to this problem, in the given constraints: The solution
consists of adding the 3 numbers. It can be done with the help of the ‘+’
operator, or bit-wise, or any other method.

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

Sum of the 3 numbers is: -5

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

 The name of MATLAB stands for


matrix laboratory.
 Starting a MATLAB Session
-On Microsoft® Windows® platforms,
start the MATLAB program by double-
clicking the MATLAB R2009a shortcut
on your Windows desktop.
-Or from Start –All programs-Matlab

9/3/2024 C1 OE 24
The Desktop of MATLAB

 When you start MATLAB, the desktop


appears, containing tools (graphical
user interfaces) for managing files,
variables, and applications associated
with 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

Note: MATLAB uses high precision for its


computations. But by default it usually displays its
results using four decimal places( short format )

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

 The Help Browser enables you to search and


view documentation for MATLAB and other
Math Works products.

 Help menu→ Product Help or click the question


mark button in the toolbar

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.

 The Help Navigator contains four tabs:

1-Contents: a contents listing tab.


2-Index:a global index tab.
3- Search: a search tab having a find function and full text search features.

4- Demos: a bookmarking tab to start built-in demonstrations.


9/3/2024 C1 OE 37
Help Functions

 help funcname: Displays in the Command window a description of the


specified function funcname.

 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

Name of Operation in Operation Symbol


operation Calculus in MATLAB
Multiplication ab ( a × b) a*b *
Right division a
b
(a÷b) a/b /
Addition a+b a+b +
Subtraction a-b a-b -
exponentiation ab a^b ^
Power of 10 10x 1ex e
9/3/2024 C1 OE 40
Examples:
1) Write MATLAB expressions for the
following algebraic expression

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

 Variables are symbolic names that represent


locations in the computer’s random access
memory (RAM)

 To use a variables must declare it by telling the


compiler its name and value. This is called a
variable declaration.

 Variable declaration tells the compiler to allocate


appropriate memory space for the variables.

9/3/2024 C1 OE 48
Variable Names
 Rules for declaring a variables in MATLAB:
a-Variable names must begin with a letter

b-The rest of name can contain letters, digits and underscore


characters

c-MATLAB is case-sensitive
◼ Thus the following names represent 4 different variables:
speed Speed
SPEED Speed_1

d-Variables names must contain less than 63 characters

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

 which of the following are legitimate


(valid) variable names in MATLAB?
Explain why invalid?
A- Global
B- My_var
C- _input
D- X+Y
E- Example1-1
9/3/2024 C1 OE 52
 Answer:
a-legitimate (valid)
b-legitimate (valid)
c-invalid because begins with a
underscore
d-invalid because contains symbol +
e-invalid because contains symbol –

9/3/2024 C1 OE 53
Special Variables and
Constants
Command Description

ans Temporary variable containing the most


recent answer.
pi The number π=3.141592653589793....

Inf Infinity ( ∞ ) (example: 7/0).


NaN Indicates an undefined numerical result
(Not a Number), (example: 0/0).

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

Write MATLAB expressions to calculate the


following algebraic expressions:
( −2.1)3
a- x=e + 3.47 log 10 14 + 4 287

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

We can write sin 2 in text ,but MATLAB requires parentheses


surrounding the 2 (which is called the function argument or
parameter) Thus to evaluate sin 2 in MATLAB, we type sin(2).The
MATLAB function name must be followed by a pair of parentheses
that surround the argument.
Example :

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

ceil(x) Round to nearest integer toward ∞.

fix(x) Round to nearest integer toward zero.

floor(x) Round to nearest integer toward -∞.

round(x) Round toward nearest integer.

sign(x) +1 if x > 0;0 if x=0;-1 if x<0


9/3/2024 C1 OE 59
a ceil(a) fix(a) floor(a)

–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)

randi ( )- To generate random integer(s)


rand, Generates one random real number from the interval (0,1).
rand(N), Generates N-by-N matrix of random real numbers from
the interval (0,1).

- To generate random real numbers from the interval (a, b).

rand *(b-a)+a, Generates one real number between a to b.

rand(N)*(b-a)+a, Generates N by N matrix of real number


between 5 to 10.

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= rand(1,2)*(20-10)+10 x= 15.9490 12.6221


September 24 L2: Basic operations in Matlab 63
randi(P), Generates one random integer between 1 and P.
randi(P,N), Generates an N-by-N matrix of random integers between 1 and P.
randi([a,b],m,n), Generates an m-by-n matrix of random integers between a and b

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

September 24 L2: Basic operations in Matlab 64


Complex numbers
1. (3 + 6i)(−7 − 9i)
5 + 4i
2. 5 − 4i
3. 2 or 2
3
i
3i

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

You might also like