Documente Academic
Documente Profesional
Documente Cultură
What is MATLAB?
Getting MATLAB
1. Use the machines in the Computer labs 1 and 2, where MATLAB is pre-installed 2. Use your own laptop and use remote desktop to the server apple.spms.ntu.edu.sg
Getting MATLAB
1. Use the machines in the Computer labs 1 and 2, where MATLAB is pre-installed 2. Use your own laptop and use remote desktop to the server apple.spms.ntu.edu.sg
Command Window
Command Workspace
Command History
Main MATLAB window has a command-line-centric interface Command Window - the main area - input commands here Command History - commands appear chronologically here Command Workspace - variables that are declared and used.
The
diary
command
Use the diary command to record all your input and output
single quotes.
>> diary l a b 1. t x t
This will dump all your input and output into a le called lab 1.txt Use diary o or diary on to toggle the recording o or on,
respectively.
Declaring variables
values to them.
The type of the variable need not be declared. Variable names must start with a letter of the English
alphabet and may otherwise contain numerics, underscore, and other letters. Correct variable names: an iter , var1 , x1 derivative Incorrect variable names: 1var , avar, var
Variables and commands in MATLAB are case sensitive. Two variables a and A are dierent. Two dierent function names such as cos and Cos - cos is a predened internal function that computes the cosine of a number - Cos is undened.
MATLAB and should not be used as variable or function names in your own programs.
1. Variables i and j are predened to be the complex number 1 2. if, while, end, else, function, etc are reserved keywords. You will get an error if you try to redene them.
MATLAB and should not be used as variable or function names in your own programs.
1. Variables i and j are predened to be the complex number 1 2. if, while, end, else, function, etc are reserved keywords. You will get an error if you try to redene them. To check whether a particular name is a keyword in MATLAB, use the function iskeyword Typing simply iskeyword by itself will list all the keywords. Use the function which to check whether a function or
variable is predened.
Clearing variables
Use the command clc to make the command window blank. - This does not clear the variables from memory Use the command clear to clear the variables from memory. >> clear a % clears variable a >> a % invoking a will give an error U n d e f i n e d function o r v a r i a b l e a . Clearing multiple variables - separate variable names by spaces >> clear A c >> clear % clears variables A and c % clears all the variables
Workspace
Help!
The documentation for any command in MATLAB can be obtained in two ways. 1. Type help <command or function name> to get a quick inline documentation 2. Type doc <command of function name> to get the documentation to open in a separate documentation window.
This separate window usually displays enhanced
Help!
Use the help and doc commands as much as possible! Many examples are present in the documentation Good for learning new commands by yourself Discover new commands by using tab completion! - For example press the Tab key after typing cos
Exercises
2. What is the output of the command iskeyword ? 3. Bob wants to compute the dierence between a real number and its oor. He is using the floor function of MATLAB. However, he is getting an error in the last line. What is the problem?
>> v a r 1 = 1 2 . 1 0 >> v a r 1 f l o o r = floor ( v a r 1 ) >> v a r 1 v a r 1 f l o o r = v a r 1 v a r 1 f l o o r