Sunteți pe pagina 1din 40

Chapter – 1

Introduction to MATLAB
Desktop

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Chapter – 1
Introduction to MATLAB
Desktop

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


After studying this chapter you should be
able to:

 Gain an overview of MATLAB desktop


 Use help for executing MATLAB commands /
functions
 Write basic MATLAB code and create m-files
 Use MATLAB demos

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


What is MATLAB?
Matlab is a high level language which has many specialized
toolboxes for making things easier for us. How high?

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


Matlab

High Level
Languages such as
C, Pascal etc.

Assembly

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


MATLAB = MATrix LABoratory

Official Website:

www.mathworks.com
or

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


www.mathworks.in

Advantages of MATLAB
• Ease of use
• Platform independence
• Predefined functions in varied domains
• Integrates Programing, Plotting, Simulation and
Hardware Interface

Disadvantages of MATLAB
• Can be slow
• Commercial software

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Getting Started

To start MATLAB, double-click the icon named MATLAB on


your PC’s desktop and the MATLAB working environment pops

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


up. This is an interactive environment, i.e., you can start
entering commands and obtain results instantly. In the
MATLAB environment, you can see a ‘menubar’ at the top of
the screen and the ‘command window’ along with some other
windows occupy the rest of the screen. In the MATLAB
command window, a command can be executed by typing it at
the MATLAB prompt ( >> ) and pressing the ENTER key.

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


The MATLAB Desktop

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
The Command Window

A user can assign values to the variables and write


expressions at the command prompt (>>) in the

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


command window and they can be executed
instantaneously. As a simple example, try entering the
following commands and observe the results in your
MATLAB command window:

>> a = 2
>> b = 4;
>> c = b+a^2

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Variables in MATLAB

No need for types. i.e.,


int a;
double b;

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


float c;

All variables are created with double precision unless


specified and they are matrices.
Example:
>>x=5;
>>x1=2;

After these statements, the variables are 1x1 matrices


with double precision

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Variables and Arrays

Array: A collection of data values organized into

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


rows and columns, and known by a single name.

Row 1

Row 2

Row 3
arr(3,2)
Row 4

Col 1 Col 2 Col 3 Col 4 Col 5

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Arrays :
The fundamental unit of data in MATLAB

Scalars are also treated as arrays by MATLAB (1 row and 1

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


column).

Row and column indices of an array start from 1.

Arrays can be classified as vectors (one dimensional) and


matrices (Two- Dimentional).

Size of an array is specified by the number of rows and the


number of columns, with the number of rows mentioned first
(For example: n x m array).

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Variables in MATLAB

Variable names must begin with a letter, followed by any


combination of letters, numbers and the underscore (_)
character.

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


The MATLAB language is Case Sensitive: NAME, name and
Name are all different variables.

Give meaningful (descriptive and easy-to-remember) names


for the variables. Never define a variable with the same
name as a MATLAB function or command.

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Common types of MATLAB variables

double: 64-bit double-precision floating-point numbers


They can hold real, imaginary or complex numbers in

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


the range from ±10-308 to ±10308 with 15 or 16 decimal
digits.
>> var = 1 + i ;

char: 16-bit values, each representing a single character


The char arrays are used to hold character strings.
>> comment = ‘This is a character string’ ;

The type of data assigned to a variable determines the


type of variable that is created.
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
The Command History Window
The Command History window displays a list of commands
that a user has entered in the command window.

The commands remain in the history until they are

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd



deleted. A section of these commands can be executed
again by selecting it using (Shift + Arrow) keys.
• To execute the previously executed commands, press
‘UP (↑)’ arrow key at the command prompt.
• Typing the first letter/word of any previous entry,
followed by an UP (↑) arrow displays all the pervious
entries beginning with that letter/word in sequence.
• The command can then be re-executed by pressing
ENTER.

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


The Command History Window

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Current Directory Browser
You can set the current working directory of MATLAB by
typing the path of a particular folder over the Current

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


Directory window in the MATLAB Desktop. Alternatively,
you can also use the icon to browse and set

the current working directory. Then set the current


directory to a folder where your files are stored.
MATLAB would look for files for execution in current
working directory.

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Workspace and Array Editor

When you issue commands like:

>> x = 10;

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


>> y = [1 2 3 4]; % Array variable

• MATLAB creates variables named x and y, and saves


them in a part of computer memory known as
Workspace.
• The symbol ‘%’ is used in MATLAB for inserting the
comments for the clarity of the codes.
• Anything written after % in a line is not executed (even
if it is an executable statement or expression).

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


• You can observe the contents of the Workspace by typing
the whos command

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


• Double-clicking on any variable in the Workspace Browser
Window brings up the Array Editor which allows the user
to observe and modify the information stored in the
variable. Any variable on Workspace can be manipulated
using the array editor.

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
The Help Browser
The “?” icon on the MATLAB desktop launches the Help
Window, which provides links to the documentation on
all the components (or toolboxes) for your MATLAB

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


version. It provides an important and dynamic reference
for working on MATLAB.

There is another simpler way to get help on different

functions e.g. by typing >> help sqrt at the


command prompt you get the help on function ‘sqrt’.

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
The Edit/Debug Window

The Editor/Debugger is a single tool that can be used for


editing, debugging, or both.

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


You can open a New File in the Editor Window using the
toolbar:

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Instead of writing the commands directly in the Command
Window, a series of commands may be placed into a file
known as script file or M-file (saved with extension “.m”). The
entire file can be executed just by typing its name (without

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


extension name) at the command prompt. For example,
consider the following file ‘simple.m’:

-----------------simple.m--------------------
x = 10;
y = x/4;
z = y*9;
------------------------------------------------
This can be executed in command space in the following
manner (assuming the file is in the current working directory):

