Tutorial 1
Tutorial 1
Tutorial 1
MATLAB
An Introductory Workshop
AKHILESH SHENDE
Department of Chemistry,
Indian Institute of Technology Kanpur
14th 19th October 2013
Organized by
Promotion of Work Experience & Research
(PoWER)
Akhilesh Shende
MATLAB WORKSHOP
1. Introduction
Desktop Basics
In-build functions
2D & 3D Plots
Structures
3. Mathematical Applications
4. Graphics
Eigen Values
Basic Plotting
2D Plots
Control Flow
Scripts
Functions
Akhilesh Shende
Introduction
MATLAB WORKSHOP
MATLAB
An Introductory Workshop
Tutorial 1
Introduction
Tutorial 1
Akhilesh Shende
MATLAB WORKSHOP
What is MATLAB
Numerical Computing environment and Programming Language
Developed by MathWorks organization www.mathworks.in
MATLAB - MATrix LABoratory
It allows us to perform
1. Mathematical operations : Matrix manipulations
2. Plotting of functions and data
3. Implementation of algorithms
4. Creation of user interfaces and interfacing with programs written in other languages
Fortran , C, C++ and Java
Used by
Academic and Research Institutions
Industrial Enterprises.
Akhilesh Shende
MATLAB WORKSHOP
Akhilesh Shende
MATLAB WORKSHOP
Row 1
Row 2
arr(3,2)
Row 3
Row 4
Introduction
MATLAB WORKSHOP
Linux Users
ssh X [email protected] - [email protected]
172.31.4.141 - 172.31.4.210
Tutorial 1
Akhilesh Shende
Introduction
MATLAB WORKSHOP
Desktop Basics
Tutorial 1
Akhilesh Shende
Introduction
MATLAB WORKSHOP
% Closing MATLAB
exit
% Saving the workspace as .mat file
Akhilesh Shende
Introduction
MATLAB WORKSHOP
% Entering a Matrix
1. Starts for [
2. Space or , between 2 numbers of a row
3. ; between 2 numbers of a column
4. Ends with ]
Example : A = [1 2 3; 4 5 6; 7 8 10];
Tutorial 1
% Dimension of a Matrix
1. [m, n] = size(A)
2. or size(A)
Akhilesh Shende
Introduction
MATLAB WORKSHOP
Akhilesh Shende
Introduction
MATLAB WORKSHOP
% Concatenation of 2 Arrays
Joining of 2 arrays to make a single array
Matrices A of size (m,n) , B of size (m,n)
Two types of Concatenations
Vertical Concatenation (Increase the number of rows)
C = [A; B]; if (m == m or or mm and n==n)
Horizontal Concatenation (Increase the number of columns)
C = [A, B] ; ) if (m == m and n==n or nn )
% Swap the columns
1. Matrix A = magic(4) of size(4,4)
2. B = A(:, [1 3 2 4])
3. Swap the columns 2 & 3
Tutorial 1
Introduction
MATLAB WORKSHOP
Tutorial 1
Akhilesh Shende
Introduction
MATLAB WORKSHOP
Matrix Generators
Matrix
zeros(m,n);
ones(m,n);
rand(m,n);
randn(m,n);
hilb
Hilbert matrix
invhilb
magic
Magic square
pascal
Pascal matrix
toeplitz
Toeplitz matrix
Tutorial 1
Akhilesh Shende
Introduction
MATLAB WORKSHOP
2D and 3D Plots
% 2D Line Plots
2 dimensional data (x, y)
Function y = f(x)
plot (x, y)
Examples :
A. x = [1 2 3 4 5 6];
y = [3 -1 2 4 5 1];
B. x = 0:pi/100:2*pi
y = sin(x);
plot(x, y)A
plot(x, y)B
% Labeling of Plots
xlabel('x');
ylabel('sin(x)');
title('Plot of the Sine Function');
% To add plots in existing Figure
hold on
% 3D Plots
3 dimensional data (x, y, z)
Function z = f(x, y)
surf(x, y, z)
Examples :
[X,Y] = meshgrid(-2:.2:2);
Z = X .* exp(-X.^2 - Y.^2);
surf(X,Y,Z)
mesh(X,Y,Z)
% Subplots
x=0:.1:2*pi;
subplot(2,2,1); plot(x, sin(x));
subplot(2,2,2); plot(x, cos(x));
subplot(2,2,3); plot(x,exp(-x));
subplot(2,2,4); plot(x,x.^2);
% Print Graphics
print -djpeg subplot.jpg
Tutorial 1
Akhilesh Shende
Introduction
MATLAB WORKSHOP
% m = mean(r);
% hold on
% plot([0,n],[m,m])
% hold off
% title('Mean of Random Uniform Data')
% Functions
Function y = f(x)
y = sin(x) or cos(x) trignometric
y = x^2 or sqrt(x)
Example function :
y = 1./((x - 0.3).^2 + .01) + 1./((x-0.9).^2 +
.04) - 6;
function y = my_fun (x)
y = = 1./((x - 0.3).^2 + .01) + 1./((x-0.9).^2 +
.04) - 6;
end
save the file by name my_fun.m
command line
>> a = my_fun(x)
Tutorial 1
HELP
Akhilesh Shende