Sie sind auf Seite 1von 41

CGE443 :

Computer Application in Oil &


Gas Engineering

Noraida Amin binti Md Taib

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.

• When you start MATLAB, a special window called the MATLAB


desktop appears. The desktop is a window that contains other
windows showing MATLAB data, plus toolbars and a “Start” button
similar to that used by Windows XP, Vista or Windows 7.

• 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.

• A user can enter interactive commands at the


command prompt (») in the Command Window,
and they will be executed on the spot.

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.

 MATLAB calculates the expression and responds by


displaying ans = and the numerical result of the
expression in the next line..

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.

• A function has a name and an argument in parentheses.

• For example, the function that calculates the square root of a


number is sqrt(x). Its name is sqrt , and the argument is x.

• When the function is used, the argument can be a number, a
variable that has been assigned a numerical value, or a computable
expression that can be made up of numbers and/or variables.
Functions can also be included in arguments, as well as in
expressions

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.

• Once a variable is assigned a numerical value, it can be used in


mathematical expressions, in functions, and in any MATLAB statements and
commands.

• A variable is actually a name of a memory location.

• When a new variable is defined, MATLAB allocates an appropriate memory


space where the variable’s assignment is stored.

• When the variable is used the stored data is used.

• 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.

• Can be up to 63 (in MATLAB 7) characters long (31 characters in


MATLAB 6.0).

• Can contain letters, digits, and the underscore character.

• Cannot contain punctuation characters (e.g. period, comma,


semicolon).

• MATLAB is case sensitive; it distinguishes between uppercase and


lowercase letters. For example, AA, Aa, aA, and aa are the names of
four different variables.

• No spaces are allowed between characters (use the underscore where


a space is desired).

• 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.

• When these commands are typed in the Command Window


and the Enter key is pressed, they either provide information,
or they perform a task as listed below

31
Script Files
• So far all the commands were typed in the Command
Window and were executed when the Enter key was
pressed.

• The commands in the Command Window cannot be saved


and executed again.

• A better way of executing commands with MATLAB is first to


create a file with a list of commands (program), save it, and
then run (execute) the file.

• If needed, the commands in the file can be corrected or


changed and the file can be saved and run again.

• 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.

 Using a script file is convenient because it can be edited (corrected


and/or changed) and executed many times.

 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

 In MATLAB script files are created and edited in the Editor/Debugger


Window.

 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.

 To be executed, MATLAB needs to know where the file


is saved.

 The file will be executed if the directory where the file


is saved is the current directory of MATLAB or if the
directory is listed in the search path

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.

 If an attempt is made to execute a script file by clicking on


the Run icon (in the Editor Window) while the current
directory is not the directory where the script file is saved,
then the prompt will open. The user can then change the
current directory to the directory where the script file is
saved, or add it to the search path
37
Script Files

38
Script Files

39
Examples of MATLAB Applications

40
Examples of MATLAB Applications

41

Das könnte Ihnen auch gefallen