Done, your profile is created.Finish your profile by filling in the following fields
Forgot Password Earn Money,Free Notes
Password sent to your Email Id, Please Check your Mail
Updating Cart........ Please Wait........
Introduction to Matlab Programming with Applications
Introduction to Matlab Programming with Applications 4
Introduction to Matlab Programming with
Department of Computer Science and Information Engineering
National Taiwan University
Winter Break, 2015Class Information
Ocial Web page
Lecture notes will be uploaded right before class.
Note that the lecture notes are organized in English.
If you have any question, please contact me via email.
ZhengLiang Lu 1/48Prerequisites
No programming experience required.
It would be helpful if you have some programming experiences.
Linear Algebra is strongly recommended.
MATLAB refers to MATrix LABoratory.
Linear Algebra is widely used in numerical analysis and
Please be informed that you should consider other classes if
you want to learn any advanced computer science courses.
I promise to keep everythingsimple in this class.
ZhengLiang Lu 2/48Grading
To pass the class, you need at least60 pts in the end of
Programming assignment (40 pts)
Final project/exam (60 pts)
Please be noted that you preserve the right to refund before
the 5th lecture.
ZhengLiang Lu 3/48Final Project
Topic: anything if you are interested in or working on.
Requirement: you should use MATLAB as the main
Your grade depends on the following aspects:
Solve a problem by formulating the problem, organizing into an
algorithm, implementing a program by MATLAB
Get familiar with programming concepts
Gain the experiences in order to learn more programming
language by yourself.
ZhengLiang Lu 4/48Final Exam
No communication with classmates and your friends
ZhengLiang Lu 5/48Who/How/Why
ZhengLiang Lu 6/48Articles about Learning Programming
5 REASONS YOU NEED TO LEARN TO PROGRAM
The reason everyone should learn to code
Why Learn to Program
ZhengLiang Lu 7/48Programs
A (computer) program is a sequence of instructions, written in an
articial language, to perform a specied task with a computer.
They are almost everywhere such that you can name it.
Operating systems, such as Windows 8.1, UNIX/Linux,
Android (family), OS X and iOS
Social networking, such as facebook, twitter, and so on
The dierences are the goal and functionality.
ZhengLiang Lu 8/48How and Where The Programs Run
The programs are activated from the disk into the main
Now we call them the processes.
All the instructions with data are executed in the registers in
The results are written back to the main memory and further
stored into the disk after the processes are terminated.
ZhengLiang Lu 9/483
See Bryant, p. 14.
ZhengLiang Lu 10/48Programming Languages
A programming language is an articial language designed to
communicate with a machine by instructions.
Programming languages syntax + semantics
Used to express algorithms
Used to control the behavior of a machine
ZhengLiang Lu 11/48How Many Programming Languages
More than 100
In historical perspective,
1st generation: machine code
2nd generation: assembly code
3rd generation: highlevel programming languages
Beyond 3rd generation
MATLAB belongs to post3rdgeneration programming
You can refer to TIOBE Programming Community Index.
ZhengLiang Lu 12/48ZhengLiang Lu 13/481stGeneration Programming Languages
A 1stgeneration programming language is a machinelevel
The instructions are made of binary numbers, represented by
1s and 0s.
There was originally no compiler or assembler to process the
ZhengLiang Lu 14/48Pros:
Most ecient for machines
Hard to program for human
Still widely (and occasionally) used in programming lower
level functions of the system, such as drivers, interfaces with
rmware and hardware devices.
Aka machine code.
ZhengLiang Lu 15/482ndGeneration Programming Languages
2ndgeneration programming language is a generational way
to categorize assembly languages.
The code can be read and written by a programmer.
Yet, it is still machinedependent.
To run on a computer, it must be converted into a machine
readable form, a process called assembly.
More often nd use in extremely intensive processing such as
games, video editing, graphic manipulation/rendering.
ZhengLiang Lu 16/483rdGeneration Programming Languages
A 3rdgeneration language aims to be closer to the human
Instructions operate at a higher, abstract level, closer to the
human way of thinking.
Each individual instruction can be translated into a number of
machinelevel instruction via a specic compiler.
More friendly to understand, easier to learn
Support structured programming
5 6 7
Such as C , C++ , and Java
Aka highlevel programming languages
ZhengLiang Lu 17/48What Can A Program Do
A program is an implementation of an algorithm expressed in a
specic programming language.
ZhengLiang Lu 18/48Algorithm In A Nutshell
An algorithm is a sequence of unambiguous instructions for
solving a problem, that is, for obtaining a required output for any
legitimate input in a nite amount of time.
Simply put, an algorithm is a procedure that solves a
particular type of problems, such as a cookbook.
See Algorithm in Wikipedia.
ZhengLiang Lu 19/48Properties of Algorithms
Typical properties of any algorithm are summarized as follows:
Number of steps: niteness
Note that an algorithm is not necessarily expressed in a specic
Instead, one can express an algorithm in some human
language using a programming language format.
Alan Turing (19121954)
ZhengLiang Lu 20/48Example
Organize an algorithm that nds the greatest element in the input
Input: A (a list of n integers)
Output: x (the greatest element in A)
1 x A(1)
2 for i 1 n
3 if A(i) y
4 x A(i)
7 return x
ZhengLiang Lu 21/48\Computers are good at following instructions,
but not at reading your mind."
Donald Knuth (1938)
ZhengLiang Lu 22/48MATLAB In A Nutshell
In my view, MATLAB is a powerful and simple math software:
Powerful: programmable, fruitful toolboxes available
Simple: don't need to worry about the machine, easy to draw
The only thing you need to provide is ...
Simple is not easy.
ZhengLiang Lu 23/48Algorithms
ZhengLiang Lu 24/48To Whom It May Concern,
If MATLAB is your rst programming language,
Pros: easy to learn
Cons: may feel frustrated when learning other (lower)
If MATLAB is not your rst programming language,
Pros: extremely easy to learn (Warning: It may be boring.)
Cons: cannot get rid of it because it is too easy...
The core concepts of programming are the same among
various programming languages.
The most important thing is your algorithm.
ZhengLiang Lu 25/48Conclusions
Program design algorithm + programming language
Algorithm idea, steps
Programming language Chinese, English
In this class, we will learn MATLAB and some algorithms which
are the core of programming world.
ZhengLiang Lu 26/48Father of MATLAB
ZhengLiang Lu 27/48Get started to
implement your idea with
ZhengLiang Lu 28/481 Lecture 1
3 Overview of MATLAB
ZhengLiang Lu 29/48\Learning a new language is harmless. It gives you new
ideas and insights."
Yukihiro Matsumoto (,L) (1965)
ZhengLiang Lu 30/48Contents
Character arrays (String)
Builtin arithmetic functions
ZhengLiang Lu 31/48Working Environment
ZhengLiang Lu 32/48Command Window
You can give a command in the Command Window.
Let's try a greeting, \Hello, MATLAB."
1 disp('Hello, MATLAB.');
disp prints the string as input in the parenthesis.
A string is singlequoted in MATLAB.
Convention in the slides:
Boxes are the listings for programs.
Words and sentences highlighted in red are important.
Words in blue are reserved words.
Bold words in black are functions.
ZhengLiang Lu 33/48Tips for Learning MATLAB
Don't be frustrated:W/ÅKï
MATLAB interrupts your program when a syntax error occurs.
lookfor is used to look for the commands related to the key
word you give.
1 lookfor display
3 disp Display array.
4 display Display array.
5 error Display message and abort function.
6 warning Display warning message; disable or ...
enable warning messages.
help returns the usage of a command
ZhengLiang Lu 34/481 help disp
Help browser provides detail of commands with examples.
Google is your friend.
ZhengLiang Lu 35/48Script Editor
We often write a program in the script editor.
Save the program in .m le.
.m les are used as scripts and functions.
A script is a complete program which contains the input data
and can be executed directly.
A function is a program which may need input data when
being called in other programs. (why)
ZhengLiang Lu 36/48ZhengLiang Lu 37/48helloworld.m
Let's create a .m le with the following code lines:
1 This is the comment section.
2 clear; Clear all variables stored in Workspace.
3 clc; Clear the screen.
4 main program
5 disp('Hello, world.');
The lines which begins with are regarded as the comments.
Comments are not executed.
clear is used to release the variables.
clc cleans the command window.
ZhengLiang Lu 38/4812 13
Save and press F5 to run the script.
Do not give a script le the same name as a variable. (Why)
Do not give a lename with blanks in the middle.
If the .m le is not at the current folder when you execute it,
MATLAB will ask:
Add to Path Pool
You can run helloworld.m later just by calling helloworld in
the command window.
3 Hello, world.
ctrl + s
Autosave before execution.
ZhengLiang Lu 39/48Comments In Scripts
Anything following a is seen as a comment.
Press ctrl + r to comment.
ctrl + t to decomment.
To comment a block of lines, f g.
The rst contiguous comment becomes the script's help
1 help helloworld
3 This is the comment section.
Document is helpful for both the programmers and users.
Comment thoroughly to avoid wasting time later.
ZhengLiang Lu 40/48Variables
Variable is used to store a value, assigned by
variable name = expression:
x = x + 1 is correct. (Why)
Actually, the assignment operator is used to put a value in the
variable, from right to left.
For example, let a = 10;b = 20. Then a +b = 30.
1 a = 10;
2 b = 20;
3 a + b
5 ans =
ans is the default variable if you don't assign one for it.
ZhengLiang Lu 41/48If you drop the semicolon (;) in the line, then the result will
appear in the command window, say,
1 a = 10
3 a =
Note that you may not drop semicolons in other programming
The variable name is suggested to begin with a letter of the
Cannot begin with a number, e.g. 1x is wrong.
Cannot have a blank, e.g. x 1 is wrong.
Cannot have +n, e.g. x1
ZhengLiang Lu 42/4814
MATLAB is casesensitive , e.g. A and a are dierent.
The length of name is limited to 63 characters .
namelengthmax returns what this maximum length is.
We avoid to use names of reserved words and builtin
The reserved words cannot be used as a variable, e.g. for, if,
i = 1 and j = 1 by default.
If you set i = 1, then i = 1 until you clear i.
Variable names should always be mnemonic .
In general, programming languages are casesensitive, but some are not,
Windows 7 (64bit) in this case
To assist your memory.
ZhengLiang Lu 43/48Data Type and Casting
Dierent types of variables occupy dierent size of memory.
All numeric variables are stored as the type double by
Variables dened in type char are of size 2 bytes.
Variables dened in type logical are of size 1 bytes.
whos shows variables that have been dened in the command
cast converts one variable to another type.
int8, int16, int32, and int64 are the integer types.
8 bytes = 64 bits.
ZhengLiang Lu 44/48Computational Limits
ow occurs when the value is beyond the limits.
ZhengLiang Lu 45/48Scalar Variables
A scalar is a single number.
A scalar variable is a variable that contains a single number.
x is said to be a scalar variable if x2R orC .
The complex eld, denoted byC, contains all the complex
numbers, in form of
where i = 1, and a;b2R .
Try x = 1 +i in the command window.
In math,C is equivalent toR .
So, x is stored as the type double with 16 = 8 2 bytes.
ZhengLiang Lu 46/4819
Scalar Arithmetic Operators
See Table 2.1 Arithmetic Operations Between Two Scalars in Moore, p. 21.
ZhengLiang Lu 47/4820
In MATLAB, it is a common convention to use e to identify a
power of 10, e.g. 1e5 = 100; 000.
See Table 1.15 Numeric display formats in Palm, p. 15.
ZhengLiang Lu 48/48