6/3/2023
CHE 310: MATLAB
Shoeb Ahmed, PhD
Professor
Chemical Engineering Department
Bangladesh University of Engineering and Technology
June 04, 2023
Introduction
© 2014-23 Shoeb Ahmed, PhD
1
6/3/2023
Operators
+ Addition
- Subtraction
* Multiplication
/ Division
^ Power
( ) Specify evaluation order
© 2014-23 Shoeb Ahmed, PhD
Operators
>> y = 2; >> z = 3*6+6*2/4
>> x = 3*y^2 Ans=
Ans= 21
12
>> x = 5^2/2
>> y = 2; x = (3*y)^2 Ans=
Ans= 12.5
36
© 2014-23 Shoeb Ahmed, PhD
2
6/3/2023
Define Variable
>> a = 7
a =
7
>> b = 14
b =
14
>> c = b/a
c =
2
© 2014-23 Shoeb Ahmed, PhD
Define Variable
>> a = 7
If you use a semi colon (;) after the
a = value then it will save the value but
will not display.
7
>> b = 14 >> a = 7;
b = >>
14
Use commas or semicolons to enter
>> c = b/a more than one statement at once.
Commas allow multiple statements
c = per line without suppressing output.
2
© 2014-23 Shoeb Ahmed, PhD
3
6/3/2023
Built-in Functions
Many standard mathematical functions, such as sin, cos, log, and log10, pi (π) are
built-in.
>> pi
ans=
3.1416
>> log(256) %log(x)computes the natural logarithm of x
ans =
5.5452
>> log10(256) %log10(x) is the base 10 logarithm
ans =
2.4082
© 2014-23 Shoeb Ahmed, PhD
Built-in Functions
Many standard mathematical functions, such as sin, cos, log, and log10, pi (π) are
built-in.
>> sin(pi/4)
ans =
0.7071
There is no “degrees” mode. All angles are measured in
radians.
You can use “%” sign in front of any text to make it
comment. These lines will not be considered during
calculation.
© 2014-23 Shoeb Ahmed, PhD
4
6/3/2023
Matrix Creation
When assigning elements to matrix, row elements are separated by spaces, and
columns are separated by semicolons.
>> A = [1 2 3; 5 7 11; 13 17 19]
A =
1 2 3
5 7 11
13 17 19
If A is a matrix, A(i,j) selects the element in the ith
row and jth column.
>> A(2,3) %2nd row, 3rd column of marix A
>> ans =
11
© 2014-23 Shoeb Ahmed, PhD
Matrix Creation
Once a variable has been created, it can be reassigned
using location.
A =
1 2 3
5 7 11
13 17 19
>> A(2,3)= 4;
>> A
A =
1 2 3
5 7 4
13 17 19
© 2014-23 Shoeb Ahmed, PhD
5
6/3/2023
Matrix Creation
Colon can be used to refer or extract ranges of matrix
elements
Syntax:
Start value : end value
Start value : increment : end value
>> p = 1:4
p =
1 2 3 4
>> s = 0:0.1:0.4
s =
0 0.1 0.2 0.3 0.4
© 2014-23 Shoeb Ahmed, PhD
Matrix Creation
For matrix elements, colon can specify a specific range
of elements.
>> A = [1 2 3; 4 5 6; 7 8 9];
>> A(:,1) % means all rows and column 1
ans =
1
4
7
>> A(2:3,1)
ans =
4
7
© 2014-23 Shoeb Ahmed, PhD
6
6/3/2023
Matrix Operation
The transpose operator converts a row vector to a
column vector (and vice versa), and it changes the rows
of a matrix to columns.
>> v = [2 4 1 7]
v =
2 4 1 7
>> w = v’
w =
2
4
1
7
© 2014-23 Shoeb Ahmed, PhD
Matrix Operation
>> A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> B = A’
B =
1 4 7
2 5 8
3 6 9
© 2014-23 Shoeb Ahmed, PhD
7
6/3/2023
Merging Matrices
Multiple matrices can be merged by writing them side by
side within []
>> A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
>>C=[A B]
4 5 6
C=
7 8 9
1 2 3 1 4 7
4 5 6 2 5 8
>> B = A’
7 8 9 3 6 9
B =
1 4 7
2 5 8
3 6 9
© 2014-23 Shoeb Ahmed, PhD
Array operators
Array operators support element-by-element operations
that are not defined by the rules of linear algebra.
Array operators have a period prepended to a standard
operator.
Symbol Operation
.* element-by-element multiplication
./ element-by-element “right” division
.\ element-by-element “left” division
.^ element-by-element exponentiation
© 2014-23 Shoeb Ahmed, PhD
8
6/3/2023
Array operators
>> u = [1 2 3];
>> v = [4 5 6];
Use .* and ./ for element-by-element multiplication and
division
>> w = u.*v
w =
4 10 18
>> x = u./v
x =
0.2500 0.4000 0.5000
© 2014-23 Shoeb Ahmed, PhD
Array operators
Each column or row can be summed up by simple ‘sum’
command.
>> A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> sum(A)
ans =
12 15 18
© 2014-23 Shoeb Ahmed, PhD
9
6/3/2023
Array operators
Each column or row can be summed up by simple ‘sum’
command.
>> A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> sum(A’)
ans =
6 15 24
© 2014-23 Shoeb Ahmed, PhD
Array operators
Specific column or row can also be summed up by ‘sum’
command.
>> A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> sum(A(:,2))
ans =
15
© 2014-23 Shoeb Ahmed, PhD
10
6/3/2023
Matrix Functions
© 2014-23 Shoeb Ahmed, PhD
Matrix Functions
Some useful built-in functions for matrix and
variables:
fliplr
flipud
linspace
ones
zeros
diag
eye
rand
© 2014-23 Shoeb Ahmed, PhD
11
6/3/2023
Plotting
Two dimensional plots are created with the plot function.
plot(x,y) % plots y vs. x
% x and y must have the same number of
elements.
>> x = linspace(0,2*pi);
>> y = sin(x);
>> plot(x,y);
1st line can also be written as
x =[0:pi/4:2*pi]
© 2014-23 Shoeb Ahmed, PhD
Plotting
Detailed syntax
plot(xdata,ydata,’colorsymbollinestyle’)
To choose a color/symbol/line style, chose one entry from
each column below.
© 2014-23 Shoeb Ahmed, PhD
12
6/3/2023
Plotting
Put black diamonds at each data point and connect the
diamonds with black dashed lines:
plot(x,y,’kd--’)
© 2014-23 Shoeb Ahmed, PhD
Plot annotations
© 2014-23 Shoeb Ahmed, PhD
13
6/3/2023
Plot annotations
x = linspace(0,2*pi);
plot(x,sin(x));
axis([0 2*pi -1.5 1.5]);
title(’sin(x)’);
If you want to see your
plot with or without a
grid, just use the command
‘grid on’ and ‘grid off’
Plot legends can also be shown on the plot. You have to
maintain the same sequence for legends as you did for the
plotting, in case of multiple plots.
© 2014-23 Shoeb Ahmed, PhD
Plot annotations
Now, multiple curves can be plotted at the same time.
t= pi:pi/50:2*pi;
y=cos(t);
y1=sin(t);
y2=sin(t+0.3);
plot(t,y,'b-*',t,y1,'g--',t,y2,'m-d');
xlabel('radian');
ylabel('different y values');
title('Different curves on same plot');
legend('cos(t)','sin(t)','sin(t+0.3)');
grid on;
© 2014-23 Shoeb Ahmed, PhD
14
6/3/2023
Plot annotations
Now, multiple curves can be plotted at the same time.
t= pi:pi/50:2*pi;
y=cos(t);
y1=sin(t);
y2=sin(t+0.3);
plot(t,y,'b-*',t,y1,'g--',t,y2,'m-d');
xlabel('radian');
ylabel('different y values');
title('Different curves on same plot');
legend('cos(t)','sin(t)','sin(t+0.3)');
grid on;
© 2014-23 Shoeb Ahmed, PhD
Plot annotations
Now, multiple curves can be plotted at the same time.
t= pi:pi/50:2*pi;
y=cos(t);
y1=sin(t);
y2=sin(t+0.3);
plot(t,y,'b-*',t,y1,'g--',t,y2,'m-d');
xlabel('radian');
ylabel('different y values');
title('Different curves on same plot');
legend('cos(t)','sin(t)','sin(t+0.3)');
grid on;
You can draw each plot with separate command. In that
case, after the first plot is drawn, you have to write
‘hold on’ so that Matlab keeps the plot active and allows
to draw again on the same plot.
© 2014-23 Shoeb Ahmed, PhD
15
6/3/2023
Subplots
subplot(n,m,p)
It divides the window into n rows and m columns for all
plots in a single figure window. p is the position of
current plot in the window.
First you have to decide how many plots you want to show
and their arrangement. Then,
1. Mention the position of your plot.
2. After that, write the typical plot command like
before.
If you want to show 6 plots in 2 rows then the command
will be:
For first one,
subplot (2,3,1)
plot (x,y…….)
xlabel(…..
ylabel(…..
………….
© 2014-23 Shoeb Ahmed, PhD
Subplots
If you want to show 6 plots in 2 rows then the command
will be:
For first one,
subplot (2,3,1)
plot (x,y…….)
xlabel(…..
ylabel(…..
………….
For third one,
subplot (2,3,3)
plot (x,y…….)
xlabel(…..
ylabel(…..
………….
© 2014-23 Shoeb Ahmed, PhD
16
6/3/2023
Subplots
>> x = linspace(0,2*pi);
>> subplot(2,2,1); % total 4 spaces for plots
>> plot(x,sin(x));
>> subplot(2,2,2);
>> plot(x,sin(2*x));
>> subplot(2,2,3);
>> plot(x,sin(3*x));
>> subplot(2,2,4);
>> plot(x,sin(4*x));
© 2014-23 Shoeb Ahmed, PhD
Other plots
If you want log-log or semi-log plot, just use ‘loglog’,
‘semilogx’ or ‘semilogy’ command instead of ‘plot’
command. Everything else will be the same.
3
10
x=0:5:50;
y=5*x;
loglog(x,y)
2
10
1
10
0 1 2
10 10 10
© 2014-23 Shoeb Ahmed, PhD
17
6/3/2023
Other plots
If you want log-log or semi-log plot, just use ‘loglog’,
‘semilogx’ or ‘semilogy’ command instead of ‘plot’
command. Everything else will be the same.
250
200
x=0:5:50; 150
y=5*x;
100
semilogx(x,y)
50
0
0 1 2 3
10 10 10 10
x=0:5:50;
2
10
y=5*x;
semilogy(x,y)
1
10
© 2014-23 Shoeb Ahmed, PhD 5 10 15 20 25 30 35 40 45 50
Other plots
You can also plot histogram and bar chart with ‘hist’ and
‘bar’ command instead of ‘plot’ command.
x=0:5:50;
y=5*x;
bar(x,y)
© 2014-23 Shoeb Ahmed, PhD
18
6/3/2023
Other plots
You can also plot histogram and bar chart with ‘hist’ and
‘bar’ command instead of ‘plot’ command.
x=[91:95];
y=[92 93 95 92 94 93 92 94 93 91 95 92 94 93 95 92 93 94
93 91];
hist(y,x)
For all commands it is (x,y),
but for hist it is (y,x).
Examples:
plot(x,y)
bar(x,y)
hist(y,x)
© 2014-23 Shoeb Ahmed, PhD
Other plots
You can plot surface plots in both 2-D or 3-D.
Plot on the domain and
[X,Y] = meshgrid(-2:0.2:2,-2:0.2:2); 1.5
Z = X.*exp(-X.^2 - Y.^2); 1
0.5
figure
0
surface(X,Y,Z) -0.5
-1
-1.5
-2
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
Color is decided based on the value of Z. Higher Z value
is indicated by high color intensity.
© 2014-23 Shoeb Ahmed, PhD
19
6/3/2023
Other plots
You can plot surface plots in both 2-D or 3-D.
Plot on the domain and
[X,Y] = meshgrid(-2:0.2:2,-2:0.2:2);
Z = X.*exp(-X.^2 - Y.^2); 0.5
figure
surface(X,Y,Z)
0
view(3)
-0.5
2
1 2
0 1
0
-1
-1
-2 -2
‘view’ determines the dimension of the plot.
© 2014-23 Shoeb Ahmed, PhD
Other plots
You can plot mesh plots in both 2-D or 3-D.
Plot on the domain and
[X,Y] = meshgrid(-2:0.2:2,-2:0.2:2);
Z = X.*exp(-X.^2 - Y.^2);
figure
0.5
mesh(X,Y,Z)
-0.5
2
1 2
0 1
0
-1 -1
-2 -2
© 2014-23 Shoeb Ahmed, PhD
20
6/3/2023
Other plots
You can plot mesh plots in both 2-D or 3-D.
Plot on the domain and
[X,Y] = meshgrid(-2:0.2:2,-2:0.2:2);
2
Z = X.*exp(-X.^2 - Y.^2); 1.5
figure 1
mesh(X,Y,Z) 0.5
view(2) 0
-0.5
-1
-1.5
-2
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
‘view’ determines the dimension of the plot.
© 2014-23 Shoeb Ahmed, PhD
21