182 lines
3.6 KiB
Matlab
182 lines
3.6 KiB
Matlab
%%%% ECE-210-B HW 3 - Plotting
|
|
% James Ryan, 02/14/24
|
|
|
|
% preamble
|
|
close all; clear; clc;
|
|
|
|
%% Two Dimensions
|
|
% (1)
|
|
x = linspace(0, 2*pi, 50);
|
|
f = sin(x);
|
|
|
|
figure;
|
|
plot(x, f, "g");
|
|
xlim([0 2*pi]); % plot only within x's range
|
|
xticks([0:pi/2:2*pi]);
|
|
xticklabels({'0','\pi/2','\pi','3\pi/2','2\pi'})
|
|
xlabel("x");
|
|
ylabel("sin(x)");
|
|
title('Plot of sin(x) over 50 evenly spaced points between [0, 2\pi]');
|
|
|
|
% (2)
|
|
x; % 50 evenly spaced pts between [0, 2pi]
|
|
g = cos(2.*x);
|
|
|
|
figure;
|
|
hold on;
|
|
plot(x, f, "g");
|
|
plot(x, g, "b");
|
|
hold off;
|
|
xlim([0 2*pi]); % plot only within x's range
|
|
xticks([0:pi/2:2*pi]);
|
|
xticklabels({'0','\pi/2','\pi','3\pi/2','2\pi'})
|
|
|
|
xlabel("x");
|
|
ylabel("y");
|
|
legend( 'y = sin(x)', ...
|
|
'y = cos(2x)');
|
|
title('knock knock! whose there? sin! sine who? $\pm\sqrt{\frac{1 + cos2\theta}{2}}$', "interpreter", "latex");
|
|
|
|
% (3)
|
|
|
|
figure;
|
|
x; % 50 evenly spaced pts between [0, 2pi]
|
|
|
|
% top plot - f = sin(x)
|
|
subplot(2,1,1);
|
|
stem(x, f, "r*");
|
|
grid on
|
|
xlim([0 2*pi]);
|
|
xticks([0:pi/2:2*pi]);
|
|
xticklabels({'0','\pi/2','\pi','3\pi/2','2\pi'})
|
|
xlabel("t [s]"); % we're pretending its time now :)
|
|
ylabel("Position [d]");
|
|
title('Position of a controlled laser pointer, relative to origin location');
|
|
%subtitle('Sampled 50 times over 2\pi seconds.');
|
|
|
|
|
|
% bottom plot - g = cos(2x)
|
|
% changing cos(2x)'s amplitude; computing amplitude ratio in d
|
|
rand_amp = 100 * rand;
|
|
g = rand_amp .* g;
|
|
amplitude_ratio_db = 20.*log10(rand_amp);
|
|
|
|
subplot(2,1,2);
|
|
stem(x, g, "color", "#D95319", "Marker", "+", "MarkerEdgeColor", "#D95319", "MarkerFaceColor", "#D95319");
|
|
grid on
|
|
xlim([0 2*pi]);
|
|
xticks([0:pi/2:2*pi]);
|
|
xticklabels({'0','\pi/2','\pi','3\pi/2','2\pi'})
|
|
yticks(linspace(-rand_amp, rand_amp, 5));
|
|
|
|
xlabel("t [s]");
|
|
ylabel("Position [d]");
|
|
title(sprintf('Pet cats vertical position while seeking laser pointer. \n Position Ratio (vs sin) [dB]: %d' ...
|
|
,amplitude_ratio_db));
|
|
%subtitle('Sampled 50 times over 2\pi seconds.');
|
|
|
|
|
|
% (4)
|
|
|
|
pointer = imread("./pointer.jpg");
|
|
%{
|
|
returns a 720x1280x3 matrix, corresponding to
|
|
720 rows
|
|
1280 columns
|
|
3 color channels per (row,col) pair
|
|
to form a complete color image, in matrix form.
|
|
Each entry in the matrix is stored as uint8, or
|
|
an 8-bit integer, implying colors are specified as
|
|
hex codes (eg #RRGGBB - specify the Red, Green, and
|
|
Blue values in HEX, 8 bits per channel, 3 channels).
|
|
%}
|
|
|
|
% Invert color channels
|
|
new_pointer = 255 - pointer;
|
|
|
|
figure;
|
|
imshow(pointer);
|
|
figure;
|
|
imshow(new_pointer);
|
|
|
|
%% Three Dimensions
|
|
% (1)
|
|
x = 0:.01:1;
|
|
y = x;
|
|
z = exp(-x.^2 - y.^2);
|
|
|
|
figure;
|
|
|
|
% "Create this plot
|
|
% three times
|
|
% in three different subplots
|
|
% using plot3..., scatter3, surf, *and* mesh (4 tools?)"
|
|
|
|
% I'm making 4 plots, because you said "and", meaning all the tools.
|
|
|
|
% plot3
|
|
subplot(2,2,1);
|
|
plot3(x,y,z);
|
|
xlabel("x");
|
|
ylabel("y");
|
|
zlabel("z");
|
|
title('z = exp(-x^2 - y^2) (PLOT3)');
|
|
legend('z = exp(-x^2 - y^2)');
|
|
daspect([1 1 1]);
|
|
view([-2 2 1]);
|
|
grid on;
|
|
|
|
|
|
% scatter3
|
|
subplot(2,2,2);
|
|
scatter3(x,y,z);
|
|
xlabel("x");
|
|
ylabel("y");
|
|
zlabel("z");
|
|
title('z = exp(-x^2 - y^2) (SCATTER)');
|
|
legend('z = exp(-x^2 - y^2)');
|
|
daspect([1 1 1]);
|
|
view([-2 2 1]);
|
|
grid on;
|
|
|
|
% surf
|
|
[X, Y] = meshgrid(x);
|
|
Z = exp(-X.^2 - Y.^2);
|
|
|
|
subplot(2,2,3);
|
|
surf(X,Y,Z);
|
|
xlabel("x");
|
|
ylabel("y");
|
|
zlabel("z");
|
|
title('z = exp(-x^2 - y^2) (SURF)');
|
|
legend('z = exp(-x^2 - y^2)');
|
|
grid on;
|
|
view([-2 5 1.3]);
|
|
|
|
% mesh
|
|
subplot(2,2,4);
|
|
mesh(X,Y,Z);
|
|
xlabel("x");
|
|
ylabel("y");
|
|
zlabel("z");
|
|
title('z = exp(-x^2 - y^2) (MESH)');
|
|
legend('z = exp(-x^2 - y^2)');
|
|
grid on;
|
|
view([-2 5 1.3]);
|
|
|
|
% (2)
|
|
t = 0:0.01:1;
|
|
x = t.*cos(27.*t);
|
|
y = t.*sin(27.*t);
|
|
z = t;
|
|
|
|
figure;
|
|
plot3(x,y,t);
|
|
xlabel("x");
|
|
ylabel("y");
|
|
zlabel("z");
|
|
title('Wheeee!');
|
|
grid on;
|
|
daspect([1 1 1]);
|
|
view([90 90 20]);
|