COMPUTER
PROGRAMMING
An Introduction
CONTENTS: INTRODUCTION
Arithmetic Operations
Variables
Mathematical Functions
Points to consider
WHAT IS MATLAB?
MATLAB® /Simulink® is a powerful software
tool for: Performing mathematical
computations and signal processing
Analyzing and visualizing data (excellent
graphics tools)
Modeling physical systems and phenomena
Testing engineering designs
INSTALLING MATLAB
Please install MATLAB on
your computers by Next
Class
MAKING FOLDERS
•Use folders to keep your programs
Organized.
Click on the arrow close to “Current Folder”
Select the “New Folder” and open MATLAB CLASS
Open Another folder under MATLAB CLASS and
name it Day 1.
SCRIPTS
Scripts is a sequence of MATLAB commands written in the
MATLAB editor.
Make a Script and give it a name of your choice
The Script should the read the following using the disp:
Hello World!
I am going to learn MATLAB!
Use “%” to insert comment
WHAT CAN YOU DO WITH MATLAB?
• It can be used for Arithmetic Operations. But always
remember B(E)ODMAS. You can try to do the following:
Perform the division operation 12/5.
Perform the division operation (12/5) symbolically.
Perform the multiple operations (15 – 4) + 12/5 –
(2*7)^4/100
Calculate the area and circumference of a circle with
a radius of 4 cm.
VARIABLES
• A way of labeling data with a descriptive name, so
programs can be understood more clearly by the reader
and ourselves.
It is helpful to think of variables as containers that hold
information.
Variables are assigned using the “=“ sign
Don't confuse the assignment operator “=“ with the
equality operator ”==“. The individual “=“ symbol
assigns value while the “==“ symbol checks if two things
VARIABLES (CONT’D)
The following are the MATLAB rules for naming
variables:
MATLAB is case-sensitive with respect to variables.
All variables must start with a letter.
You may have up to 31 characters in a variable.
Punctuation signs are not allowed in a variable.
The underscore sign “_” is allowed in a variable.
Both letters and digits are allowed in a variable.
LET’S CREATE VARIABLES
oLet r = 6.3 and s = 5.8. Calculate the value of the
variable final defined by final = r + s-r*s.
oDefine the variable centigrade equal to 28 then calculate
the variable Fahrenheit defined by Fahrenheit =
(centigrade*9/5) + 32.
oPerform the fraction addition (2/3) + (3/4) numerically.
oPerform the fraction addition (2/3) + (3/4) symbolically.
oPerform the who command to get a list of the variables
stored in this session.
OVERWRITING VARIABLES
•You can always reassign a value to
your variable.
• >>t = 1
• >> t = 2+3
•You can also suppress a variable
by using a semicolon (;)
IMPORTANT COMMANDS TO NOTE
clear clears all variables in the MATLAB® workspace
clear a, b just clears variables a and b
clc clears the command window
i and j are defined in MATLAB to be √(−𝟏). If you
define these variables to be something else, you lose
the ability to work with complex numbers. So, avoid
using i and j as variables.
pi is defined in MATLAB as 3.14159….
IMPORTANT COMMANDS TO NOTE
format Long and short
Semi-column can also be used to
enter many statements per line
>>a = 4 ; b =5; c =0.4
ERROR MESSAGES
• If we enter an expression incorrectly, MATLAB will return an error
message. For example,
• in the following, we left out the multiplication sign, *, in the
following expression
• >> x = 10;
• >> 5x
• ??? 5x
•|
• Error: Unexpected MATLAB expression.
HELP!
The help command provides information about
a function. Type help cos at the command
prompt. This only works if you know the name
of the function you want help with.
You can also click on Help (It is just a question
mark) in the MATLAB toolbar and search for
information by keyword(s).
MATHEMATICAL FUNCTIONS
These are functions embedded in MATLAB. Some
MATLAB functions are the square root function, the
factorial, the trigonometric functions, the exponential
functions, and the rounding and remainder function.
In MATLAB, angles are represented in radians. So it
is important to convert angles to radians to obtain the
desired results
I. Cos 30
II. Cos (30*pi/180)
MATHEMATICAL FUNCTIONS (CONT’D)
MATHEMATICAL FUNCTIONS
tan(pi/2)
round(4.57)
rem(10,4)
abs(-7)
sym(tan(30*pi/180))