You are on page 1of 10

Al-Furat Al-Awsat Technical University

Engineering Technical College - Najaf

MATLAB FOR ENGINEERING

By
Mueen M. Al-Rubye
Assist Lecturer

2020
Mueen M. Al-Rubaye Engineering Technical College - Najaf

Introduction

MATLAB is an interactive software package which was developed to perform


numerical calculations on vectors and matrices. MATLAB comes from a MATrix
LABoratory.

Today, MATLAB is powerful software:

• It can do quite sophisticated graphics in two and three dimensions.


• It contains a high-level programming language such as C, C++ which
makes it quite easy to code complicated algorithms involving vectors and
matrices.

1
Mueen M. Al-Rubaye Engineering Technical College - Najaf

MATLAB Environment

1.1. Starting MATLAB


On Windows platforms, start MATLAB by double-clicking the MATLAB
shortcut icon on your Windows desktop.

1.2. MATLAB Desktop


When you start MATLAB, the MATLAB desktop appears as showing in Figure (1).

Current Command Workspace


Folder Window Window

Command History
Figure (1) the desktop of MATLAB
Window

2
Mueen M. Al-Rubaye Engineering Technical College - Najaf

When you get into MATLAB, it contains these main basic windows:

• Command Window: This is the main area where commands can be entered at
the command line. It is indicated by the command prompt (>>). As shown in
Figure (1).

• Workspace Window: It shows all the variables created and/or imported from
files. As shown in Figure (1):

• Command History Window: This panel shows or rerun commands that are
entered at the command line. As shown in Figure (1):

• Current Folder Window: This panel allows you to access the project folders and
files. As shown in Figure (1):

3
Mueen M. Al-Rubaye Engineering Technical College - Najaf

MATLAB Commands and Basics

2.1. Variables
• A variable represents any types of data, it usually represents a scalar, a vector,
a string, or a matrix. In order to store values, either in an M-file or the Command
Window, variables are used.
variable = expression
For example:
>> x = 4
X =
4

• This variable can then be used on the right-hand side of an equation such as

>> f = 3*x^6 - 17*x^3 + 79
• Overwritten: if you put another value to the variable x (like x=5) it takes the
new value x=5 not 4

• There are some variables we are not preferable to use such as:

pi = 𝜋 3.14

i, j = √−1
inf infinity

nan not a number (e.g. the result of 0/0)
ans default variable for results

4
Mueen M. Al-Rubaye Engineering Technical College - Najaf

2.2. Reserved Wards:


• There are a few names which are reserved because they have special meanings.
We shouldn’t use. The reserved words, called keywords, are

• Variables can also be deleted by using clear. For example, to delete x type
>> clear x

Warning: This is a very dangerous command because it is so easy to lose a great


deal of work. If you mean to type
>> clear x but instead you type
>> clear
you will delete all the variables you have created in the workspace!

2.3. Notes About Variables:

Variables: Case Sensitive which means variable X capital letter is not the same
x small letter.
must start with a letter, and can then be followed by any number of
letters, numbers, and/or underscores (so z_0 is allowed).
, Separates multiple statements on the same line. The results appear
on the screen.
; The result is not printed on the screen. This can
also separate multiple statements on the same line.
% Begin a comment.

5
Mueen M. Al-Rubaye Engineering Technical College - Najaf

2.4. Commands for Managing


MATLAB provides various commands for managing a session. Such as shown in the table
below:

Command Purpose
clc Clears command window.
Clear Removes variables from memory.
Quit Stops MATLAB.
Lookfor Searches help entries for a keyword.
Who Lists current variables.
Whos Lists current variables (long display).

2.5. Operators and Precedence:

Operator Description
() parentheses
^ exponentiation
*, /, \ all multiplication and division
+, - addition and subtraction

For example:

In Mathematics Formula In MATLAB Statements


x-4y x-4*y
x2 -13 X^2-13
𝑥' + 𝑦'
(X^2+y^2)/8
8
x(2y-9d) x*(2*y-9*d)

x – 4*y x ^ 2 -13 ( x^2 + y^2 )/8 x *( 2*y – 9*d )

❶ ❷ ❶ ❷
❶ ❶

❷ ❷ ❸ ❸

❹ ❹

6
Mueen M. Al-Rubaye Engineering Technical College - Najaf

Examples: Answer:
Write the following by using MATLAB: 6* 35^ (1/4) + 14^.35
/
6 ,350 1 + 143.56 Ans=
17.1123

2.6. Logical Operators, Relational Operators:

< Less than > Great than


Less than or Great than or
<= equal to
>= equal to
== Equal ~= Not equal to
A & B AND A | B OR
~A Not

Boolean variable = variable with true / false value, 1 means true and 0 means false.

For example:
1 < 3 3 < 2 3 < 3
ans= ans= ans=
1 0 0

1 <= 3 x=12; x ~= 13
ans= x == 13 ans=
1 ans= 1
0
(x==12) | (x<3) (x==12) | (x<3)
ans= ans=
1 0

7
Mueen M. Al-Rubaye Engineering Technical College - Najaf

2.7. Functions for Arithmetic Operations

Function Meaning
ceil Round to the nearest integer toward ∞ (e.g.: 3.6 becomes 4)
fix(x) Round to the nearest integer toward zero (e.g.: 3.6 becomes 3)
floor(x) Round to the nearest integer toward -∞ (e.g.: -3.6 becomes -4)
sign(x) Return sign of the number (e.g.: plus, minus, or zero)
mod(x) Return the real part of division x/y (e.g.: result =3.5 return 3)

2.8. Most useful Mathematical Functions:

Function Meaning
sqrt(x) Square root, √x
exp(x) Exponential, 𝑒 :
log(x) Natural logarithm
log10(x) Common (base 10) logarithm
sin(x) Sine
abc(x) Absolute Value |x|
asin(x) Inverse sine
cos(x) Cosine
acos(x) Inverse cosine
tan(x) Tangent
atan(x) Inverse tan

For example: the value of the expression 𝒚 = 𝒆=𝒂 𝐬𝐢𝐧(𝒙) + 𝟏𝟎G𝒚 for a = 5, x=2
and y=8.
>> a=5; x=2; y=8;
>> y=exp(-a)*sin(x)+10*sqrt(y)
Y=
28.2904

8
Mueen M. Al-Rubaye Engineering Technical College - Najaf

In Mathematics Formula In MATLAB Statements


𝑦 = G𝑥 5 + 20 𝑦 = 𝑠𝑞𝑟𝑡(𝑥^3 + 20)
b = ln (ca + ny) b = log (c ∗ a + n ∗ y)

𝑧 = 𝑠𝑖𝑛5 (𝑥 + 𝑛𝑏) 𝑧 = (sin(𝑥 + 𝑛 ∗ 𝑏))^3

𝑥
𝑧 = 𝑡𝑎𝑛=_ ( ) 𝑧 = atan (𝑥 /𝑦)
𝑦

𝑚 = 4G𝑒 :=6 𝑚 = 4 ∗ 𝑠𝑞𝑟𝑡(exp (𝑥 − 5)

|𝑦 − √𝑥|
𝑧= 𝑧 = 𝑎𝑏𝑠(𝑦 − 𝑠𝑞𝑟𝑡(𝑥))/(𝑎 − 𝑏)
(𝑎 − 𝑏)

Reference:
Overman, E. (2016). A MATLAB Tutorial. Department of Mathematics. The Ohio State University,
Columbus, 180.

MATLAB, M. (2012). The language of technical computing. The MathWorks, Inc. http://www.mathworks.com.

You might also like