0% found this document useful (0 votes)
12 views37 pages

MATLAB Basics for Engineering Students

The document is a guide titled 'Introduction to MATLAB for Engineering Students (Level I)', published in April 2023, aimed at engineering freshmen with no prior experience in MATLAB. It covers essential MATLAB features through a step-by-step approach, including tutorials on the MATLAB interface, basic calculations, mathematical functions, plotting, and matrix generation. The book emphasizes a hands-on learning experience with exercises at the end of each tutorial session.

Uploaded by

projectdesign649
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)
12 views37 pages

MATLAB Basics for Engineering Students

The document is a guide titled 'Introduction to MATLAB for Engineering Students (Level I)', published in April 2023, aimed at engineering freshmen with no prior experience in MATLAB. It covers essential MATLAB features through a step-by-step approach, including tutorials on the MATLAB interface, basic calculations, mathematical functions, plotting, and matrix generation. The book emphasizes a hands-on learning experience with exercises at the end of each tutorial session.

Uploaded by

projectdesign649
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

See discussions, stats, and author profiles for this publication at: [Link]

net/publication/369850454

Introduction to MATLAB for Engineering Students (Level I)

Book · April 2023


DOI: 10.13140/RG.2.2.18426.16323

CITATIONS READS
0 2,381

1 author:

Abdellatif M. Sadeq
Qatar Naval Academy
119 PUBLICATIONS 847 CITATIONS

SEE PROFILE

All content following this page was uploaded by Abdellatif M. Sadeq on 21 April 2025.

The user has requested enhancement of the downloaded file.


Introduction to MATLAB for
Engineering Students – Level I
First Edition: April 2023

@ Copyright – Author Retained


All rights reserved. No part of this book may be reproduced, distributed, or transmitted in any
form without the author’s prior written permission.
Readers are kindly requested to cite this book when referencing its content in academic or
professional work.
DOI: 10.13140/RG.2.2.18426.16323
ISBN: 978-1-326-50391-8
Imprint: [Link]
Published and distributed via [Link].

Disclaimer
This book is an independent publication and is not affiliated with, endorsed by, or sponsored by
MathWorks or MATLAB. The MATLAB logo or related imagery is used solely to acknowledge the
use of the software during a one-month free trial license provided by MathWorks for academic
purposes.

i
Contents
1. Tutorial Lesson 1 ...................................................................................................................... 1
1.1 Introduction to MATLAB .................................................................................................. 1
1.2 MATLAB Interface ............................................................................................................ 1
1.3 Using MATLAB as a Calculator ......................................................................................... 2
1.4 Creating MATLAB Variables.............................................................................................. 4
1.5 Controlling the Hierarchy of Operations or Precedence ................................................. 5
1.6 Controlling the appearance of floating point number ..................................................... 6
1.7 Keep Track of the Work Session ....................................................................................... 7
1.8 Entering Multiple Statements per Line ............................................................................ 7
1.9 Getting Help ..................................................................................................................... 8
Exercise 1 ..................................................................................................................................... 9
2. Tutorial Lesson 2 .................................................................................................................... 10
2.1 Mathematical Functions................................................................................................. 10
2.2 Basic Plotting .................................................................................................................. 11
2.3 Matrix Generation .......................................................................................................... 15
Exercise 2 ................................................................................................................................... 24
3. Tutorial Lesson 3 .................................................................................................................... 25
3.1 Array Arithmetic Operations .......................................................................................... 25
3.2 Solving Linear Equations ................................................................................................ 27
3.3 Matrix Inverse ................................................................................................................ 28
Exercise 3 ................................................................................................................................... 29

ii
List of Tables

Table 1: Basic arithmetic operators ................................................................................................ 3


Table 2: Elementary functions ...................................................................................................... 10
Table 3: Predefined constant values............................................................................................. 10
Table 4: Attributes of the plot .................................................................................................... 155
Table 5: Elementary matrices ....................................................................................................... 22
Table 6: Array operators ............................................................................................................. 255

iii
List of Figures
Figure 1: The graphical interface to the MATLAB workspace ........................................................ 2

