Professional Documents
Culture Documents
Introduction
to MATLAB
Addition
MATLAB as
Calculator
Rules
Subtraction
+
-
Multiplication *
Divide
Addition
MATLAB as
Calculator
Subtraction
+
-
Multiplication *
Divide
Example
Example
x=+
5 *3
2/2-10
2
x=11
ans=-9
Example
y=(5+3)*2
y=16
4
MATLAB as
Calculator
Basic scalar arithmetic operations
Matlab
Command
Exponential
Multiplication
Right division (Divide)
Left division
Addition
Subtraction
^
*
/
a
a
a
b
b
b
\
+
a
a
b
b
b
5
MATLAB as
Calculator
Basic scalar arithmetic operations
Matlab
Command
Exponential
Multiplication
Right division (Divide)
Left division
Addition
Subtraction
*
/
a^b
a* b
a/ b
\
+
a\ b
a+b
a- b
Variable
Variables are a fundamental concept in MATLAB.
To assign variable in MATLAB, = sign is used
e.g.,
x=1
y=sin(x)
It is case sensitive (e.g., NAME, Name, name)
Example
The volume of a
cylinder of height h
and a radius r is given
by
. A
particular cylindrical
tank is 150 m tall and
has a radius of 80m.
Using
MATLAB
to
calculate the volume
of the tank.
>> r=80;
>> h=150;
>> V=pi*r^2*h
V=
3.0159e+06
Example
>> V=pi*r^2*h
V=
3.0159e+06
>> format long
If we want to display
all the decimal places,
type
format long
Also try format short
Example
If we want to display
all the decimal places,
type
format long
also try format short
Also try format short
3.015928947446202e+06
max(A) / min(A)
sum(A)
Summation
std(A)
standard deviation
inv(A)
Inverse of a matrix
There are many built-in function
See MATLAB help file
Mathematics expression
Function
MATLAB code
a_value=23
a_value =
23
matrix = [1, 2, 3 ; 4, 5, 6; 7, 8, 9]
matrix =
1 2 3
4 5 6
7 8 9
Extracting a Sub-matrix
matrix =
row 1
row 2
row 3
1 2 3
4 5 6
7 8 9
col 1
col 2
col 3
Extracting a Sub-matrix
matrix =
1 2 3
4 5 6
7 8 9
>> matrix(: , 2)
row
column
If we want to extract a
sub-matrix from the
matrix shown on the
left
Anything before comma,
is row;
Anything after comma,
is column
[1, 2, 3, 4, 5]
ans =
1 2 3 4 5
a=[1: 1 :5]
a=
1 2 3 4 5
a=[1: 2 :5]
a=
1 3 5
a=[1: -2 : -5]
a=
1 -1 -3 -5
a=3;
>>
b=[1, 2, 3; 4, 5, 6];
>>c= a + b
1 2 3
3 +
4 5 6
c=
4 5 6
7 8 9
a=3;
>>
b=[1, 2, 3; 4, 5, 6];
>>c= a - b
1 2 3
3 4 5 6
c=
-2 1 0
-1 -2 -3
Try this
example
b=[1, 2, 3; 4, 5, 6]
b=
1 2 3
4 5 6
>>size(b)
ans =
row
column
Element Operator - .
b=
1 2 3
Divide each
element of b
by 2
>> b ./ 2
4 5 6
Multiply each
element of b by
2
>> b.* 2
Square each
element of b by
2
>> b.^ 2
ans =
ans =
ans =
0.50001.0000 1.5000
2.00002.5000 3.0000
10
12
16
25
36
Graph - 2D plot
Linear plot - command plot(x, y)
Example
x = [-3 -2 -1 0 1 2 3];
y = [8 3 0 -1 0 3 8];
plot(x, y)
x=[-3:3];
>>y=[8 3 0 -1 0 3 8];
Graph of x vs y
>>plot(x, y)
>>ylabel(y)
>>xlabel(x)
>>title(Graph of x vs y)
>>hold on
>>y1=(4/3)*x+4;
>>plot(x, y1, r)
plot(x, y)
Try
plot(x, y, ro)
Thank You!