Cairo University
Faculty of Engineering
Mining, Petroleum and Metallurgy Department
4th Year Metallurgy
Computer Applications in
Metallurgical Industries
LECTURE 1
By Dr. Ahmed Hatem Al-Khoribi
• Introduction
• MATLAB stands for MATrix LABoratory.
• Matlab considers all the variables and values as
matrices. The scalar is considered as a 1×1 matrix.
• The language of MATLAB was developed in 1970s
for applications involving matrices, linear algebra,
and numerical analysis. Thus, MATLAB was used
formerly by specialists in signal processing and
numerical analysis, but it has recently achieved a
widespread and acceptance throughout the
engineering community.
• MATLAB has the same logical, relational,
conditional, and loop structures as other
programing languages such as Fortran, C, BASIC,
and Pascal.
• Starting MATLAB
• Five windows appear upon starting MATLAB.
These are the Command window, the Current
Directory window, the Workspace window, the
Command History window, and the Details
window.
• Across the top of the Desktop are a row of
icons and menus (File, Edit, View, Debug,
Window, Help, etc..) called the toolbar. Below
or right to the toolbar, there exists a browsing
box showing the directory where MATLAB looks
for and saves files.
• Command Window
• To communicate with the MATLAB program, by
typing instructions of various types called
commands, functions, and statements.
• MATLAB displays the prompt (>>) to indicate
that it is ready to receive instructions. Before
you give MATLAB instructions, make sure the
cursor is located just after the prompt. If it is
not, use the mouse to move the cursor.
• Current Directory/Folder Window
• The Current Directory window is much like a
file manager window; you can use it to access
files. Double-clicking on a file name with the
extension .m will open that file in the MATLAB
Editor (Script File). The Script File is discussed
later. The Figure below shows that the Current
Directory is C:\MyMATLABFiles. You can change
the current directory by browsing for the
destination of your choice by double-clicking
on the desired file.
• Workspace Window
• To the left of MATLAB Desktop is the
Workspace window. The Workspace window
displays the variables created in the Command
window. Double-click on a variable name to
open the Array Editor.
• Command History Window
• The fourth window in the default Desktop is
the Command History window. This window
shows all the previous keystrokes you entered
in the Command window. It is useful for
keeping track of what you typed. You can click
on a keystroke and drag it to the Command
window or the Editor to avoid retyping it.
Double-clicking on a keystroke executes it in
the Command window.
• Entering Commands and Expressions
• If you make a typing mistake, just press the
Enter key until you get the prompt, and then
retype the line.
• Use the down-arrow key (↓) to scroll forward
through the commands. When you end the line
you want, you can edit it using the left- and
right-arrow keys (←and →), and the Backspace
key, and the Delete key. Press the Enter key to
execute the command.
• Note that you can see your previous
keystrokes displayed in the Command History
window. You can copy a line from this window
to the Command window by highlighting the
line with the mouse, holding down the left
mouse button, and dragging the line to the
Command window.
• Make sure the cursor is at the prompt in the
Command window. To divide 8 by 10, type 8/10
and press Enter (the symbol / is the MATLAB
symbol for division). Your entry and the
MATLAB response look like the following on the
screen (we call this interaction between you
and MATLAB an interactive session, or simply a
session). Remember, the symbol >>
automatically appears on the screen; you do
not type it.
• MATLAB indents the numerical result. MATLAB uses
high precision for its computations, but by default it
usually displays its results using four decimal places
except when the result is an integer.
• MATLAB assigns the most recent answer to a variable
called ans, which is an abbreviation for answer. A
variable in MATLAB is a symbol used to contain a
value. You can use the variable ans for further
calculations; for example, using the MATLAB symbol
for multiplication (*), we obtain
• Note that the variable ans now has the value 4.
• You can use variables to write mathematical
expressions. Instead of using the default
variable ans, you can assign the result to a
variable of your own choosing, say, r, as
follows:
• Spaces in the line improve its readability; for
example, you can put a space before and after
the = sign if you want. MATLAB ignores these
spaces when making its calculations. It also
ignores spaces surrounding + ,− , *, /, and \
signs.
• If you now type r at the prompt and press Enter, you
will see
• thus verifying that the variable r has the value 0.8. You
can use this variable in further calculations. For
example,
• A common mistake is to forget the multiplication
symbol * and type the expression as you would in
algebra, as s=20r. If you do this in MATLAB, you will
get an error message.
• MATLAB has hundreds of functions available.
One of these is the square root function, sqrt.
A pair of parentheses is used after the
functions name to enclose the value--called the
function’s argument--that is operated on by the
function. For example, to compute the square
root of 9 and assign its value to the variable r,
you type r = sqrt(9). Note that the previous
value of r has been replaced by 3. Other roots
are typed in exponentiation form, e.g.
>> r = 32^(1/5)
r=
2
• Arithmetic Operations
• MATLAB uses the symbols + − * / ^ for addition,
subtraction, multiplication, division, and
exponentiation (power) of scalars. These are
listed in Table 1.1-1. For example, typing x = 8 +
3*5 returns the answer x = 23. Typing 2^3-10
returns the answer ans = -2.
• The forward slash (/) represents right division,
which is the normal division operator familiar
to you. Typing 15/3 returns the result ans = 5.
MATLAB has another division operator, called
left division, which is denoted by the backslash
(\). The left division operator is useful for
solving sets of linear algebraic equations, as we
will see. A good way to remember the
difference between the right and left division
operators is to note that the slash slants
toward the denominator. For example,
7/2 = 2\7 = 3.5.
• Order of Precedence
• The mathematical operations represented by the
symbols + − * / \ and ^ follow a set of rules called
precedence. 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.
Evaluation begins with the innermost pair of
parentheses and proceeds outward. Table 1.1-2
summarizes these rules. For example, note the
effect of precedence on the following session.
• The Assignment Operator
• The = sign in MATLAB is called the assignment or
replacement operator. It works differently than the
equals sign you know from mathematics. When you
type x = 3, you tell MATLAB to assign the value 3 to
the variable x. This usage is no different than in
mathematics. However, in MATLAB we can also
type something like this: x = x + 2. This tells
MATLAB to add 2 to the current value of x, and to
replace the current value of x with this new value. If
x originally had the value 3, its new value would be
5. This use of the operator is different from its use
in mathematics. For example, the mathematics
equation x = x + 2 is invalid because it implies that 0
= 2.
• In MATLAB the variable on the left-hand side of
the = operator is replaced by the value generated
by the right-hand side. Therefore, one variable,
and only one variable, must be on the left-hand
side of the = operator. Thus in MATLAB you cannot
type 6 = x. Another consequence of this restriction
is that you cannot write in MATLAB expressions
like the following:
>> x+2=20
• The corresponding equation x + 2 = 20 is
acceptable in algebra and has the solution x = 18,
but MATLAB cannot solve such an equation
without additional commands (these commands
are available in the Symbolic Math toolbox).
• Another restriction is that the right-hand side
of the = operator must have a computable
value. For example, if the variable y has not
been assigned a value, then the following will
generate an error message in MATLAB.
>> x = 5 + y
• In addition to assigning known values to
variables, the assignment operator is very
useful for assigning values that are not known
ahead of time, or for changing the value of a
variable by using a prescribed procedure. The
following example shows how this is done.
• Volume of a Circular Cylinder
• The volume of a circular cylinder of height h
and radius r is given by V = πr2h. A particular
cylindrical tank is 15 m tall and has a radius of 8
m. We want to construct another cylindrical
tank with a volume 20 percent greater but
having the same height. How large must its
radius be?
• Solution
• First solve the cylinder equation for the radius r. This gives
• The session is shown below. First we assign values to the variables r
and h representing the radius and height. Then we compute the
volume of the original cylinder and increase the volume by 20
percent. Finally we solve for the required radius. For this problem we
can use the MATLAB built-in constant pi.
>> r = 8;
>> h = 15;
>> V = pi*r^2*h;
>> V = V + 0.2*V;
>> r = sqrt(V/(pi*h))
r=
8.7636
• Thus the new cylinder must have a radius of 8.7636 m. Note that the
original values of the variables r and V are replaced with the new
values. This is acceptable as long as we do not wish to use the
original values again. Note how precedence applies to the line V =
pi*r^2*h;. It is equivalent to V = pi*(r^2)*h;.
• Commands for Managing the Work Session
• Special Variables and Constants
• Numeric Display Formats