Professional Documents
Culture Documents
1. Introduction
1
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
Install and start MATLAB. You should see the GUI shown below. The various
windows may be positioned differently on your version of MATLAB – they are
‘drag and drop’ windows. You may also see a slightly different looking GUI if you
are using an older version of MATLAB.
2
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
➢ The MATLAB Command Window is the main window where you type
commands directly and execute.
➢ The Command History Window displays a log of statements that you ran in
the current and previous MATLAB sessions.
➢ The Workspace Window contains variables that you create or import into
MATLAB. The workspace shows the name of each variable, its value, its
array size. Workspace variables do not persist after you exit MATLAB.
➢ The MATLAB Editor Window is a simple text editor where you can load,
edit and save complete MATLAB programs.
➢ The Current Folder Window is a reference location that MATLAB uses to
find files.
3
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
➢ The Figure Window that floats independently from the main desktop. It is
not already present, it is launched when command execution results in
graphical output
➢ The Command window is where the command line prompt for interactive
commands is located.
➢ The MATLAB prompt on the command window consists of two adjacent
right angle brackets, i.e., >>.
➢ Command Window: In the Command Window, you can enter commands
and data, make calculations, and print results. You can write a script in the
Command Window and execute the script. However, writing a script directly
into the Command Window is discouraged because it will not be saved, and
if an error is made,the entire script must be retyped. By using the up arrow
(↑) key on your keyboard, the previous command can be retrieved (and
edited) for re-execution.
➢ Results of command operations will also be displayed in this window unless
the command line is terminated by a semi-colon, in which case the display
of results is suppressed.
➢ If a command or script specified on the command line is questionable or
cannot be executed because of invalid syntax, undefined variables, etc., a
diagnostic message will be displayed in the Command window in red font.
➢ The current value of any saved variable is also displayed in this window if its
name is entered at a prompt.
4
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
➢ The Command History Window displays a log of statements that you ran in
the current and previous MATLAB sessions
➢ The commands appear in command history will not deleted unit you delete
them. To delete command history contents, enter the command history
window and select all commands with Ctrl+A and push delete button.
➢ >> clc
5
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
➢ A workspace is the collection of all the variables and arrays that are defined
within the command window.
➢ This window is also called the Workspace Browser.
➢ The Workspace Window contains variables that you create or import into
MATLAB.
➢ The workspace shows the name of each variable, its value, its array size.
➢ Workspace variables do not persist after you exit MATLAB.
➢ To clear workspace window , use clear command to delete a variable
6
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
The contents of the workspace persist between the executions of separate commands.
Therefore, it is possible for the results of one problem to have an effect on the next
one. To avoid this possibility, it is a good idea to issue a clear command at the start
of each new independent calculation.
>>clear
The command clear or clear all removes all variables from the workspace. This frees
up system memory.
7
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
8
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
• The MATLAB Editor Window is a simple text editor where you can load,
edit and save complete MATLAB programs.
• It is also called Edit/Debug Window.
• An Editor Window is created when you create a new M-file or open an
existing one.
• There are several ways to open an editor window:
9
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
4-7 M – files
An m-file, or script file, is a simple text file where you can place MATLAB
commands. When the file is run, MATLAB reads the commands and executes
them exactly as it would if you had typed each command sequentially at the
MATLAB prompt. A new m-file can be created by selecting
or :
click on the New button from the toolbar. This opens up a floating editing window
(Blank m-file ) into which a sequence of commands can be entered.
10
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
1-Command Window: This is the main window, and contains the command
them again.
11
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
3. Workspace: Lists all the variables you have generated in the current
session. It shows the type and size of variables, and can be used to
4. Current Directory: Shows the files and folders in the current directory.
The path to the current directory is listed near the top of the MATLAB
on your M:drive, and this is where you should save your work
As you work in MATLAB, you issue commands that create variables and call
functions. For example, create a variable named a by typing this statement at the
command line:
a=1
MATLAB adds variable a to the workspace and displays the result in the Command
Window.
a=
b=2
b=
c=a+b
c=
12
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
d = cos(a)
d=
0.5403
When you do not specify an output variable, MATLAB uses the variable ans, short
for answer, to store the results of your calculation.
sin(a)
ans =
0.8415
Notes:
• If a statement does not fit on one line, use " . . . ", followed by Enter to indicate
that the statement continues on the next line. For example:
(20*sqrt (100)
• If we don’t specify an output variable, MATLAB uses the variable ans (short for
• Use Up arrow and Down arrow to edit previous commands you entered in
Command Window.
• Insert " % " before the statement that you want to use it as comment; the
statement will appear in green color.
13
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
•it is possible to enter multiple statements per line. Use commas (,) or semicolons (;)
to enter more than one statement at once. Commas (,) allow multiple statements per
line without suppressing output.
b = 0.6570
c = 548.3170
•When the symbol % (percent) is typed at the beginning of a line, the line is
designated as a comment. This means that when the Enter key is pressed the line is
not executed.
There are a set of rules that must be followed in choosing the names of variables,
which are:
Keywords
>> iskeyword
ans = 'break'
'case'
'catch'
'classdef'
…….
Note: You cannot assign these reserved names as your variable names.
Note: MATLAB allows you to reassign built-in function names as variable
names, but that is not recommended! – so be carefully when you select
the name of your variables
>>sin=4
sin =
4
15
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
>>sin(3)
??? Index exceeds the number of array elements (1).
'sin' appears to be both a function and a variable. If this is unintentional, use 'clear
sin' to remove the variable 'sin' from the workspace.
In the above example you have defined a variable “sin” – but “sin” is also a
built-in function – and this function will no longer work! If you accidently do so,
use the clear command to reset it back to normal
Example:
Note: All MATLAB commands are written in lowercase letters, such as:
( if,while,for,…)
You can recall previous commands by pressing the up- and down-arrow keys,
↑ and ↓. Press the arrow keys either at an empty command line or after you type the
first few characters of a command. For example, to recall the command b = 2, type
b, and then press the up-arrow key.
Note: Once you have assigned a value to a variable, MATLAB remembers and save
it in workspace windows. To remove a value from a variable you can use the ‘clear’
statement
>>clear a
>>a
If you type ‘clear’ and omit the variable, then everything gets cleared. it is useful
when you want to start a fresh calculation.
16
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
Predefined Variables
Notice that:
• If you do put a semicolon, the operation will be completed but MATLAB will
not print the result. This can be useful when you want to do a sequence of
calculations.
• Special numbers, like `pi’ can use in MATLAB directly.
• The Command History window keeps track of everything you have typed.
You can double left click on a line in the Command history window to repeat
it, or right click it to see a list of other options.
• If you screw up early on in a sequence of calculations, there is no quick way
to fix your error, other than to type in the sequence of commands again. You
can use the ‘up arrow’ key to scroll back through a sequence of commands.
Again, there is a better way to use MATLAB that gets around this problem.
• You can right click the command window and delete everything (but this will
not reset variables). You can also delete lines from the Command history, by
right clicking the line and selecting Delete Selection. Or you can delete the
entire Command History.
• You can get help on MATLAB functions by highlighting the function, then
right clicking the line and selecting Help on Selection.
17
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
5. MATLAB help
Help is available through the online manual – Click on the question-mark in the strip
near the top right of the window).
By default the help window opens inside the MATLAB GUI, but you can drag it out
so it occupies a new window on your desktop.
If you already know the name of the MATLAB function you want to use the
help manual is quite good – you can just enter the name of the function in the search,
and a page with a good number of examples usually comes up. It is more challenging
to find out how to do something, but most of the functions you need can be found
by clicking on the MATLAB link on the main page and then following the menus
that come up on subsequent pages.
18
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
6. MATLAB Operators
A-Arithmetic Operators
Symbol Role
+ Addition
- Subtraction
.* Element-wise multiplication
* Matrix multiplication
./ Element-wise right division
/ Matrix right division
.\ Element-wise left division
\ Matrix left division
(also known as backslash)
.^ Element-wise power
^ Matrix power
19
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
Mathematical expressions are evaluated starting from the left, with the
exponentiation operation having the highest order of precedence, followed by
multiplication and division with equal precedence, followed by addition and
subtraction with equal precedence. Parentheses can be used to alter this order. the
order in which the arithmetic operations are evaluated is given in Table 1.2.
MATLAB arithmetic operators obey the same precedence rules as those in most
computer programs.
20
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
In summary:
Examples:
>> 1/0
ans = Inf
Another special value is NaN, which stands for not a number. NaN is used to
express an undefined value. For
example.
>> Inf/Inf
ans = NaN
21
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
Relational Operators
Symbol Role
== Equal to(for logical equality)
~= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
Logical Operators
22
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
7. MATLAB as a calculator
Note1:
>> 2+3
ans= 5
>> 5ˆ3
ans= 125
>>2.5^2
ans = 6.2500
>> 3*(23+14.7-4/6)/3.5
ans= 31.7429
>> 1/0
ans = Inf
23
MATLAB 2024- 2023 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
Another special value is NaN, which stands for not a number. NaN is used to express
an undefined value. For
example.
>> Inf/Inf
ans = NaN
Note: To close MATLAB by selecting Exit MATLAB from the File menu in the
MATLAB desktop window or by typing the Exit command in the Command
window, or in the desktop corner (×) the upper-right MATLAB sign.
MATLAB Exercises
EX1:
Find the value of y:
Where x = 18 y =7 z= 3.5
24