Sunteți pe pagina 1din 20

MH2401 Algorithms & Computing III Slides 1

Chua Chek Beng Henk Hollmann Punarbasu Purkayastha

August 19, 2013

Topics for today

Introduction to MATLAB Simple calculations in MATLAB Variables in MATLAB

What is MATLAB?

MATLAB is a programming environment for algorithm

development, data analysis, visualization, and numerical computation. (http://www.mathworks.com/products/matlab/)


We will be using MATLAB primarily for visualization of

various mathematical concepts and for numerical computations.

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

 Backup your les to an external drive or USB drive!

The Command Window

Current Folder Contents

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

in the command window.


Give it a nice descriptive name that you can remember. Use

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.

Run any mathematical computations


>> 10 9 8 7 6 5 4 3 2 1 ans = 3628800 >> sin ( ans ) / cos ( ans ) == tan ( ans ) ans = 1 >> a = f a c t o r i a l ( 1 0 ) ans = 3628800 >> tan ( a ) ans = 0.2736

Declaring variables

Variables can be declared in MATLAB by simply assigning

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

Declaring variables - Examples


>> a = 1 a = 1 >> A = cos ( 1 ) A = 0.5403 >> c = a /A c = 1.8508

Declaring variables - Examples


>> A = a s t r i n g of characters ) A = a string >> isstr (A) % isstr () checks whether a variable is a string ans = 1 >> isstr ( a ) % a is a just a number 1, so isstr returns 0 ( false ) ans = 0 % A is now a string (an array

Variables and commands

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.

In-built functions and reserved keywords


Some variables and many functions are predened in

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.

In-built functions and reserved keywords


Some variables and many functions are predened in

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.

In-built functions and reserved keywords

>> i s k e y w o r d ( i f ) ans = 1 >> i s k e y w o r d end % another way of calling iskeyword ans = 1

In-built functions and reserved keywords

>> which i b u i l t i n (C : \ Program F i l e s \MATLAB\ R2011b \ t o o l b o x \ m a t l a b \ e l m a t \ i ) >> i ans = 0 + 1.0000 i

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

The cleared variables will disappear from the Command

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

documentation about the command or function.


This window can also be opened by clicking on the ? icon in

the main MATLAB window.

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

1. Look up the help and documentation of


help, doc, iskeyword , which, clc, clear

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

S-ar putea să vă placă și