0% found this document useful (0 votes)
51 views25 pages

Numerical Technique Ex-P1-2-3

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 25

Numerical technique Sessional (EEE-2306)

Exp.- 01, 02 and 03


Introduction to Matlab
Dr.Yasir Arafat
Assistant Professor
Dept. of EEE, IIUC

Power point slides prepared by: Mohammed Abdul Kader, Assistant Professor, Dept. of EEE, IIUC
What is MATLAB?
• Stands for MATrix LABoratory developed by The Mathworks, Inc
(www.mathworks.com)

 It is an interactive, integrated, environment

– for numerical/symbolic, scientific computations and other apps.

– shorter program development and debugging time than traditional


programming languages such as FORTRAN and C.

– slow (compared with FORTRAN or C) because it is interpreted.

– automatic memory management; no need to declare arrays.


N.B. An interpreted language is a type of
– intuitive, easy to use. programming language for which most of its
implementations execute instructions directly
– compact notations. and freely, without previously compiling a
program into machine-language instructions.
MATLAB Help Commands

>> version % this will tell you the running MATLAB version
ans =
9.0.0.341360 (R2016a)
>> help % lists available packages/toolboxes on system.
>> help elfun % lists functions in elementary functions package
>> help sin % instructions on the sine function
>> lookfor sine % if you don’t know the function name …
>> doc % start matlab help documentation
>> doc sin % for full details of function
>> quit % to quit MATLAB
MATLAB Preview

Current Directory

Command Window

Workspace
Variables
 Don’t have to declare type, is case sensitive
 variable begins with a letter, e.g., A2z or a2z
 can be a mix of letters, digits, and underscores (e.g.,
vector_A)
 Variable name can be up to 63 characters
 Don’t even have to initialise
 Just assign in command window
>>
>> a=12; % variable a is assigned 12
comment operator

suppress command output


Matlab prompt
Try the same line without the
assign operator
semicolon and comments
Size of Variables
 All numerical variables in MATLAB are matrices, a
mathematical data type corresponding to a two-dimensional
array of numbers.
>> m=3;
>> size(m)
ans =
1 1
>> a=[1,2,3];
>> size(a)
ans =
1 3
Workspace
 The workspace is Matlab’s memory
 Can manipulate variables stored in the workspace
>> a=12;
>> b=10;
>> c=a+b
c=
22
>>
Workspace (Cont.)
• Display contents of workspace
>> a=12;
>> b=10;
>> c=a+b;
>> whos
Name Size Bytes Class Attributes
a 1x1 8 double
b 1x1 8 double
c 1x1 8 double
>>
• Delete variable(s) from workspace
>> clear a b; % delete a and b from workspace
>> whos
>> clear all; % delete all variables from workspace
>> whos
>> clc % clear command window (workspace remain unchange)
Matrices
 Don’t need to initialise type, or dimensions
>>A = [3 2 1; 5 1 0; 2 1 7]
A=
3 2 1 square brackets to define matrices

5 1 0 semicolon for next row in matrix


2 1 7
>>
Manipulating Matrices

 Access elements of a matrix


>> A = [3 2 1; 5 1 0; 2 1 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
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
ans =
Try the following
1 3 5 7 9 >> x=0:pi/12:2*pi;
>> y=sin(x)
The : Operator and Matrices

>>A(3,2:3)
A=
ans = 3 2 1
1 7 5 1 0
>>A(:,2) 2 1 7
ans =
2
1
1
What’ll happen if you type A(:,:) ?
Manipulating Matrices
>> A ' % transpose
>> B*A % matrix multiplication
>> B.*A % element by element multiplication
>> B/A % matrix division
>> B./A % element by element division
>> [B A] % Join matrices (horizontally)
>> [B; A] % Join matrices (vertically)
A= B=
3 2 1 1 3 1
5 1 0 4 9 5
2 1 7 2 7 2
Task: Create matrices A and B and try out the matrix operators in this slide
MATLAB Graphics

 Line plot
 Bar graph
 Surface plot
 Contour plot
 MATLAB has 2D, 3D visualization tools as well as
other graphics packages.
MATLAB Graphics: Line Plot

>> t = 0:pi/100:2*pi;
>> y = sin(t);
>> plot(t,y)
MATLAB Graphics: Line Plot (Cont.)
>> xlabel(‘t’);
>> ylabel(‘sin(t)’);
>> title(‘The plot of t vs sin(t)’);
MATLAB Graphics: Line Plot (Cont.)
>> y2 = sin(t-0.25);
>> y3 = sin(t+0.25);
>> plot(t,y,t,y2,t,y3) % make 2D line plot of 3 curves
>> legend('sin(t)','sin(t-0.25)','sin(t+0.25',1)
Scripts

 Matlab editor
 Use scripts to execute a series of Matlab commands

Press to create new


m-file in the matlab
editor
Scripts (Cont.)
 Scripts will manipulate and store
variables and matrices in the Matlab
Workspace (memory).
 They can be called from the Matlab
command line by typing the (case
sensitive!) 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.
For loop
for j=1:5 % use for-loops to execute iterations /
repetitions
for i=1:3
a(i, j) = i + j ;
end
end

Utilities to initialize or define arrays: ones, rand, eye, . . .


Trigonometric and hyperbolic functions : sin, cos, sqrt, exp, . . .
These utilities can be used on scalar or vector inputs

>> a = sqrt(5); v = [1 2 3]; A = sqrt(v);


If Conditions
Scalar operation . . .
a = zeros(3); b = zeros(3);
for j=1:3
for i=1:3
a(i,j) = rand; % use rand to generate a random number
if a(i,j) > 0.5
b(i,j) = 1;
end
end
end
Equivalent vector operations . . .
A = rand(3); % A is a 3x3 random number double array
B = zeros(3); % Initialize B as a 3x3 array of zeroes
B(A > 0.5) = 1; % set to 1 all elements of B for which A > 0.5
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.
Functions (Cont.)

function name input


>> I=iterate(5)
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!
Functions (Cont.)
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
Debugging
 Set breakpoints to stop the execution of code Debug
>> [i j]=sort2(2,4)
K>>
menus
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=
2 local
K>> return function
i=
workspace
4 exit Click mouse on the
j=
2 debug left of the line of
mode code to create a
breakpoint

You might also like