IS086IU-INTRODUCTION TO
07-SEP-20
C O M P U T I N G - M AT L A B A P P L I C AT I O N
Semester 1, 2023-2024
lectured by
L O N G T O N T H AT, P H . D .
ROOM O2.206,
S C H O O L O F E L E C T R I C A L E N G I N E E R I N G , I N T E R N AT I O N A L
U N I V E R S I T Y, V N U - H C M C
EMAIL: [email protected]
2
COURSE OUTCOMES
An ability to apply knowledge of mathematics, science and engineering
An ability to design and conduct experiments as well as to analyze and interpret data
An ability to use the techniques, skills, and modern engineering tools necessary for
engineering practice
3
C O U R S E I N F O R M AT I O N
Timetable:
18/9/2023-14/01/2024
Pre-requisite:
None
Co-requisite:
None
Course Evaluation:
Assignments (2)/ Quizzes (3): 30%
Midterm: 30%
Final: 40%
4
C O U R S E M AT E R I A L S
Textbooks
Stormy Attaway, MATLAB: A practical introduction to programming and problem solving, 3rd
edition, Elesvier, 2013
References
Shawna Lockhart and Eric Tilleson, An Engineer's Introduction to Programming with
MATLAB, SDC, 2018
Online materials
MATLAB from Mathworks Inc., https://www.mathworks.com/
5
TOPICS
1. Introduction to MATLAB
2. Expression and Interactivity
3. Making decisions
4. Looping
5. Visual programming
6. Elementary Data Structures
7. Object Oriented Programming
LECTURE 1:
I N T R O D U C T I O N T O M AT L A B
6
7
CONTENTS
Introduction
MATLAB Starting
Variables Definition
MATLAB Computation
Practical Exercises
8
INTRODUCTION
The need to simulate physical problems based on computers led to the invention of
numerical computing
Computer programming languages are used to codify the numerical methods for
programmed investigations of simulations, e.g. C, Python, Java,...
MATLAB (MatrixLAboratory) is initially developed to replace FORTRAN by doing
similar tasks such as solving systems of linear equations involving matrix algebra
For a long history of development, MATLAB is currently popular and widely used in
various universities and companies, especially to science and engineering fields
9
S TA R T I N G T H E M AT L A B ( I )
To start Matlab on a MS Windows system:
Double-click on the shortcut MATLAB icon in the Desktop
Waiting for the program running
After a short while, a window should open with three or four panes
A command prompt (>>) should appear in the new pop-up window to ensure the program has
10
S TA R T I N G T H E M AT L A B ( I I )
MATLAB Panels MATLAB version 2017
Command Window
• Type MATLAB commands
Current Folder (Current Directory in older version of
Matlab)
• Lists the files in that directory
Workspace
• List all the variables which have been defined
• Displayed variables may be viewed, manipulated,
saved, and cleared
Command History
• List all previous commands
11
S TA R T I N G T H E M AT L A B ( I I I )
Directory
Toolbar
Menu names
12
MENUS AND THE TOOLBAR
Some menus (above the Command window) are often displayed:
New Script
New Line Script
New
Open
Find Files
Compare
Import Data
Save Workspace
Quick Save
Undo
13
COMMAND WINDOW
14
CURRENT FOLDER
Called as Current Directory in the older version, e.g. 2008
A file manager window used to access files.
Open a file by double-clicking on the file name with the extension .m (Matlab Editor)
15
M AT L A B V E R S I O N S ( 2 0 0 8 V S 2 0 1 7 )
S TA R T I N G M AT L A B : W O R K S PA C E
16
WINDOW
Displays the variables created in the Command window.
Open Array Editor by double-clicking on a variable name.
S TA R T I N G M A T L A B - C O M M A N D 17
HISTORY
Show all the previous keystrokes entered in the Command window.
Used for keeping track what are typed.
Press Up Arrow key ( or enter commandhistory to open the Command History window
By default, the Command History window closes after a statement is selected or the Esc key is
pressed.
Double-click on a keystroke execute it in the Command Window.
ENTERING COMMANDS AND
18
EXPRESSIONS (I)
Enter: Executes the command, retype if there is a mistake.
Backspace key, Delete key, the left- / right arrow keys (← →): edit the commands.
up- /down-arrow (↑↓): scroll back/forward through the commands (once to see the previous
entry, twice to see the entry before that, and so on)
ENTERING COMMANDS AND
19
EXPRESSIONS (II)
The interaction between the user and Matlab
entry
response
Example:
By default, four decimal places are used in the result
Can use the command “format” to change the default
In Matlab, e represents exponentiation to a power of 10.
ENTERING COMMANDS AND
20
EXPRESSIONS (III)
Variable: a symbol used to contain a value and is assigned the answer by “ans”.
“ans” can be used for further calculations.
Use variables to write mathematical expression
→ assign the result to a variable of your own choosing
Matlab ignores spaces
ENTERING COMMANDS AND
21
EXPRESSIONS (IV)
When the users do not specify a variable name for a result, Matlab uses the symbol “ans” as a
temporary variable containing the most recent answer.
ENTERING COMMANDS AND
22
EXPRESSIONS (V)
Argument: is the value operated on by the function which is enclosed by the parentheses after
the function’s name.
There are hundreds of functions available.
function’s name
argument
23
ORDER OF PRECEDENCE (I)
A scalar is a single number.
A scalar variable is a variable that contains a single number.
Scalar arithmetic operations
Symbol Operation Matlab form
^ Exponentiation: ab a^b
* Multiplication: ab a*b
/ Right division: a/b a/b
\ Left division: a\b a\b
+ Addition: a+b a+b
- Subtraction: a-b a-b
24
ORDER OF PRECEDENCE (II)
Precedence Operation
First Parentheses, evaluated starting with the innermost pair.
Second Exponentiation, 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.
• Note: Parentheses can be used to alter this order.
25
ORDER OF PRECEDENCE (III)
26
ORDER OF PRECEDENCE (IV)
27
ORDER OF PRECEDENCE (V)
Practice: Use Matlab to calculate
5
3 4
a. 6 7 2 2
4 7 145
48.255 9 4
a. 209.8333
b. -15.7028
b.
53 14 2 c. 202.4120
27 2 319 4 / 5
6014
3
c.
4 5
07-SEP-20
28
T H E A S S I G N M E N T O P E R AT O R ( I )
29
T H E A S S I G N M E N T O P E R AT O R ( I I )
30
T H E A S S I G N M E N T O P E R AT O R ( I I I )
Example:
The volume of a sphere is given by , where is the radius. Use Matlab to compute the radius of a sphere
having a volume 30 percent greater than that of a sphere of radius 0.3m.
31
VA R I A B L E N A M E S
Workspace → names and values of any variables in use in the current work session.
Variable names:
Begin with a letter.
Contain less than 63 characters.
Can contain letters, digits, and underscore characters in the rest of the name.
Ex: practice, practice_1,...
32
D E F I N I N G VA R I A B L E S ( I )
D E F I N I N G VA R I A B L E S ( I I )
07-SEP-20 33
34
R U L E S F O R VA R I A B L E S ( I )
Variable names can be up to 63 characters long
Variable names must begin with a letter
Variable names can contain letters, digits, and the underscore character
MATLAB is case sensitive, i.e. distinguishes between UPPERCASE and LOWER CASE letters,
e.g. A and a are not the same variable
Avoid using names of functions for variables
Once a function name is used to define a variable, the function can not be used without quitting
MATLAB and restarting it, i.e. The variables should not be named sin, cos, exp, tan, sqrt, max, min,
sum, etc...
35
MANAGING THE WORK SESSION
Commands related Description
to the variables
clc _Clears the Command Window.
clear _Removes all variables from memory.
clear var1, var 2 _Removes the variables var1 and var2 from memory
exist (‘name’) _Determines if a file or variable exists having the name “name”.
quit _Stop Matlab.
who _Lists the variables currently in memory.
whos _Lists the current variables and sizes, and indicates if they have imaginary parts.
36
MANAGING THE WORK SESSION (II)
Command Description
: •_Colon; generates an array having regularly spaced elements.
, •_Comma; separates elements of an array.
; •_Semicolon; suppress screen printing; also denotes a new row in an array.
… •_Ellipsis, continues a line.
37
MANAGING THE WORK SESSION (III)
Use the arrow, tab, and control keys to recall, edit, and reuse functions and variables you typed
earlier.
Others:
↑: smart recall,
Tab: reduce the amount of typing,
Enter: display the value of the variable, or continue editing to create a new executable line.
Esc: clear the entire line
Ctrl+k: simultaneously to delete (kill) to the end of the line
38
MANAGING THE WORK SESSION (IV)
Special variables and constants
Command Description
ans _Temporary variable containing the most recent answer.
eps _Specifies the accuracy of floating point precision.
i,j _The imaginary unit
inf _Infinity
NaN _Indicates an undefined numerical result
pi _The number
39
MANAGING THE WORK SESSION (V)
Function: with arguments enclosed in the parentheses.
Command/statement: argument is not needed/ or not enclosed in the parentheses.
Ex: plot (x);
clear;
clear x;
40
C O M P L E X N U M B E R O P E R AT I O N S
Addition
Multiplication
Subtraction
Division
Complex conjugate
41
F O R M AT T I N G C O M M A N D
The format command controls how output numbers appear on the screen
Input numbers can be written in the following format(s)
>> format short (the default)
41.4286 Fixed-point with 4 decimal digits
>> format long
41.42857142857143 Fixed-point with 14 decimal digits
>> format short e
4.1429e+001 Scientific with 4 decimal digits
>> format long e
4.142857142857143e+001 Scientific with 15 decimal digits
>> format bank
41.43 Two decimal digits
42
O V E R V I E W O N A R R AY S
Handling collections of numbers (arrays) is one of computational capabilities of Matlab.
A numerical array is an ordered collection of number.
The elements of the array must be separated by commas or spaces.
Use “length” function to determine how many values are in the array.
Example
43
P O LY N O M I A L R O O T S
A polynomial in Matlab can be described by an array whose elements are the polynomial’s
coefficients, starting with the coefficient of highest power of .
Example:
- 4x3 – 8x2 + 7x -5 can be represented by
B U I LT – I N F U N C T I O N S 44
O C T AV E I N T R O D U C T I O N
46
PRACTICAL EXERCISES
Problem 1:
Suppose x takes on the values x=1, 1.2, 1.4, ..., 5. Use MATLAB to compute the array y that results from
the function y=7*Sin(4x). Use Matlab to determine how many elements are in the array y, and the value
of the third element in the array.
Problem 2:
Use Matlab to determine how many elements are in the array
[sin(-pi/2):0.05:Cos(0)].
Use Matlab to:
1. Determine the 10th element.
2. Create a new array taking the values from the 4 th to 8th element.
3. Calculate sum of the first three elements of the new array.
47
SUMMARY
Introduced MATLAB environment
Notes in computing with MATLAB
Rules in defining with the variables