[go: up one dir, main page]

0% found this document useful (0 votes)
18 views11 pages

Matlab-Basic-Usage

The document is a tutorial on MATLAB basics, covering its features such as matrix operations, plotting, control flow, loops, and functions. It provides examples of creating and manipulating matrices, performing basic plotting, and using control structures like if statements and loops. The tutorial also introduces functions, including anonymous functions and how to define and call them.

Uploaded by

Sreya Banerjee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views11 pages

Matlab-Basic-Usage

The document is a tutorial on MATLAB basics, covering its features such as matrix operations, plotting, control flow, loops, and functions. It provides examples of creating and manipulating matrices, performing basic plotting, and using control structures like if statements and loops. The tutorial also introduces functions, including anonymous functions and how to define and call them.

Uploaded by

Sreya Banerjee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

ADVANCE TRANSPORT PHENOMENA

Tutorial on MATLAB
BASICS

Instructor : Dr. Pratyush Dayal


Teaching Assistant : Surbhi Khewle
Indian Ins7tute of Technology Gandhinagar, Gujarat
Introduc7on
• MATrix LABoratory
• High performance language for numerical computation,
visualisation and programming
• The heart of the MATLAB is the matrix-based language
• Machine learning, signal processing, computational finance,
optimization etc.
• MATLAB desktop
• Command window
• Workspace
• Current directory
• Editor
• Help browser
• Getting started with MATLAB
Matrices & Arrays
colon supresses Row
• Crea%ng a vector operator output vector
x = xmin:dx: xmax; x = 0: 1: 5; 0 1 2 3 4 5
x = linspace(xmin, xmax,no. of points); x = linspace(0,1,6); Creates linear spaced vector
• Crea%ng a matrix next row 1 2 3
A = [1 2 3; 4 5 6; 7 8 9]; [3×3] matrix A= 4 5 6
A (3,2) extracts 8 from matrix 7 8 9
B = A(2:3,1:3) submatrix of last two rows of A
C = A(2:3,:) submatrix of last two rows of A
C(:,2) = [] deletes second column by seJng it to null vector []
D = A’ transpose of matrix A
E = A.^2 element wise opera>on
• Opera%ons on matrices
A = rand(m,n); creates random matrix of m×n size
A = rand(2,3); B = rand(3,2);
C = A*B; Matrix mul>plica>on
disp(C) displays G in the command window
size(A),size(B),size(C) displays dimension of the matrix
Matrices & Arrays (cont…)
• Ini%alizing matrix
A = zeros(2,3); creates a 3×4 matrix with ini>aliza>on to zero
A = ones(3,4); creates a 3×4 matrix with elements set to one
A = eye(3,4); creates an iden>ty matrix of dimension 3 ∗ 4
A(1,2) = 5; ini>alizing the second element of first row
A(2, :) = 6; sets all the elements of 2 row to 6
A(:,:) = 2; sets all the elements to 2
disp(A) displays final A matrix

