MATLAB Tutorial
Presented By:
Sanjay Dubey
Teaching
Assistant
1
Contents
1. Session One
What is MATLAB?
MATLAB Desktop
Matrix
Numerical Arrays
String Arrays
Elementary Math
Logical Operators
Math Functions
Polynomials
Graphics Fundamentals
2D plotting
2
What is MATLAB?
• high-performance software
• Computation
• Visualization
• Easy-to-use environment.
• high-level language (independent to computer
type)
• Data types
• Functions
• Input/output
• Graphics
4
MATLAB Desktop Tools
• Command Window
• Command History
• Help Browser
• Workspace Browser
• Editor/Debugger
6
General Functions
• whos: List current variables
• clear: Clear variables and functions from memory
• Close: Closes last figures
• clc: Clear command window
• dir: List files in directory
• format: Set output format
8
Basic Operation symbol
Calculations at the Command Line
MATLAB as a calculator Assigning Variables
» -5/(4.8+5.32)^2
» a = 2; Semicolon
ans =
» b = 5; suppresses
-0.0488
screen output
» (3+4i)*(3-4i) » a^b
ans = ans = Results
25 32 assigned to
» cos(pi/2) “ans” if name
» x = 5/2*pi;
ans = not specified
6.1230e-017 » y = sin(x)
» exp(acos(0.3)) y =
ans = 1
3.5470
» z = asin(y) () parentheses for
z = function inputs
1.5708
7
Task 1
>> x=0.7854-(0.7854)^3/(1*2*3)+0.785^5/(1*2*3*4*5)
Ans:
x=
0.7071
>> 27^1/3+32^0.2 27^1 and 32^0.2 are executed first,
ans =11 /3 is executed next, and + is
executed last.
>> 27^(1/3)+32^0.2
ans = 5 1/3 is executed first, 27^(1/3) and
32^0.2 are executed next, and + is
executed last.
Display Format
ELEMENTARY MATH BUILT-IN FUNCTIONS
Task 2
Mat lab use the matrix rule!
Addition:
>> C = A + B
Subtraction:
>> D = A – B
Multiplication:
>> E = A * B (Matrix multiplication)
>> E = A .* B (Element wise multiplication, A and B same size)
Division:
Left Division and Right Division
>> F = A . / B (Element wise division)
>> F = A / B = A*inv(B) (A * inverse of B)
>> F = A . \ B (Element wise division)
>> F = A \ B=inv(A)*B (inverse of A * B)
10
Creating Array
Other way of Creating Array
Creating Array : Character
Character Arrays (Strings)
• Created using single quote delimiter (')
» str = 'Hi there,'
str =
Hi there,
» str2 = 'Isn't MATLAB great?'
• Each
str2 =
character is a separate matrix element
(16 bits of memory per character)
Isn't MATLAB great?
• Indexing same as for numeric arrays
str = H i t h e r e , 1x9 vector
15
Array Operation
Selecting Rows and Columns
13
Array Operation
Generating basic matrices
Matrix with ZEROS:
>> A = zeros(m, n)
Matrix with ONES:
>> B = ones(m, n)
IDENTITY Matrix:
>> I = eye(m, n)
m Rows
n Columns
zeros, ones, eye Matlab functions
14
Task 3
>> a=[5 35 43; 4 76 81; 21 32 40]
>> A=[1:2:11; 0:5:25; linspace(10,60,6); 67 2 43 68 4 13]
Task 4
>> A=[2:3:17; 3:3:18; 4:3:19; 5:3:20; 6:3:21]
>> B=[5:5:30; 30:5:55; 55:5:80]
Can you Multiply
Mat. A and Mat B ?
>> C=[5:5:30; 30:5:55; 55:5:80]
Can you Multiply
Mat. B and Mat C ?
Task 5
A=[2 1 4; 4 1 8; 2 -1 3]
Use the inv function to find
B=inv(A) the inverse of A and assign it
to B.
What will be A*B?
Task 6
>> A=[1 4 2; 5 7 4; 8 6 9];
>> B=[1 4 2; 5 7 4; 8 6 9];
What happen if
you multiply
C= A*B
C=A.*B???
Array Multiplication vs Element Mul.
Elementary Math
•Logical Operators
•Math Functions
•Polynomial and Interpolation
16
Logical Operations
= = equal to
> greater than
< less than
>= Greater or equal
<= less or equal
~ not
& and
| or
isfinite(), etc. . . .
all(), any()
find
17
Plotting
Task 7
>> x=[1 2 3 5 7 7.5 8 10];
>> y=[2 6.5 7 7 5.5 4 6 8];
>> plot(x,y)
Play with this
2-D Plotting
Title
Ylabel
Grid
Legend
Xlabel
24
Programming and
Application
Development
25
Script and Function Files
Script and Function Files
• Script Files
• Work as though you typed commands into
MATLAB prompt
• Variable are stored in MATLAB workspace
• Function Files
• Let you make your own MATLAB Functions
• All variables within a function are local
• All information must be passed to functions as
parameters
• Subfunctions are supported
26
Task 8
x=[10:0.1:22];
y=95000./x.^2;
xd=[10:2:22];
yd=[950 640 460 340 250 180 140];
plot(x,y,'-','LineWidth',1.0)
xlabel('DISTANCE (cm)')
ylabel('INTENSITY (lux)')
title('\fontname{Arial}Light Intensity as a Function of
Distance','FontSize',14)
Plot
23
Getting more help
• Contact http://www.mathworks.com/support
• You can find more help and FAQ about
mathworks products on this page.
• Contact comp.soft-sys.matlab Newsgroup
• Using Google Groups Page to Access this page
http://groups.google.com/
33