INTRODUCTION TO MATLAB
Jude Kwaku Bonsu (PhD)
Department of Chemical Engineering, Kwame Nkrumah University of Science and Technology
What is MATLAB?
• MATLAB is a programming and numeric platform, developed by
MathWorks
• The software is used by engineers and scientist to perform tasks such
as:
• Manipulation of Matrices and vectors
• Analysis data
• Plotting of functions and data
• Developing of algorithm
• Creation of models
• Solving mathematical equations
• Numerical computations etc
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
More about MATLAB
• The computational power of MATLAB empowers engineers and
scientist to attempt and solve more complex problems
• This course introduces students to the topics ranging from a
fundamental to an intermediate level knowledge of MATLAB
• Specific applications are collected in packages referred to as
toolbox.
• There are toolboxes for signal processing, symbolic computation,
control theory, simulation, optimization, and several other ¯elds of
applied science and engineering
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
More about MATLAB
• Students will use MATLAB in other courses such as Chemical
reaction Engineering (CHE 352/PCE 350), Numerical Methods for
Engineers (CHE 358), Process Control and Simulation (CHE 456),
Computer Optimization Methods (CHE 461)
• The symbolic toolbox in MATLAB can also be used in courses such
as differential equations (Math 251) and multi-variable calculus
(Math 252)
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Starting MATLAB
• MATLAB can be started by double clicking the icon on the desktop
• When the window opens, 3 standard MATLAB windows can be seen
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
MATLAB windows
• The command window is the main MATLAB window
• MATLAB commands after the ">>" prompt and presses enter to
execute the command
• Multiple commands may be entered on one line separated by
commas or semi colons
• The name of an M-file (will be explained later) can be entered in
the command window to be executed
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
More on MATLAB windows (Figure window)
The Figure window opens automatically when
graphic commands are executed and they contain
graphs created by these commands
More will be said about this window during the
discussion of Plots in MATLAB
Department of Chemical Engineering
More on MATLAB windows (Editor window)
The editor window is used for writing and editing
programs
The editor window can be used in creating script or
function files
Department of Chemical Engineering
Activities in the command window
• The simplest way to operate MATLAB is to
use it as a calculator
• This can be done by typing simple Operation Symbol
mathematical expressions in the Addition +
command window Subtraction -
• Before we do that me must know the Multiplication *
syntax for the basic mathematical Right division /
expressions in MATLAB Left division
Exponentiation ^
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Using MATLAB as a calculator
• Now that we have some basic
mathematical operations in MATLAB
• The use of MATLAB as a calculator will
be demonstrated in a quick tutorial
• Notice how the right division is
different from the left division
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Other basic commands
• There are other common commands used in the command
window
• The table below some basic MATLAB commands
Commands Purpose
clc Clears the command window
clear Clears the workspace of all variables
format Used to determine the format of numerical values
Exit/quit Used in closing the MATLAB
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Determining format in MATLAB
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Determining format in MATLAB
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Variables
• A variable is a tag you assign a value so that it can be stored in the
workspace
• Variables are fundamental to programming in MATLAB
• Once a variable has been defined it can be used in other
operations
• There are several predefined variables which can be used at any
time, in the same manner as user defined variables (ans, pi, eps, j)
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Variables
• The first thing will be to suggest a name for the variable
• The rules for choosing the name for a variable:
1.It may consist only of the letters a–z, the digits 0–9, and the
underscore ( _ ).
2. It must start with a letter
• Note: MATLAB is case sensitive, which means it distinguishes
between upper- and lowercase letters
• A1 is different from a1
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Creating of variables
• The first thing will be to suggest a name for the variable
• The rules for choosing the name for a variable:
1.It may consist only of the letters a–z, the digits 0–9, and the
underscore ( _ ).
2. It must start with a letter
• Note: MATLAB is case sensitive
Department of Chemical Engineering, Kwame
Created variables being stored in the
Nkrumah University of Science and Technology
Other basic features in MATLAB
• The semi colon ‘;’ is used in suppressing the output of a command
• This can be used when the user does not want to see the output
• If several commands are typed on the same line, they can be
separated by commas or semi-colons
• When a comma instead of semi-colon, all output will be shown
• However if a semi-colon is used none of them will be displayed
• The % is also used to indicate a comment, MATLAB ignores any
command that comes after ‘%’
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
The effect of semi-colons
All variables are displayed
in the command window
Only the variable without
the semi-colon is displayed
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Using the ‘%’
When the ‘%’ sing is used, the command is
ignored
Note: Comments are mostly used in scripts and not in the command window
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Other basic features in MATLAB
• The diary command can be used to save the entries on the
command window to a file
• This done by issuing the command diary and the name of the file
• Other important commands
• diary off: to switch off the diary
• Diary on: to switch on the diary
• Type filename: to recall a diary
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Using the diary command
A diary known as class_example is created
The diary being filled with contents
When the ‘%’ sing is used, the command is
ignored
Diary is switched off
Content of the diary ‘class_example’ is recalled
Note: Comments are mostly used in scripts and not in the command window
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Elementary MATLAB in-built maths functions
• In addition to the basic arithmetic operations………
• MATLAB has several in-built functions that can be used in scripts
or directly in the command window
• Just typing a function with its argument in the command window
and pressing enter will return the answer
• Functions can take numbers, assigned variables and other
functions as arguments
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Examples of MATLAB functions
sind(x) returns the sine of the angle x, for x is in
degrees
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Examples of MATLAB functions
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology
Summary
• In this lecture, we have been introduced to MATLAB
• We are also know some basic things about MATLAB such starting
MATLAB, MATLAB windows, basic MATLAB commands, variables
etc.
• Thank you for your cooperation
Department of Chemical Engineering, Kwame
Nkrumah University of Science and Technology