iv
Preface
“Introduction to MATLAB for Engineering Students I” is an introductory course for MATLAB that
is given as a step by step guide for engineering freshmen and those seeking to learn MATLAB.
This file is not a comprehensive introduction to MATLAB such as the reference manual, however
it focuses on the main features of MATLAB, which is useful for engineering students in their
courses. In principle, this workshop aims to familiarize the students with the use of MATLAB
software to solve mathematical and engineering problems. Also, this workshop assumes that the
students have no prior experience with MATLAB. The document is designed in such a way to be
used by the students while they are working on the computer. The emphasis here is learn by
doing. At the end of each tutorial session, there will be an exercise given as a quiz, which is
estimated to be solved in 30 minutes.

The availability of technical computing platforms such as MATLAB is currently reshaping the
applications and role of computer laboratory projects to get the students involved in more
intense problem-solving experience. In addition, this availability provides a chance to conduct
numerical experiments easily and to tackle complicated and more realistic problems. Basically,
the document is divided into three laboratory sessions devoted for students seeking to build the
fundamentals of MATLAB software.

v
1. Tutorial Lesson 1

1.1 Introduction to MATLAB

In this introduction, the simple numerical expressions and mathematical formulas are handled in
MATLAB. The word MATLAB stands for MATrix LaBoratory. MATLAB was originally developed to
simplify solving system of linear equations and Eigenvalue problems [1]. MATLAB is a high-
performance technical computing language. It integrates visualization, computation, and
programming environment. Moreover, MATLAB is considered as a modern programming
language and it has a sophisticated data structures, debugging tools, built-in editing tools, in
addition to supporting object-oriented programming. Due to that, MATLAB is an excellent
research and teaching tool.

1.2 MATLAB Interface

After starting MATLAB by double-clicking its shortcut icon, a special window called “MATLAB
desktop” will appear, and this window contains other windows. In the MATLAB desktop windows,
you will see the following windows:

• The Command Window


• The Command History
• The Workspace
• The Current Directory
• The Help Browser
• The Start button

1
Figure 1 shows how a typical user interface should looks when starting MATLAB

Figure 1: The graphical interface to the MATLAB workspace

1.3 Using MATLAB as a Calculator

MATLAB can be used as a calculator and performs simple interactive calculations. Let us suppose
that you want to calculate 2 +1 x 3. You type it at the prompt command (>>) as follows,

You may have noticed that if you do not specify an output variable, MATLAB considers a default
variable “ans”, that is short for answer, to store the results of the running calculation. Also, notice

2
that the variable “ans” is created (or overwritten if it is already existed). To avoid this, you may
specify a value to a variable or output argument name. For example,

Now, MATLAB will assign the value of 5 to x unless it is overwritten by another value. Thus,
computing the value of 5x will give:

Before ending this section, Table 1 gives a summary for the basic arithmetic operators used in
MATLAB:

Table 1: Basic arithmetic operators

Symbol Operation Example


+ Addition 2+4
- Subtraction 2-4
* Multiplication 2*4
/ Division 2/4

3
1.4 Creating MATLAB Variables

MATLAB variables can be created using an assignment statement. The syntax of this assignment
is

variable name= an expression (or a value)

For example,

Where “expression” is a combination of mathematical operators, numerical values, function


calls and variables. On other words, “expression” may involve:

• Manual entry
• User-defined functions
• Built-in functions

Once you have created a variable, it can be overwritten by assigning a new expression (value)
to it. In addition, if you do not wish the intermediate results to appear, you can put a semicolon
(;) at the end of the line. Thus, the sequence of the lines looks like the following:

4
1.5 Controlling the Hierarchy of Operations or Precedence

Let us consider these two arithmetic operations and see how MATLAB handles them:

From the previous arithmetic operations, it is clear how MATLAB gives different results due to
the use of brackets in the second operation. In MATLAB, the order of the arithmetic operations
is the same as the one thought in high algebra courses. Exponentiations are done first, followed
by multiplications and divisions, and finally additions and subtractions. Parentheses are used to
overrule priority, and this is highly recommended when using complex expressions.

Example:

Solve the following expression using MATLAB

1 4 6
2
+ +
2+3 5 7

Solution:
In MATLAB, this expression becomes:

5
If parentheses are missing, the solution would be:

1.6 Controlling the appearance of floating point number

MATLAB by default shows four digits in the results computed from a certain calculation.

For example,

