0% found this document useful (0 votes)
2 views10 pages

(Matlab) Week2-File

The document contains a homework assignment with various programming problems related to data visualization and analysis using MATLAB. It includes tasks such as creating semilog plots, subplots, bar graphs, surface plots, and implementing functions for finding nearest values and smoothing data. Additionally, it features exercises on circle plotting and flow control using loops.

Uploaded by

ducminh111206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

(Matlab) Week2-File

The document contains a homework assignment with various programming problems related to data visualization and analysis using MATLAB. It includes tasks such as creating semilog plots, subplots, bar graphs, surface plots, and implementing functions for finding nearest values and smoothing data. Additionally, it features exercises on circle plotting and flow control using loops.

Uploaded by

ducminh111206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Homework #1

Họ tên: Phùng Đức Minh MSSV:202413089

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);

Z1 = interp2(X0, Y0, Z0, X1,


Y1, 'cubic');
surf(X1, Y1, Z1);
colormap(hsv);
shading interp;

hold on;
contour(X1, Y1, Z1, 15, 'k');

colorbar;
caxis([0 1]);

% Phien ban cua em khong ve


duoc mau cua cac duong contour
thay a.

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:

% Loops and flow control


function loopTest(N)
for n = 1:N
if mod(n,2)==0 &&
mod(n,3)==0
disp([num2str(n) ' is
divisible by 2 AND 3']);
elseif mod(n,2)==0
disp([num2str(n) ' is
divisible by 2']);
elseif mod(n,3)==0
disp([num2str(n) ' is
divisible by 3']);
else
disp([num2str(n) ' is
NOT divisible by 2 or 3']);
end
end
end

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

halfWin = floor(width / 2);


len = length(x);
smoothed = zeros(size(x));

for n = 1:len
startIdx = max(1, n -
halfWin);
endIdx = min(len, n +
halfWin);
smoothed(n) =
mean(x(startIdx:endIdx));
end
end

% Testing the Smoothing filter


load noisyData.mat
smoothed = rectFilt(x, 11);
plot(x, 'bo');
hold on;
plot(smoothed, 'r-', 'LineWidth',
1.5);
legend('Original Data', 'Smoothed');
title('Smoothing Illustration');
xlabel('Index');
ylabel('Data Value');
grid on;
Problem 8:
% Part a
function [x, y] = getCircle(center, r)
t = linspace(0, 2*pi, 100);
x = r * cos(t) + center(1);
y = r * sin(t) + center(2);
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;

colors = {'b', 'black', 'r', 'y', 'g'};


centers = [0 0; 2 0; 4 0; 1 -1.5; 3 -1.5];
r = 1;

for i = 1:5
[x, y] = getCircle(centers(i, :), r);
plot(x, y, 'Color', colors{i},
'LineWidth', 2.5);
end

You might also like