An Introduction to
MATLAB
Allen Anilkumar
What is MATLAB?
• MATrix LABoratory
• Software package developed by MathWorks, Inc.
• Makes lives of Scientists and Engineers simpler
• MATLAB help: Best resource to self learning
• SCILAB, Maple, Mathematica
Interesting plots!
Default Screen
• Title bar
• Menu bar
• Current folder
• Command Window
• Workspace
• Command history
• .m
• .mat
• .fig
Let’s start!
• Try simple calculations
3249+34
213*878
a=(7-2)*9
Q=(sqrt(3^2+4^2))/2
• Trigonometry
t=30
sin(t)=??
Go to MATLAB help and find out how to find sine-inverse
cos(), tan(), sind(), sinh(), atan()
Arrays
• Try the following
X=[1,2,3]
Y=[4;5;6]
Y*X
X*Y
P= 1: 1: 10
Q= 40: -5: 0
X.*2
Y./10
sum(X), max(X), min(X),sign(X)
Algebra and Calculus
• Symbolic variables:
syms x
y=sin(x)
dy=diff(y)
int_dy=int(dy)
• Integration with limits
int(x+(3*x^2)-(x^3),0,1)
• Substitute values for variable
subs(y,x,pi/6)
• Note: subs works only for symbolic variables!!!!
Plotting
x=linspace(0,2,50) • Try
y1=x.^2 subplot(311)
plot(x,y1) plot(x,y1)
hold on subplot(312)
y2=exp(x) plot(x,y2)
plot(x,y2,'r') subplot(313)
y3=(x.^0.5)-(2*(x.^3))+(x.^2) plot(x,y3)
plot(x,y3,'og')
hold on
y4=sin(x)
plot(x,y4, 'or')
plot(x,y4, 'k')
Matrices
• clc • Also try
• clear all C=A*B
• Try D=transpose(A)
A=[1,2,3;4,5,6;7,8,9] D=A’
B=ones(3,3)
E=inv(A)
Z=zeros(5,2)
d=det(A)
I=eye(3,3)
R=rand(2,4)
• Remember that inv, det etc. works
only for square matrices!
Programing
• MATLAB allows you to write your own program
• if condition
Find the absolute value of a number
num=-5; % any number
if num<0
abs=num*-1;
end
• for loop
Example: add all numbers from 0 to 100
sum=0;
for i=1:1:100
sum=sum+i;
end
Exercise 1
Exercise 2