(Matlab) Week2-File
(Matlab) Week2-File
Problem 1:
% Semilog plot
x = 1:6;
y = [15, 25, 55, 115, 144,
242];
semilogy(x, y, 'ms',
'MarkerSize', 10, 'LineWidth',
4);
hold on;
xlabel('Year');
ylabel('Number of Students');
title('Enrollment Growth Over
6 Years');
xlim([0 7]);
grid on;
Problem 2:
% Sublot and axis modes
load('mitMap.mat');
figure;
subplot(2,2,1);
image(mit);
colormap(cMap);
axis square;
title('Square');
subplot(2,2,2);
image(mit);
colormap(cMap);
axis tight;
title('Tight');
subplot(2,2,3);
image(mit);
colormap(cMap);
axis equal;
title('Equal');
subplot(2,2,4);
image(mit);
colormap(cMap);
axis xy;
title('XY');
Problem 3:
% Bar graph
v = rand(1,5);
bar(v, 'r');
title('Bar Graph of 5 Random
Values');
Problem 4:
% Interpolation and surface
plots
Z0 = rand(5);
[X0, Y0] = meshgrid(1:5);
[X1, Y1] = meshgrid(1:0.1:5);
hold on;
contour(X1, Y1, Z1, 15, 'k');
colorbar;
caxis([0 1]);
Problem 5:
% Fun with find
function [row, col] =
findNearest(x, desiredVal)
diff = abs(x - desiredVal);
minDiff = min(diff(:));
[row, col] = find(diff ==
minDiff);
end
Problem 6:
Problem 7:
% Smoothing filter
function smoothed = rectFilt(x,
width)
if mod(width, 2) == 0
width = width + 1;
warning('Width is not odd.
Increasing by 1 to make it odd.');
end
for n = 1:len
startIdx = max(1, n -
halfWin);
endIdx = min(len, n +
halfWin);
smoothed(n) =
mean(x(startIdx:endIdx));
end
end
% Part b
figure;
colors = jet(5);
hold on;
axis equal;
for i = 1:5
r = i;
[x, y] = getCircle([0, 0], r);
plot(x, y, 'LineWidth', 2, 'Color',
colors(i, :));
end
% Part c
figure;
hold on;
axis equal;
for i = 1:5
[x, y] = getCircle(centers(i, :), r);
plot(x, y, 'Color', colors{i},
'LineWidth', 2.5);
end