However, there are some commands that let MATLAB display the results in different formats.
The command “format” is used to control the computed results.

For example,

6
The command ‘format long” is used to show all the 15 digits in the result. If we want to return
to the previous format, we will give the command “format short” or simply “format”

1.7 Keep Track of the Work Session

You can keep tracking everything during a MATLAB session using the command “diary”

Or you can give your file a name then tracks it:

Where the “FileName” could be any arbitrary name of your choice.

Also, you can start recording your session using the command “diary on” and then stop recording
using the command “diary off”. In case you need to clear the command window, type clc, and to
abort a MATLAB computation, type ctrl-c

1.8 Entering Multiple Statements per Line

It is possible to enter more than one statement in MATLAB at once using the semicolons (;) or
the commas (,). The use of the comma (,) allows multiple statements per one line without
suppressing the result.

7
Example,

1.9 Getting Help

You can access the online documents of MATLAB by selecting “MATLAB Help” from Help menu
or the one in the command window. Else, you can type “help Command” in the command
window

In addition, you can ask for some help about a specific function using the command “lookfor”
such as,

8
Exercise 1

1- Open a new file and name it with your QUID (e.g. 201504937)
2- Start recording your session
3- Find the value of a and b using MATLAB as a calculator, and control the display of the
answer to show 15 digits:
2(3 + 23 )
𝑎=
(5 + 3(2))3

2(𝑎 + 5)
𝑏=
(5 + 8𝑎)3

4- Find the value of a*b


5- Stop recording your session

Additional References

• [Link]

• [Link]
.html

9
2. Tutorial Lesson 2

2.1 Mathematical Functions

There are many built in and special functions that can be used in MATLAB for technical
computing. The full list of these functions can be obtained by typing “help elfun” or “help
specfun” in the command window. Many standard mathematical functions such as sin (x), cos
(x), tan (x), log(x) and exp(x) can be evaluated directly by using the functions sin, cos, tan, log and
exp respectively in MATLAB. Table 2 lists the most used functions, where the variable x can be a
number, a vector, or matrix.

Table 2: Elementary functions

sin (x) Sine abs (x) Absolute value


cos (x) Cosine max (x) Maximum value
tan (x) Tangent min (x) Minimum value
asin (x) Arc sine sign (x) Signum function
acos (x) Arc cosine floor (x) Round towards −∞
atan (x) Arc tangent ceil (x) Round towards +∞
sqrt (x) Square root round (x) Round to nearest integer
log (x) Natural logarithm angle (x) Phase angle
log10 (x) Common logarithm rem (x) Remainder after division
exp (x) Exponential conj (x) Complex conjugate

Along with these elementary functions, MATLAB has a list of predefined constant values and they
are given in Table 3.

Table 3: Predefined constant values

pi The π number, π = 3.14159 . . .


Inf The infinity, ∞
i,j The imaginary unit i, √−1
NaN Not a number

10
Example 1: Find the value of sin(π/4) using MATLAB

Example 2: Find the value of e10 using MATLAB

Example 3: Find the value of y = e −a sin(x) + 9√y, for a = 5, x = 3, and y = 8 using MATLAB

2.2 Basic Plotting

MATLAB allows you to make graphs using an excellent set of graphic tools. The use of plots to
represent mathematical functions is highly recommended and adds a great value to your end
results. In order to plot a graph in MATLAB, you need to define vectors of x (x = (x1, . . ., xN)) and
y (y = (y1, . . . , yN )) coordinates which contains all the x and y points respectively. You need to
prepare x and y points in identical arrays (either row or column arrays) that have the same size.

11
Example: Given the vectors x = (1, 2, 3, 4, 5, 6) and y = (3, −1, 2, 4, 5, 1), use MATLAB to generate
a plot of y as a function of x.

Also, you can specify the size of step (increment), the plot title, x and y axis’s labels, the color
and style of the line and the legend’s names. The following example shows the commands that
can perform these actions.

12
Example: plot the function sin (x) on the interval [0, 2π] taking π/100 as a step size.

Note: by default, the color of the line is set to be blue, however you can specify the color by
inserting the symbol of the desired color in the plot command. In the last example, the color of
the line can be changed to red instead of blue by using this command “plot (x,y, 'r')” instead of
“plot (x,y)”.

