Introduction
Introduction
Introduction
1
6/3/2023
Operators
+ Addition
- Subtraction
* Multiplication
/ Division
^ Power
( ) Specify evaluation order
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
2
6/3/2023
Define Variable
>> a = 7
a =
7
>> b = 14
b =
14
>> c = b/a
c =
2
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
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
Built-in Functions
Many standard mathematical functions, such as sin, cos, log, and log10, pi (π) are
built-in.
>> sin(pi/4)
ans =
0.7071
4
6/3/2023
Matrix Creation
When assigning elements to matrix, row elements are separated by spaces, and
columns are separated by semicolons.
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
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
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
8
6/3/2023
Array operators
>> u = [1 2 3];
>> v = [4 5 6];
>> w = u.*v
w =
4 10 18
>> x = u./v
x =
0.2500 0.4000 0.5000
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
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
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
10
6/3/2023
Matrix Functions
Matrix Functions
Some useful built-in functions for matrix and
variables:
fliplr
flipud
linspace
ones
zeros
diag
eye
rand
11
6/3/2023
Plotting
>> x = linspace(0,2*pi);
>> y = sin(x);
>> plot(x,y);
Plotting
Detailed syntax
plot(xdata,ydata,’colorsymbollinestyle’)
To choose a color/symbol/line style, chose one entry from
each column below.
12
6/3/2023
Plotting
Put black diamonds at each data point and connect the
diamonds with black dashed lines:
plot(x,y,’kd--’)
Plot annotations
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)’);
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;
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;
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;
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.
Subplots
If you want to show 6 plots in 2 rows then the command
will be:
subplot (2,3,1)
plot (x,y…….)
xlabel(…..
ylabel(…..
………….
subplot (2,3,3)
plot (x,y…….)
xlabel(…..
ylabel(…..
………….
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));
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
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)
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)
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.
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
19
6/3/2023
Other plots
You can plot surface plots in both 2-D or 3-D.
[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
Other plots
You can plot mesh plots in both 2-D or 3-D.
[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
20
6/3/2023
Other plots
You can plot mesh plots in both 2-D or 3-D.
[X,Y] = meshgrid(-2:0.2:2,-2:0.2:2);
2
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
21