• Concatena%ng matrices
A = ones(1,4); A= 1 1 1 1
B = zeros(1,4); B= 0 0 0 0
C = [A B]; C= 1 1 1 1 0 0 0 0
D = [A;B]; D= 1 1 1 1
0 0 0 0
Basic ploDng
• 2D plot • Subplots
x = linspace(-pi,pi,100); x = linspace(0,2*pi,100);
y = sin(x); calculates y as a func>on of x y1 = 0.5*sin(x); y2 = sin(x);
figure(1) creates a new plot window y3 = 2*sin(x); y4 = 3*sin(x);
plot(x,y) figure(3)
xlabel(‘x’); labels x axis hold on; holds the plot window for mul>ple plots
ylabel(‘y’); labels y axis subplot(2,2,1); divides the window into 2*2 sec>on
>tle(‘Graph of sin wave from -\pi to \pi’); plot(x,y1); xlabel(‘y’); ylabel(‘x’);
print('-dpng','-r600','figure1.png’); saves plot >tle(‘y = 0.5*sin(x)’);
subplot(2,2,2); second plot
plot(x,y1);
• Multiple plots xlabel(‘y’); ylabel(‘x’);
x = linspace(0,2*pi,100); >tle(‘y = sin(x)’);
y1 = sin(x); y2 = cos(x); subplot(2,2,3); third plot
figure(2) creates a new plot window plot(x,y1);
plot(x,y1,’-o’,x,y2,’-b’) xlabel(‘y’); ylabel(‘x’);
legend(‘sin(x)’,’cos(x)’) >tle(‘y = sin(x)’);
ylabel(‘y1 & y2’); hold off;
xlabel(‘x’);
Control flow & Loops
• if & else if statements • while statements : when no. of passes isn’t specified
Syntax Example Syntax Example
if expression D = b^2 – 4*a*c x=1
while expression
statement(s) if D < 0 while x < 10
statement(s)
end disp(‘Negative roots’) x = 3*x;
end
end end

• break, switch, continue, return statements


Syntax Example
• Operators
if expression D = b^2 – 4*a*c
> Greater than
statement(s) if D < 0
disp(‘Roots are imaginary’) < Less than
elseif expression
elseif D = 0 >= Greater than or equal to
statement(s)
<= Less than or equal to
end disp(‘Roots are equal and real’)
else == Equal to
disp(‘Roots are real’) ∼= Not equal to
end & AND operator
| OR operator
∼ NOT operator
Control flow & Loops (cont..)
• for loop – executes command repeatedly at • Nested loops – multiple for loops
fixed and predetermined number of times Syntax
for variable = expression
Syntax
for variable = expression
for variable = expression statement(s);
statement(s); end
end end

Example Example
x = 0:2:20; x = 2:2:10;
y =0:1:10; y = 1:1:10;
for i = 1:length(x)
z(i) = (x(i))^2 + (y(i))^2; for i = 1:length(x)
end for j = 1:length(y)
A(i,j) = x(i)*y(j);
end
end
Func7ons
• Anonymous functions : single inline • Functions Output Input
Syntax arguments arguments
executable expressions that return one
function [F1,…FN] = function_name(x1,…xN)
output. F1 = f(x1,x2);
Example Example FN = f(x1,x2);
f = @(x) (2 + x^2 + 3*x); f = @(x,y) (x.^2 + y.^2); end
a = f(1); x = 2;
b = integral(f,0,1); y = 4; Example
z = f(x,y); Function file : Creating a function
function [a,b] = trignos(x)
f = function handle a = sin(x);
@ = create handles b = cos(x);
() = includes function input end
argument
Script file/command window : Calling a function
x = linspace(-pi,pi,100);
c = trignos(x);
Func7ons
• Anonymous functions : single inline • Functions
Example
executable expressions that return one
Function file : Creating a function
output. function [a,b] = trignos(x)
Example Example a = sin(x);
f = @(x) (2 + x^2 + 3*x); f = @(x,y) (x.^2 + y.^2); b = cos(x);
a = f(1); x = 2; end
b = integral(f,0,1); y = 4;
z = f(x,y); Script file/command window : Calling a function
x = linspace(-pi,pi,100);
• Functions c = trignos(x);
Output Input
arguments arguments • Global & local variables
Syntax Local var - variables inside the function
function [F1,…FN] = function_name(x1,…xN)
F1 = f(x1,x2); Global var – single copy of variable between
FN = f(x1,x2); functions that declare it global
end
Func7ons (cont..)
Example Example
Function file : Function file :
function [A] = rms(v)
n = length(v); x = linspace(0,1,10);
ms = avg(v,n); n = length(x);
A = sqrt(ms); c = square(x);
end A = (sum(c))/n;

function A = avg(v,n) function c = square(x)


A = sum(v)/n ; c = x.^2 ;
end end

Script file/command window :


v = linspace(0,1,10);
f = rms(x);
Thank You

You might also like