13
Example: plot three related functions of x: y1 = 2 cos(x), y2 = cos(x), and y3 = 0.5 ∗ cos(x), in the
interval 0 ≤ x ≤ 2π.

14
Table 4 summarizes the most frequently used attributes in the MATLAB plots

Table 4: Attributes of the plot

Line
Symbol Color Symbol Symbol Marker
Style
k Black − Solid + Plus sign
b Blue : Dotted ∗ Asterisk
c Cyan −− Dashed o Circle
y Yellow −. Dash-dot . Point
r Red none No line x Cross
g Green d Diamond
m Magenta s Square

2.3 Matrix Generation

Matrices are fundamental parts of MATLAB and they are the major element in its environment.
They are two-dimensional arrays consisting of m rows and n columns. In this section, we will see
how matrices are generated and manipulated in MATLAB. In general, to create a matrix, you need
to do the following steps:

• Start with a square bracket, “[“


• Use spaces or commas (,) to separate elements in a row
• Use the semicolon (;) to separate rows
• Use another square bracket, “]” to end the matrix with

15
1 2 3
Example: Create the matrix [𝐴] = [4 5 6] in MATLAB
7 8 9

Once we have created a matrix, it is automatically stored in the workspace. We can refer to a
specific element within the matrix and show it. We write A (2,3) to show the element in the
second row and third column and as so.

Also, we can replace any element in the matrix by another element and get the new modified
matrix as shown below.

16
If we need to create a vector instead of a matrix, it would be much easier because it is one-
dimensional. The following example will show how the use of the colon is very efficient when
dealing with vectors creation. For example, to create the vector x that consists of the points (0,
0.1, 0.2, 0.3, · · ·, 5), we can simply write “x=0:0.1:5” in MATLAB and all the 51 elements will be
shown in rows.

On the other hand, there is another useful command “linspace” that is used to create a vector
between two desired points a and b, and with the required number of points between them
(equally spaced points). For example, writing the command “theta = linspace(0,2*pi,101)” will
divide the interval [0 2π] into 100 equal subintervals, and then create a vector with the 101
elements.

The colon operator can be used to select a certain column or row. For example, the statement A
(m:n,k:l) specifies the rows m to n and columns k to l.

1 2 3
Example: Pick up the second row of matrix [𝐴] = [4 5 6]
7 8 9

17
1 2 3
Example: Pick up the second and third column of matrix [𝐴] = [4 5 6]
7 8 9

A column or a row can be deleted by setting it to null. For example, to delete the second column
of matrix [A], write the following command A ( : , 2) = [ ]

You can extract a matrix [B] from matrix [A] by specifying the rows and columns you want to
extract. The following example explains how this could happen.

18
To interchange the first and second row of A, use the vector of row indices along with the colon
operator.

To create a vector form of matrix A, write the following:

Also, you can use the command A (end, :) or A (: end) to show the last row or last column in
matrix A, respectively.

19
In addition, you can reverse the order of numbers in a certain row or column. The following
example illustrates this methodology:

To know the size of a matrix, use the command “size”:

20
Or

Where m stands for the number of rows, and n stands for the number of columns.

To flip a matrix about its main diagonal (transposing a matrix), using the single quote (‘) as the
following:

By transposing a matrix, the structure changes from (m*n) to (n*m) because of interchanging the
rows and columns, and the matrix transpose is denoted by AT in algebra. In addition to matrix
transpose, there is a set of elementary matrices summarized in Table 5.

21
Table 5: Elementary matrices

eye(n) Returns an n-by-n square identity matrix

eye(m,n) Returns an m-by-n matrix with 1 on the main diagonal

zeros(m,n) Returns an m-by-n matrix of zeros

ones(m,n) Returns an m-by-n matrix of ones

rand(m,n) Returns an m-by-n matrix of random numbers

diag(A) Extracts the diagonal of matrix A

Exercise: Create the following matrices:


a) 4 by 4 identity matrix, and extract the diagonal of this matrix
b) 3 by 5 zeros matrix
c) 6 by 4 random numbers matrix

a)

22
b)

c)

23
Exercise 2

