MATLAB: Introduction
Front Matter
MATLAB: Introduction
Part 2
Bruno Abreu Calfa
Last Update: August 9, 2011
MATLAB: Introduction
Table of Contents
Outline
MATLAB Classes
Elements of Programming
Plotting
2-D Plotting
3-D Plotting
MATLAB: Introduction
MATLAB Classes
Outline
MATLAB Classes
Elements of Programming
Plotting
2-D Plotting
3-D Plotting
MATLAB: Introduction
MATLAB Classes
ND Arrays
I MATLAB allows multidimensional arrays (n dimensions)
I >> nd1 = zeros(2,3,4) % 2-by-3-by-4 full of 0s
MATLAB: Introduction
MATLAB Classes
ND Arrays
I MATLAB allows multidimensional arrays (n dimensions)
I >> nd1 = zeros(2,3,4) % 2-by-3-by-4 full of 0s
I >> nd2 = ones(10,5,8,7) % 10-by-5-by-8-by-7
full of 1s
MATLAB: Introduction
MATLAB Classes
ND Arrays
I MATLAB allows multidimensional arrays (n dimensions)
I >> nd1 = zeros(2,3,4) % 2-by-3-by-4 full of 0s
I >> nd2 = ones(10,5,8,7) % 10-by-5-by-8-by-7
full of 1s
I >> nd1(:,1,2) = 1:2
MATLAB: Introduction
MATLAB Classes
ND Arrays
I MATLAB allows multidimensional arrays (n dimensions)
I >> nd1 = zeros(2,3,4) % 2-by-3-by-4 full of 0s
I >> nd2 = ones(10,5,8,7) % 10-by-5-by-8-by-7
full of 1s
I >> nd1(:,1,2) = 1:2 % Replaces column 1 of page 2
by [1,2]
MATLAB: Introduction
MATLAB Classes
ND Arrays
I MATLAB allows multidimensional arrays (n dimensions)
I >> nd1 = zeros(2,3,4) % 2-by-3-by-4 full of 0s
I >> nd2 = ones(10,5,8,7) % 10-by-5-by-8-by-7
full of 1s
I >> nd1(:,1,2) = 1:2 % Replaces column 1 of page 2
by [1,2]
I >> nd2(:,:,5,7) = rand(10,5)
MATLAB: Introduction
MATLAB Classes
ND Arrays
I MATLAB allows multidimensional arrays (n dimensions)
I >> nd1 = zeros(2,3,4) % 2-by-3-by-4 full of 0s
I >> nd2 = ones(10,5,8,7) % 10-by-5-by-8-by-7
full of 1s
I >> nd1(:,1,2) = 1:2 % Replaces column 1 of page 2
by [1,2]
I >> nd2(:,:,5,7) = rand(10,5) % Replaces rows
and columns of page 5 and chapter 7 by random 10-by-5
matrix
MATLAB: Introduction
MATLAB Classes
Cell and Structure Arrays
I Cell Arrays (cell): generic containers (store any type of data)
I >> cell1 = {’aaa’, 1, rand(2,3)} % Use curly
braces to retrieve/assign values
MATLAB: Introduction
MATLAB Classes
Cell and Structure Arrays
I Cell Arrays (cell): generic containers (store any type of data)
I >> cell1 = {’aaa’, 1, rand(2,3)} % Use curly
braces to retrieve/assign values
I >> a = cell1(1) % ‘a’ is the first container (also a
cell)
MATLAB: Introduction
MATLAB Classes
Cell and Structure Arrays
I Cell Arrays (cell): generic containers (store any type of data)
I >> cell1 = {’aaa’, 1, rand(2,3)} % Use curly
braces to retrieve/assign values
I >> a = cell1(1) % ‘a’ is the first container (also a
cell)
I >> b = cell1{1} % ‘b’ is the first content (a char
array)
MATLAB: Introduction
MATLAB Classes
Cell and Structure Arrays
I Cell Arrays (cell): generic containers (store any type of data)
I >> cell1 = {’aaa’, 1, rand(2,3)} % Use curly
braces to retrieve/assign values
I >> a = cell1(1) % ‘a’ is the first container (also a
cell)
I >> b = cell1{1} % ‘b’ is the first content (a char
array)
I >> cell1{:} % {:} generates a comma-separated list
MATLAB: Introduction
MATLAB Classes
Cell and Structure Arrays
I Cell Arrays (cell): generic containers (store any type of data)
I >> cell1 = {’aaa’, 1, rand(2,3)} % Use curly
braces to retrieve/assign values
I >> a = cell1(1) % ‘a’ is the first container (also a
cell)
I >> b = cell1{1} % ‘b’ is the first content (a char
array)
I >> cell1{:} % {:} generates a comma-separated list
I >> [a,b,c] = cell1{:} % Assigns each content to a
variable
MATLAB: Introduction
MATLAB Classes
Cell and Structure Arrays
I Cell Arrays (cell): generic containers (store any type of data)
I >> cell1 = {’aaa’, 1, rand(2,3)} % Use curly
braces to retrieve/assign values
I >> a = cell1(1) % ‘a’ is the first container (also a
cell)
I >> b = cell1{1} % ‘b’ is the first content (a char
array)
I >> cell1{:} % {:} generates a comma-separated list
I >> [a,b,c] = cell1{:} % Assigns each content to a
variable
I Structure Arrays (struct): data types with fields and values
I >> methane.omega = .012; % Methane’s acentric
factor
MATLAB: Introduction
MATLAB Classes
Cell and Structure Arrays
I Cell Arrays (cell): generic containers (store any type of data)
I >> cell1 = {’aaa’, 1, rand(2,3)} % Use curly
braces to retrieve/assign values
I >> a = cell1(1) % ‘a’ is the first container (also a
cell)
I >> b = cell1{1} % ‘b’ is the first content (a char
array)
I >> cell1{:} % {:} generates a comma-separated list
I >> [a,b,c] = cell1{:} % Assigns each content to a
variable
I Structure Arrays (struct): data types with fields and values
I >> methane.omega = .012; % Methane’s acentric
factor
I >> methane.Tc = 190.6; % Its critical temperature, K
MATLAB: Introduction
MATLAB Classes
Cell and Structure Arrays
I Cell Arrays (cell): generic containers (store any type of data)
I >> cell1 = {’aaa’, 1, rand(2,3)} % Use curly
braces to retrieve/assign values
I >> a = cell1(1) % ‘a’ is the first container (also a
cell)
I >> b = cell1{1} % ‘b’ is the first content (a char
array)
I >> cell1{:} % {:} generates a comma-separated list
I >> [a,b,c] = cell1{:} % Assigns each content to a
variable
I Structure Arrays (struct): data types with fields and values
I >> methane.omega = .012; % Methane’s acentric
factor
I >> methane.Tc = 190.6; % Its critical temperature, K
I >> methane.Pc = 45.99; % Its critical pressure, bar
MATLAB: Introduction
MATLAB Classes
Cell and Structure Arrays
I Cell Arrays (cell): generic containers (store any type of data)
I >> cell1 = {’aaa’, 1, rand(2,3)} % Use curly
braces to retrieve/assign values
I >> a = cell1(1) % ‘a’ is the first container (also a
cell)
I >> b = cell1{1} % ‘b’ is the first content (a char
array)
I >> cell1{:} % {:} generates a comma-separated list
I >> [a,b,c] = cell1{:} % Assigns each content to a
variable
I Structure Arrays (struct): data types with fields and values
I >> methane.omega = .012; % Methane’s acentric
factor
I >> methane.Tc = 190.6; % Its critical temperature, K
I >> methane.Pc = 45.99; % Its critical pressure, bar
I >> methane % Display methane fields and values
MATLAB: Introduction
Elements of Programming
Outline
MATLAB Classes
Elements of Programming
Plotting
2-D Plotting
3-D Plotting
MATLAB: Introduction
Elements of Programming
Relational and Logical Operators
I Relational Operators:
>, < greater than, smaller than
>=, <= greater or equal than, smaller or equal than
==, ∼= equal to, not equal to
I Logical Operators:
&&, & short-circuiting AND, element-wise AND
||, | short-circuiting OR, element-wise OR
∼ element-wise NOT
MATLAB: Introduction
Elements of Programming
if-elseif-else Statements: Flow Control
I General form: I Example:
if expression1 r = rand;
statements1 if (r < .3)
elseif expression2 r = r*2;
statements2 elseif (r >= .3 && ...
else r < .6)
statements3 r = r*3;
end else
r = r*4;
end
MATLAB: Introduction
Elements of Programming
switch-case Statements: Flow Control
I General form:
switch switch_expr
case case_expr
statement, ..., statement
case {case_expr1, case_expr2, case_expr3, ...}
statement, ..., statement
otherwise
statement, ..., statement
end
I Example:
method = ’Bilinear’;
switch lower(method)
case {’linear’, ’bilinear’}
disp(’Method is linear’)
otherwise
disp(’Unknown method’)
end
MATLAB: Introduction
Elements of Programming
for Loop Statements
I General form: I Example:
for var = init:step:end a = zeros(10);
statement for i = 1:10
statement for j = 1:10
... a(i,j) = 1/(i+j-1);
end end
end
MATLAB: Introduction
Elements of Programming
while Loop Statements
I General form: I Example:
while expression x0 = .5;
statement x = x0 - tan(x0);
statement while (sqrt(xˆ2 - x0ˆ2) > 1E-3)
... x0 = x;
end x = x0 - tan(x0);
end
sprintf(’x_end = %g’, x)
MATLAB: Introduction
Elements of Programming
try-catch Statements: Error Handling
I General form: I Example:
try try
statement fid = fopen(’a.txt’, ’r’);
... d_in = fread(fid);
catch [ME] % Optional catch EX
statement disp(’Exception caught!’)
... EX
end end
MATLAB: Introduction
Plotting
Outline
MATLAB Classes
Elements of Programming
Plotting
2-D Plotting
3-D Plotting
MATLAB: Introduction
Plotting
2-D Plotting
2-D Plotting I
I The plotting commands in MATLAB work in a similar way:
command(data1,data2,...,[’Prop1Name’,Prop1Value,...])
where data1, data2, . . . are arrays of data to be graphed
and ’Prop1Name’, Prop1Value, . . . are the plotting
properties’ names and respective values (optional)
I See MATLAB’s Help for a description of all lineseries
properties
I Some plotting commands: plot, loglog, semilogx,
semilogy
MATLAB: Introduction
Plotting
2-D Plotting
2-D Plotting II
I Basic example: plot sin(x) between [0, 2π]
x = linspace(0,2*pi);
y = sin(x);
figure
plot(x,y);
MATLAB: Introduction
Plotting
2-D Plotting
2-D Plotting III
I Adding more information to the plot of sin(x) between
[0, 2π]
x = linspace(0,2*pi);
y = sin(x);
figure
plot(x,y,’Color’,’red’);
title(’Plot of sin(x)’);
xlabel(’x’);
ylabel(’y’);
MATLAB: Introduction
Plotting
2-D Plotting
2-D Plotting IV
I Plotting multiple data on the same figure
x = linspace(-10,10,1000);
y = 2*x;
z = 4*x.^2 - 2;
w = 8*x.^3 - 12*x;
figure
plot(x,y,x,z,x,w);
title(’Plot of three
polynomials’);
xlabel(’x’);
ylabel(’H(x)’);
ylim([-10 10]);
legend(’H_2(x)’,’H_3(x)’,’H_4
(x)’);
MATLAB: Introduction
Plotting
2-D Plotting
2-D Plotting V
I Plotting multiple data on the same figure with hold on
and hold off
x = linspace(-1,1,1000);
y = (3*x.^2 - 1)/2;
z = (5*x.^3 - 3*x)/2;
figure
plot(x,y,’Color’,rand(1,3));
hold on;
plot(x,z,’Color’,rand(1,3));
hold off;
MATLAB: Introduction
Plotting
2-D Plotting
2-D Plotting VI
I Adding multiple plots on the same figure: subplot
x = linspace(-5,5,1000).’;
y = [x.^2, sin(x), cosh(x), exp(x), exp
(-x).*sin(x), x];
colors = lines(6);
figure(’Name’,’3-by-2 Plots’,’Color’,’
white’);
for i = 1:6
subplot(3,2,i);
plot(x,y(:,i),’Color’,colors(i,:));
end
MATLAB: Introduction
Plotting
3-D Plotting
3-D Plotting I
I In three dimensions, you can plot lines (plot3) and
surfaces (surf, surfc, mesh, meshc)
I See MATLAB’s Help for a description of all surface
properties
I Set the current color map with the command colormap
MATLAB: Introduction
Plotting
3-D Plotting
3-D Plotting II
I Basic example: plot z = xˆ2 + yˆ2
x = linspace(-10,10,1000);
y = x;
[X,Y] = meshgrid(x,y);
Z = X.^2 + Y.^2;
figure
surf(X,Y,Z,’EdgeColor’,’
none’);
xlabel(’x’);
ylabel(’y’);
zlabel(’z’);
MATLAB: Introduction
Plotting
3-D Plotting
3-D Plotting III
I Adding contours to z = xˆ2 - yˆ2
x = linspace(-5,5,50);
y = x;
[X,Y] = meshgrid(x,y);
Z = X.^2 - Y.^2;
figure
colormap(’cool’);
meshc(X,Y,Z);
xlabel(’x’);
ylabel(’y’);
zlabel(’z’);