0% found this document useful (0 votes)
39 views

Introduction To Matlab-1

Matlab is an interpreted language and scientific programming environment used for matrix manipulation and visualization. It has a desktop interface with a command window, workspace, and help features. Variables can be assigned without declaring type and matrices are defined using square brackets. The colon operator ":" is used to generate sequences and access matrix elements. Scripts and functions allow automating tasks and avoiding duplicating code. Debugging tools like breakpoints help test and troubleshoot programs.

Uploaded by

NahidaNigar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Introduction To Matlab-1

Matlab is an interpreted language and scientific programming environment used for matrix manipulation and visualization. It has a desktop interface with a command window, workspace, and help features. Variables can be assigned without declaring type and matrices are defined using square brackets. The colon operator ":" is used to generate sequences and access matrix elements. Scripts and functions allow automating tasks and avoiding duplicating code. Debugging tools like breakpoints help test and troubleshoot programs.

Uploaded by

NahidaNigar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Introduction to Matlab

Matlab
• Stands for MATrix LABoratory
• Interpreted language
• Scientific programming environment
• Very good tool for the manipulation of matrices
• Great visualisation capabilities
• Loads of built-in functions
• Easy to learn and simple to use

2
Matlab Desktop

Workspace /
Current Directory
Command
Window

Command
History

Explore the Matlab Desktop


3
Variables
• Don’t have to declare type
• Don’t even have to initialise
• Just assign in command window
>>
>> a=12; % variable a is assigned 12

Matlab comment
prompt suppress operator
assign
operator command
output Try the same line without the
semicolon and comments
4
Variables (continued …)
• View variable contents by simply typing the
variable name at the command prompt
>> a
a=
12
>>
>> a*2
a=
24
>>

5
Workspace
• The workspace is Matlab’s memory
• Can manipulate variables stored in the workspace
>> b=10;
>> c=a+b
c=
22
>>

6
Workspace (continued …)
• Display contents of workspace
>> whos
Name Size Bytes Class
a 1x1 8 double array
b 1x1 8 double array
c 1x1 8 double array
Grand total is 3 elements using 24 bytes
>>

• Delete variable(s) from workspace


>> clear a b; % delete a and b from workspace
>> whos
>> clear all; % delete all variables from workspace
>> whos

7
Matlab help commands
• help
>> help whos % displays documentation for the function whos
>> lookfor convert % displays functions with convert in the first help line

• Start Matlab help documentation


>> helpdesk

8
Matrices
• Don’t need to initialise type, or dimensions
>>A = [3 2 1; 5 1 0; 2 1 7]
A=
square brackets to define matrices
3 2 1
5 1 0 semicolon for next row in matrix
2 1 7
>>
9
Manipulating Matrices A=
3 2 1

• Access elements of a matrix 5


2
1
1
0
7

>>A(1,2)
ans= indices of matrix element(s)
2
• Remember Matrix(row,column)
• Naming convention Matrix variables start
with a capital letter while vectors or scalar
variables start with a simple letter
10
The : operator
• VERY important operator in Matlab
• Means ‘to’
>> 1:10
ans =
1 2 3 4 5 6 7 8 9 10
>> 1:2:10
Try the following
ans = >> x=0:pi/12:2*pi;
>> y=sin(x)
1 3 5 7 9
11
The : operator and matrices
A=
>>A(3,2:3) 3 2 1
ans = 5 1 0
2 1 7
1 7
>>A(:,2)
ans =
2
1
What’ll happen if you type A(:,:) ?
1

12
A=
3 2 1
Manipulating Matrices 5
2
1
1
0
7

>> A ' % transpose B=

>> B*A % matrix multiplication 1


4
3
9
1
5
>> B.*A% element by element multiplication 2 7 2

>> B/A % matrix division


Enter matrix B
>> B./A % element by element division into the Matlab
workspace
>> [B A] % Join matrices (horizontally)
>> [B; A] % Join matrices (vertically)

Create matrices A and B and try out the the matrix operators in this slide
13
Scripts
• Matlab editor
• Use scripts to execute a series of Matlab
commands

Matlab
Desktop
Press to create
new m-file in the
matlab editor

14
• Scripts will manipulate and Scripts (continued)
store variables and matrices
in the Matlab Workspace
(memory).
• They can be called from the
Matlab command line by Will be slightly
typing the (case sensitive!) different in Linux
filename of the script file.
>> myscript
• Scripts can be opened in the
editor by the following
>> open myscript
Highlight a few lines of your
script by left- clicking and
dragging the mouse over the
lines. Right-click the
highlighted lines and select
Evaluate Selection.

15
Functions
• Programming in Matlab.
• Users can write functions which can be called from the command line.
• Functions can accept input variable(s)/matrice(s) and will output
variable(s)/matrice(s).
• Functions will not manipulate variable(s)/matrice(s) in the Matlab
Workspace.
• In Matlab functions closely resemble scripts and can be written in the
Matlab editor. Matlab functions have the function keyword.
• Remember that the filename of a function will be its calling function name.
• Don’t overload any built-in functions by using the same filename for your
functions or scripts!
• Functions can be opened for editing using the open command. Many built-
in Matlab functions can also be viewed using this command.

16
Functions (continued)
>> I=iterate(5) function name input
output
I=
1 4 9 16 25

function keyword

help lines for function

for statement block


Access the comments of
your Matlab functions
>> help iterate Make sure you save changes to the
m-file before you call the function!
17
Functions (continued)
Functions can have many
>> [i j]=sort2(2,4) outputs contained in a matrix
i=
4
j=
2
if statement
>> block

Remember to use the


Matlab help command for
syntax
>> help if

18
More flow control
While statement block Switch statement block

Without ; to
print output

i=
4
i=
16
i= Method is linear
256 >>

19
Debugging
Debug menus
• Set breakpoints to stop the execution of code
>> [i j]=sort2(2,4)
K>>
K>> whos
Name Size Bytes Class
a 1x1 8 double array
b 1x1 8 double array
Grand total is 2 elements using 16 bytes
K>> a
a= local function
2 workspace
K>> return
i=

4 exit debug Click mouse on the left


j= mode of the line of code to
2
create a breakpoint

20
Visualisation - plotting data
>> figure % create new figure
>> t=0:pi/12:8*pi;
Plot style
>> y=cos(t);
>> plot(t,y,‘b.-')
Investigate the function
>> y=A*cos(w*t+phi);
for different values of phi (eg: 0, pi/4, pi/3,
pi/2), w (eg: 1, 2, 3, 4) and A (eg: 1, 0.5, 2). Use
the hold on Matlab command to display your
plots in the same figure. Remember to type A = amplitude
hold off to go back to normal plotting mode. phi = phase
Try using different plot styles (help plot) w = angular frequency = 2*pi*frequency
21
Useful operators and built-in functions
Operating
< ¦ save ! system
> rand load guide command
Continue in next line
~= zeros … get
== min '' set Graphical
string user interface
>= max { } cell

<= repmat try error handling


& axis catch

Remember to use the Matlab help command if you get stuck

22

You might also like