You are on page 1of 25

Week1: Introduction to

MATLAB/Octave
CES 513 : Computational
Analysis For Engineers (Lab)

Ts. Syahrul Fithry Senin


COURSE LEARNING OUTCOMES

At the end of this lecture, all students must master the


the following skills ;

• to understand the MATLAB desktop layout (CO2:PO5)


• to use the MATLAB workspace (CO2:PO5)
• to write create variables and mathematical
expression in MATLAB (CO2:PO5)
• to perform script editing, saving, printing and other
file manipulations operation (CO2:PO5)
OBE Briefing
• https://sway.office.com/TssjqrOtkdCqCZK8?ref
=Link&loc=mysways
INTRODUCTION
What is
Matlab/Octave Why do I
? need to
learn ‘it’?

Because the
course leader said
so…
What is
Matlab/Octave?

What is a computer
program?
What does computer
programming mean?

What is a
programming
language?

What is code?
What is a computer
program?

In civil engineering…

DATA

PROGRAM RESULT
Concrete
Material Prediction of
Damage data damage index of
a concrete
Do something clever… material
What is a computer
program?

In civil engineering …

DATA

PROGRAM RESULT

Predict whether
the crack is
severe/mild
Concrete damage images damage
Measure length of
crack…
What is a computer
program?

In Administration…

DATA

PROGRAM RESULT

Predict student
performance in
course
Data of students marks

Measure average
marks/highest
marks/student
performance
What does computer
programming mean?
Writing your own computer programs...
… telling the computer to do exactly what
you want.

A way of translating human logic into What is a


commands a computer understands… programming
language?
… like human languages, there a lots of
different languages (often similar to each
other), each with a specific set of rules
(syntax, grammar) to obey.

A chunk of commands in a specific


programming language…
What is ‘code’?
A program consists of bits of code put
together in a logical way …
… by using other people’s code, you can incorporate
bits of their program in your own (as long as you’re
using the same language!).
And finally….
What is
Matlab/Octave?

Matlab/Octave is both a program and a programming language ...


… it has lots of tools you can just use to analyse data and
images. But you can also write code to extend it to do any
analysis you like (although unlike a ‘pure’ language, it will only
work if the Matlab program is installed on the computer).

Excel Photoshop
Using other Writing
+ + people’s
code
+ your own
code

= A really powerful tool for imaging research!


Lesson Overview
• Using the Matlab/Octave interface (or try Spyder)
• Some simple commands in the Command Window
• Assigning variables and suppressing visual output
• Collecting sequences of commands: scripts
• Basic programming tips: expressive variable names
and comments
Matlab Command window

Directory
Workspace

Command history
Octave Command window

Directory

Workspace

Command history
Command window
• A fancy scientific calculator
– 2+3
– 2*3 Any of the text in navy blue is code
– 2/3 you can run in the command
window. Try copy and pasting, then
– 2^3 hitting return
– 2^0.5
– 2+3*6/2
– (2+3)*6/2
• Try using ↑ and ↓
– Select earlier expressions and edit them
Assigning Variables
• Use ‘=‘ to assign variables
– Keeps data in memory to use again
–a=4
–b=3+6
– y = (4*a^2 +3*b^3) / 5
• Can also self-assign:
– b = b*2
• Check the work space
Workspace

Shows you what variables are currently stored in memory


Suppressing visual output
• Try a = rand(100)
• This creates a 100 x 100 matrix of random
numbers
• Use clc to clear the Command Window
• Try a = rand(100);
Scripts
• Use clear to wipe the current memory
– Check that the workspace is now empty
• Click the ‘New script’ button (top left of main menu)
– Opens a blank page in the editor
• Copy the previous commands from the Command
History
• Paste them into the blank document
• Save the document as ‘my_first_script.m’
• Type my_first_script (note the lack of ‘.m’) at the
command line
Scripts (cont.)
• Scripts allow us to run sequences of commands
• All data is stored in the main workspace, as if we
typed the commands in the command window
• We can run parts of scripts by
– Selecting text and hitting F9
– Using %% to create ‘cells’

Even when ‘hacking around’ use scripts, date tagged


(e.g. work2013_02_11) to run commands
– That way you have a record of your work
– Think of them as your Matlab lab book
How does Matlab/Octave work?
• Matlab/Octave interprets each command it
sees
• It recognises certain keywords, mathematical
operators etc.
• When it sees something that isn’t a keyword it
1. Checks if it is a variable in the current memory
space
2. Looks for a script in the Matlab/Octave path
3. If it can’t find either, returns an error
The Matlab/Octave path
• Type ‘path’ at the command line
• This displays all the folders on your computer where
Matlab/Octave will ‘look’ for functions and scripts
• Use ‘File -> Set path’ to add new folders
• If 2 scripts have the same name, Matlab/Octave uses
the first one it finds on the path
– Avoid name clashes with existing functions
– Avoid mixing variable and function names
Variable, script and function names
• Must start with a letter
• Followed by any number of letters, digits, or
underscores.
• Matlab is case sensitive
– A and a, my_fun and My_fun, etc are not the same name
• Certain keywords cannot be used
– if, for, end, etc
• Be expressive: try and use names that
– Describe what functions do
– Describe what variables are
Variables access and deletion
• Example: Suppose we have three variables
CES522 subject_sem and REGIster
• To access the value of CES522, just type
CES522 in the command window and press
ENTER OR right click the mouse on this
variable and view it value
• Use clear command to delete the stored
variable, such as clear CES522 subject_sem
• Use clear all to delete all variables in
workspace
Organising your scripts
• Use scripts to generate results/output for specific tasks
– Assignments in your maths course
– Experiments in your project
– Give them a sensible name, and add comments at the start
describing what they do
• Use functions for methods that can be reused across
multiple tasks
– Organise them in sub-folders
• E.g. ‘stats’, ‘optimisation’, ‘image_processing’
• Remember, you can always rearrange the file structure
– As long as you remember to add any new folders to the
path
THANKS!
Does anyone have any questions?

syahrul573@uitm.edu.my

You might also like