a) Plot three related functions of x: y1 = exp(x), y2 = 2 exp(x), and y3 = 0.5 exp(x), in the interval
0 ≤ x ≤ 2π. Color y1 with red, y2 with black and y3 with blue. Also, Specify the names of the
legends, axes and the chart title,

3 2 3
b) Write the following matrix [𝐴] = [4 4 7]in MATLAB and perform the following:
8 6 9

• Pick up the element that exists in the second row and second column
• Pick up the third column
• Find the size of matrix [A]
• Extract the diagonal of matrix [A]
3 2
• Extract matrix [B] out of matrix [A] such that matrix [B]=[ ]
4 4
• Exchange the first and third row of matrix [A]
• Delete the third row of matrix [A]
• Replace the number ‘3’ with ‘5’ in matrix [B]

Additional References

• [Link]

• [Link]

24
3. Tutorial Lesson 3

3.1 Array Athematic Operations

Array arithmetic operations differs from matrix ones as they are done element by element. The
period character “.” distinguishes the array arithmetic operations from the matrix operations.
Table 6 lists the operators used for arrays.

Table 6: Array operators

+ Element by element addition


- Element by element subtraction
.* Element by element multiplication
./ Element by element division
.^ Element by element exponentiation

The addition and subtraction are the same for arrays and matrices operations, however
multiplication, division and exponentiation differ.

25
1 2 3 10 20 30
Example: Suppose you have matrix = [4 5 6] , 𝐵 = [40 50 60]. Find the following:
7 8 9 70 80 90

a) C=A.*B

b) D= A.^2

26
3.2 Solving Linear Equations

One of the most frequently encountered problems in engineering applications is, solving a system
of linear equations in the form Ax=b. In this problem, A is a square matrix of order n, x is a column
vector matrix of n components that includes all the unknowns. b is a column vector matrix of n
known components.

Example: Find the values of x, y and z in the following system of linear equations:

x + 2y+ 3z = 1
4x + 5y+ 6z = 1
7x + 8y =1
Solution

27
Alternatively, you can find the solution by writing x=A\b,

3.3 Matrix Inverse

Considering matrix, A again,

1 2 3
𝐴 = [4 5 6]
7 8 9

Calculating the inverse of this matrix by hand does not seem like a pleasant work. In this aspect,
MATLAB provides an easy way to compute the inverse of a square matrix using the command
“inv”.

1 2 3
Example: Compute the inverse and determinant of matrix 𝐴 = [4 5 6] using MATLAB.
7 8 0

28
, and the determinant of A is

Exercise 3

a) Suppose you have matrix = [1 2 3] , 𝐵 = [3 5 7]. Find the following:


4 5 6 2 6 8

• C=A.*B

• D= A.^2

b) Find the values of x, y and z in the following system of linear equations

3x + 2y- z = -7
x - y+ 2z = 19
2x + y+ z = 8

Additional References

• [Link]

• [Link]

29
Bibliography

[1] The MathWorks Inc. MATLAB 7.0 (R14SP2). The MathWorks Inc., 2005.

[2] C. F. Van Loan. Introduction to Scientific Computing. Prentice Hall, 1997.

[3] C. B. Moler. Numerical Computing with MATLAB. Siam, 2004.

[4] S. J. Chapman. MATLAB Programming for Engineers. Thomson, 2004

[5] D. J. Higham and N. J. Higham. MATLAB Guide. Siam, second edition edition, 2005

[6] J. Cooper. A MATLAB Companion for Multivariable Calculus. Academic Press, 2001.

[7] A. Gilat. MATLAB: An introduction with Applications. John Wiley and Sons, 2004.

[8] K. R. Coombes, B. R. Hunt, R. L. Lipsman, J. E. Osborn, and G. J. Stuck. Differential Equations


with MATLAB. John Wiley and Sons, 2000.
[9] D. Kahaner, C. Moler, and S. Nash. Numerical Methods and Software. Prentice-Hall, 1989.
[10] D. Houcque. Applications of MATLAB: Ordinary Differential Equations. Internal
communication, Northwestern University, pages 1–12, 2005.
[11] J. C. Polking and D. Arnold. ODE using MATLAB. Prentice Hall, 2004.
[12] J. W. Demmel. Applied Numerical Linear Algebra. Siam, 1997.
[13] [Link]. Numerical Methods Lab Manual, 2020

30

View publication stats

You might also like