>> simple

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Path Browser
• The Path Browser allows you to view and modify the
MATLAB search path.
• It allows the user to add, delete, or change the order of
directories in the path.

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


• Make sure that the folder where you have saved your M-
file exists in MATLAB Path, otherwise it won’t be
executed.
• If there are more than one functions, commands or script
files (M-files) with the same name, the first one found in
the search path will be executed and all the others will be
inaccessible.
It is extremely important not to define a variable with
the same name as a standard MATLAB function or
command, which will make that function inaccessible !!
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Getting Help from Command Window

To access help from command window, type ‘help’ at the


MATLAB prompt and press ENTER. e.g.

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


>> help
>> help plot

To search a particular topic by title, the lookfor command


proves useful. For example,

>> lookfor integral

The output of this is a list of several functions that are related


to integrals and contain the word ‘integral’ in their help
documentation.

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Other important commands with similar purpose are:

what : lists the MATLAB related files found in the current


working directory,

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


dir : lists all files in the current working directory,

which FUNCT : displays the full pathname of the function


with the name FUNCT.

You can also learn how to effectively use help command by


typing the following:

>> help help

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


General Syntax
• MATLAB automatically creates a variable in the memory as
soon as it is assigned a value.
• The variable names must begin with a letter, followed by
any combination of letters, numbers and the underscore (_ )
character.

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


• Variable names are case sensitive, in the sense that the
uppercase and lowercase letters are distinguished. For
example, “MULT”, “mult” and “Mult” are three different
variables.
MATLAB requires that all variable names (except for those used
by symbolic toolbox) be assigned numeric values prior to being
used in an expression. For example:
>> a=2.3;
>> b = 4.7;
>> d = c + a*b;
MATLAB responds as:
??? Undefined function or variable 'c'.

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Several variables (or expressions) can also be defined in the
same line, separated by a comma (,) or a semicolon (;). For
example, when you type the following:

>> x=1.1, y=3.3, z=5.5

MATLAB responds with the following display:

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


x =
1.1
y =
3.3
z =
5.5

The use of semicolons in place of the commas would suppress


the outputs, e.g.

>> x=3.5; y=1.9; z=2.75;

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Writing Simple Expressions
Unlike many other programing languages, you need not
declare the type of values a variable can take in MATLAB.
For example, a variable a can take an integer value, and
in the next step, you can assign the same variable a float
value and in the third step, you can assign it a string and

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


so on. Here is an example:
>> newvar = 5
newvar =
5
>> newvar = 20.197
newvar =
20.1970
>> newvar = 'surabhi'
newvar =
surabhi

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Operators (arithmetic)

+ addition

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


- subtraction
* multiplication
/ division
^ power
‘ complex conjugate transpose

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Writing Simple Expressions

variable_name = expression :

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


addition a+b  a+b
subtraction a-b  a-b
multiplication axb  a*b
division a/b  a/b
exponent ab  a^b

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Hierarchy of operations
Guess what is value of this ? :
x=3*2+6/2

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


Processing order of operations is important

– parentheses (starting from the innermost)

– exponentials (from left to right)

– multiplications and divisions (from left to right)

– additions and subtractions (from left to right)

>> x = 3 * 2 + 6 / 2
x =
9
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
The parentheses ( ) are used in complex operations. Other
braces (such as { } and [ ] ) are reserved for special
purposes in MATLAB. Here are some examples of writing
simple expressions:

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


Mathematical Expression MATLAB Code

1. x + 2y – 3 (y-x) x + 2*y – 3* (y-x)


2. 2 x3 + 5/x5 2*x^3 + 5/x^5
3. 5 x1/3 + 9 y0.173 5*x^(1/3) + 9*y^(0.173)
4. 3 tan(x)/ (1+ sin2(x)) 3*tan(x)/(1+(sin(x))^2)

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Screen Display Control (The "format"
function):

MATLAB performs numeric computations in double-precision


format. The number of digits after the decimal of a floating

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


point number can be controlled by using the format command.
>> format % this is same as 'format short', the
% default format
>> pi % pi has a pre-defined value in MATLAB
ans =
3.1416
>> format long % change the display format to long
>> pi
ans =
3.14159265358979

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


>> format long e
>> pi
ans =

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


3.141592653589793e+000
>> format hex
>> pi
ans =
400921fb54442d18

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


>> format compact
>> x = 2, y = 3,
x =
2
y =

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


3

This results in the values being displayed, but the


display is compact to save your Desktop area.

See >> help format for more details on the other


display formats available with MATLAB

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Changing the data format

>> value = 12.345678901234567;

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


format short  12.3457
format long  12.34567890123457
format short e  1.2346e+001
format long e  1.234567890123457e+001
format short g  12.346
format long g  12.3456789012346
format rat  1000/81

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


MATLAB Demonstrations

MATLAB is shipped with a number of demonstration programs.

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


Use >> help demos to find out more about these. You can
also type >> demo on the MATLAB prompt to enter the
demonstration programs. The number of demos depends upon
the version of MATLAB that you have.

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


How to Quit MATLAB ?

You can leave a MATLAB session by typing quit or by typing


exit at the MATLAB prompt. If MATLAB session enters into an

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


infinite loop, you can interrupt the running of the program by
a “Ctrl+c” command the from keyboard, and thus you would
get back to the command prompt.

Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal


Summary (some important commands)
help command  Online help

lookfor keyword  Lists related commands

Copyright © 2014 Dorling Kindersley (India) Pvt. Ltd


which  Version and location info

clear  Clears the workspace variable

clc  Clears the command window

who, whos  Lists content of the workspace

ctrl+c  Aborts operation

…  Continuation

%  Comments
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal

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