MATLAB: Introduction: Bruno Abreu Calfa
MATLAB: Introduction: Bruno Abreu Calfa
Front Matter
MATLAB: Introduction
Part 2
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
ND Arrays
ND Arrays
ND Arrays
ND Arrays
ND Arrays
Outline
MATLAB Classes
Elements of Programming
Plotting
2-D Plotting
3-D Plotting
MATLAB: Introduction
Elements of Programming
I Relational Operators:
I Logical Operators:
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 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
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
command(data1,data2,...,[’Prop1Name’,Prop1Value,...])
2-D Plotting II
x = linspace(0,2*pi);
y = sin(x);
figure
plot(x,y);
MATLAB: Introduction
Plotting
2-D Plotting
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
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
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
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
3-D Plotting II
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
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’);