CGE443: Computer Application in Oil & Gas Engineering: Noraida Amin Binti MD Taib March 2017
CGE443: Computer Application in Oil & Gas Engineering: Noraida Amin Binti MD Taib March 2017
March 2017
Course Information
Subject Name Computer Application in Oil & Gas
Engineering
Code CG443
Credit hour / Contact hour 3 Credit hours
(2 hour lecture & 2 hour lab per week)
Pre-requisite (if any) Nil
Assessment
Assignment/ Project/Test 50%
Final Exam 50%
2
Rules & Regulation
• Be on time.
• Replacement for test/quiz will not be entertained
in the case of ABSENT – without solid
justification/proof
• No extension on assignment. Late submission
will be penalized.
3
MATLAB Basics
• Introduction
• The command window
• Operations with scalars
• Display formats
• Elementary math built-in functions
• Defining scalar variables
• Script files
• Examples of MATLAB applications
4
Introduction
• MATLAB (MATrix LABoratory)
• It is a special-purpose computer program optimized
to perform engineering and scientific calculations.
• Capable of solving essentially any technical
problem.
• It has an extensive library of predefined functions to
make technical programming tasks easier and more
efficient.
• MATLAB is a huge program with an incredibly rich
variety of functions.
5
The MATLAB Environment
• When MATLAB executes, it can display several types of windows
that accept commands or display information.
• The major tools within or accessible from the MATLAB desktop are
the following:
▫ The Command Window
▫ The Command History Window
▫ The Start Button
▫ The Figure Windows
▫ The Workspace Browser
▫ The Help Browser
▫ The Path Browser
6
The MATLAB Environment
7
The MATLAB Environment
8
The command window
• The right-hand side of the default MATLAB
desktop contains the Command Window.
9
Figure window
• The Figure Window opens automatically when
graphics commands are executed, and contains
graphs created by these commands
10
Editor window
• The Editor Window is used for writing and
editing programs.
• This window is opened from the File menu in
the Command Window.
11
Help window
• The Help Window contains
help information.
• This window can be
opened from the Help
menu in the toolbar of any
MATLAB window.
• The Help Window is
interactive and can be used
to obtain information on any
feature of MATLAB.
12
Working in the Command Window
• The Command Window is MATLAB’s main window,
and can be used for executing commands, opening
other windows, running programs written by the user.
13
Notes for working in the Command
Window
• To type a command the cursor must be placed next to the command prompt (>>).
• Once a command is typed and the Enter key is pressed, the comand is executed. However,
only the last command is executed. Everything executed previously is unchanged.
• Several commands can be typed in the same line. This is done by typing a comma between
the commands. When the Enter key is pressed the commands are executed in order from
left to right.
• It is not possible to go back to a previous line in the Command Window make a correction,
and then re-execute the command.
• A previously typed command can be recalled to the command prompt with the up-arrow key
( ↑ ). When the command is displayed at the command prompt, it can be modified if needed
and executed. The down-arrow key ( ↓ ) can be used to move down the previously typed
commands.
• If a command is too long to fit in one line, it can be continued to the next line by typing three
periods “…” (called an ellipsis) and pressing the Enter key. The continuation of the command
is the typed in the new line. The command can continue line after line up to a total of 4096
characters.
14
Notes for working in the Command
Window (cont’d)
• If a semicolon ( ; ) is typed at the end of a
command the output of the command is not
displayed.
• When the symbol % (percent symbol) is typed in
the beginning of a line, the line is designated as a
comment.
• The clc command (type clc and press Enter)
clears the Command Window
15
Arithmetic Operations with Scalars
• Numbers can be used in arithmetic calculations
directly (as with a calculator), or they can be
assigned to variables, which can subsequently
be used in calculations.
16
Arithmetic Operations with Scalars
(cont’d)
• Order of Precedence
MATLAB executes the calculations according to
the order of precedence displayed below. This
order is the same as used in most calculators.
17
Arithmetic Operations with Scalars
(cont’d)
• Using MATLAB as a Calculator
The simplest way to use MATLAB is as a calculator.
This is done in the Command Window by typing a
mathematical expression and pressing the Enter key.
18
19
Display Formats
• the output format is fixed-point with 4 decimal
digits (called short), which is the default format
for numerical values.
• The format can be changed with the format
command.
• Once the format command is entered, all the
output that follows is displayed in the specified
format
20
21
Elementary Math Built-In Functions
• MATLAB has a very large library of built-in functions.
22
Elementary Math Built-In Functions
(cont’d)
23
24
25
Defining Scalar Variables
• A variable is a name made of a letter or a combination of several letters (and
digits) that is assigned a numerical value.
• If the variable is assigned a new value the content of the memory location is
replaced
26
Defining Scalar Variables
• The Assignment Operator
In MATLAB the = sign is called the assignment
operator.
The assignment operator assigns a value to a
variable.
27
Defining Scalar Variables
28
29
Defining Scalar Variables
• Rules About Variable Names
A variable can be named according to the following rules:
• Must begin with a letter.
• Avoid using the names of a built-in function for a variable (i.e. avoid
using: cos, sin, exp, sqrt, etc.). Once a function name is used to define
a variable, the function cannot be used.
30
Useful Commands for Managing Variables
• The following are commands that can be used to eliminate
variables or to obtain information about variables that have
been created.
31
Script Files
• So far all the commands were typed in the Command
Window and were executed when the Enter key was
pressed.
• Files that are used for this purpose are called script files.
32
Script Files
• Notes About Script File
A script file is a sequence of MATLAB commands, also called a
program.
When a script file runs (is executed), MATLAB executes the commands
in the order they are written just as if they were typed in the Command
Window.
When a script file has a command that generates an output, the output
is displayed in the Command Window.
Script files can be typed and edited in any text editor and then pasted
into the MATLAB editor.
Script files are also called M-files because the extension .m is used
when they are saved.
33
Script Files
• Creating and Saving a Script File
This window is opened from the Command Window. In the File menu,
select New, and then select M-file
34
Script Files
35
Script Files
• Running (Executing) a Script File
A script file can be executed either directly from the
Editor Window by clicking on the Run icon, or by
typing the file name in the Command Window and
then pressing the Enter key.
36
Script Files
• Current Directory
The current directory is shown in the “Current Directory”
field in the desktop toolbar of the Command Window, as
shown in Figure.
38
Script Files
39
Examples of MATLAB Applications
40
Examples of MATLAB Applications
41