You are on page 1of 42

1

Alexandria University
Faculty of Engineering

Matlab

Edit by:Eng-Abdelrahman Mohamed Farghly


Abdelrahman Mohamed fargly
2

Contents
➢Introduction
➢Matrices
▪ Numerical matrixes
➢Elementary Math
▪ Math Functions
➢Graphics Fundamentals
▪ 2D plotting
▪ 3D plotting
▪ Subplots
➢Script Files
➢M-file Programming
➢Flow Control Statements
Abdelrahman Mohamed fargly
3

➢Matlab Simulink
➢Graphical User Interface (GUI)

--------------------------------------------------------------
for any extra information contact math works

Abdelrahman Mohamed fargly


4

Alexandria University
Faculty of Engineering

Introduction

Abdelrahman Mohamed fargly


5

• MATLAB Name origin:

➢Matlab stands for matrix Laboratory

Abdelrahman Mohamed fargly


6

• What is MATLAB?

Abdelrahman Mohamed fargly


7

Abdelrahman Mohamed fargly


8

Installing matlab

o First install matlab to make a practice

Abdelrahman Mohamed fargly


9

• Desktop Tools (Matlab)


• Command Window
type commands

• Workspace
▫ view program variables
▫ clear to clear
▫ double click on a variable to see it in the Array Editor

• Command History
▫ view past commands
▫ save a whole session using diary

• Launch Pad
▫ access tools, demos and documentation

Abdelrahman Mohamed fargly


10

Change the current


directory to the
location of your
Matlab file at
startup
Workspace

Command
History Command Window
Abdelrahman Mohamed Farghly
11

Abdelrahman Mohamed fargly


12

Workspace Interface
⚫ The workspace provides a summary of the
variables

Abdelrahman Mohamed fargly


13

Command History Interface


⚫ The Command History stores all the commands
entered in the Command Window previously
⚫ No history of .m-files is shown here

Abdelrahman Mohamed fargly


14

Notes before starting


✓MATLAB is case-sensitive
✓ A variable name should begin with a letter
✓ When you save a file care for its name:
✓ No spaces
✓ Start with a letter
✓ Use only letters, numbers or underscores

Abdelrahman Mohamed fargly


15

• Changing Font Size :

Abdelrahman Mohamed fargly


16

• Changing color:

Abdelrahman Mohamed fargly


17


Syntax Errors
⚫ All errors are given in the Command Window in
red
⚫ Nature of error is explained

Abdelrahman Mohamed fargly


18

❑Clear command window

▪ Clc

❑To clear workspace

▪ clear all

❑To clear variable

▪ Clear “ variable”

Abdelrahman Mohamed fargly


19

➢X=5 command

➢X=5;

to return to before command or after


click in keyboard

Abdelrahman Mohamed fargly


20

➢who command

➢whos

Abdelrahman Mohamed fargly


21

Basic Math Operations


➢To compute a math expression composed of numbers
and mathematics operators
➢+ addition
➢- subtraction
➢* multiplication
➢/ division
➢^ power

Compute (2+3-9)*7^2/4
ans
=-49

Abdelrahman Mohamed fargly


22

Example

Abdelrahman Mohamed fargly


23

Arithmetic operation:
» -5/(4.8+5.32)^2
ans =
-0.0488
» (3+4i)*(3-4i)
ans =
25
» cos(pi/2)
ans =
6.1230e-017
» exp( acos(0.3))
ans =
3.5470

Abdelrahman Mohamed fargly


24

Complex number:
» X=3+4i
real(x)
ans =
3
» Imag(x)
ans =
4
» Abs(x)
ans =
5
» Angle(x)
ans =
0.9273

» Conj(x)
Ans =
3-4i

Abdelrahman Mohamed fargly


25

Trigonometric
➢Example:
➢ compute the difference
Sin(pi/2) and Sind(90)

▪ First for radian


▪ Second for degree

▪ More example :
▪ sin(pi/4) ,Sind(45)
▪ asin(1) and a Sind(1)
▪ Sec (x),cosec(x),cot(x),tan(x),…….ete

Abdelrahman Mohamed fargly


26

Abdelrahman Mohamed fargly


27

General Math Expressions


• To compute a math expression composed of
numbers, variables and mathematics operators
• Define all variables
• The others are the same
• E.g. x=3;y=6;x+y=? It is equivalent to 3+6

Abdelrahman Mohamed fargly


28

Calculations at the Command Line


Assigning Variables
» a = 2; Semicolon
» b = 5; suppresses
screen output
» a^b
ans = Results
32 assigned to
» x = 5/2*pi; “ans” if name
not specified
» y = sin(x)
y =
1
» z = asin(y) () parentheses for
z = function inputs
1.5708

A Note about Workspace:


All angles are in radians Abdelrahman Mohamed fargly
29

Abdelrahman Mohamed fargly


30

Abdelrahman Mohamed fargly


31

Round-off functions

Abdelrahman Mohamed fargly


32

Relational operation
➢ A > B The result is true if A is greater than B, and is false otherwise
➢ A < B The result is true if A is smaller than B, and is false otherwise
• A>=B True if A is greater than or equal to B, false otherwise
• A<=B True if A is smaller than or equal to B, false otherwise
• A==B The result is true is A is equal to B, and is false otherwise
• A~=B The result is true is A is not equal to B, and is false otherwise

• Examples:
• >>a=5;b=2;
• >>x=a>b; %x=1
• >>x=(a~=b) %x=1

• Note:
• Many users confuse the double equality sign (==) used in relational tests with
the equality sign (=) used in assignments. When a user uses (=) instead of (==),
Matlab usually reports that an expected relational operator wasn't found.

Abdelrahman Mohamed fargly


33

Relational operation

Abdelrahman Mohamed fargly


34

Relational operation

Abdelrahman Mohamed fargly


35

Relational operation

Abdelrahman Mohamed fargly


36

Logical operation

Abdelrahman Mohamed fargly


37

Logical operation

Abdelrahman Mohamed fargly


38

Logical operation

Abdelrahman Mohamed fargly


39

Logical operation

Abdelrahman Mohamed fargly


40

Logical operation

Abdelrahman Mohamed fargly


41

General Functions
• who: List current variables
• whos: List current variables in details
• clear x: Clear variable x from memory
• clear all: Clear all variables from memory
• open: opens new file
• figure(n): opens new figure numbered n
• close: closes last figure
• close all: closes all figures
• dir: List files in directory
• format: Set output format
• Help: to know about any command and example
• Lookfor: to search for command
• exit: quits MATLAB

Abdelrahman Mohamed fargly


42

Exercise
• 5 minutes
• x=123;y=234;z=345
• Write the command expression and get the
result:
1) the sum of x and y
2) subtract y from z
3) the multiplication of x and z
• Answer
1) x+y=357;2)z-y=111;3)x*z=42435

Abdelrahman Mohamed fargly

You might also like