配套课件可访问https://github.com/BIMK/MATLAB下载,是安徽大学Matlab本科教学课件,逻辑清晰,简洁明了,颇为实用,用来入门再好不过(机器学习方向)。
本系列博文是课后练习的个人解答,通过几个小实验展示matlab基本语法和技巧。接触Matlab也有几年了,略有心得,分享给大家。
1. 熟悉matlab图形绘制的基本方法
2. 掌握二维曲线、点集、等高线的绘制
3. 掌握三维曲面、点集的绘制
%% 二维曲线绘制
function task1()
x = 0:0.01:1;
y = sqrt(1-x.^2);
figure;
plot(x,y);
end
%% 二维点集绘制1
function task2()
x = 0:0.01:1;
y = sqrt(1-x.^2);
figure;
plot(x,y,'LineStyle','none','Marker','o','MarkerSize',8,'MarkerEdgeColor',[0 0.4470 0.7410],'MarkerFaceColor',[0.3010 0.7450 0.9330]);
end
%% 二维点集绘制2
function task3()
t = 0:0.01:pi/2;
x = sin(t);
y = cos(t);
figure;
plot(x,y,'LineStyle','none','Marker','o','MarkerSize',8,'MarkerEdgeColor',[0 0.4470 0.7410],'MarkerFaceColor',[0.3010 0.7450 0.9330]);
end
%% 二维等高线绘制
function task4()
[x,y] = meshgrid(0:0.01:1);
z = x.^2+y.^2>1;
figure;
contour(x,y,z,1);
end
%% 三维曲面绘制1
function task5()
[x,y,z] = sphere;
figure;
surf(x,y,z,'EdgeColor','none');
end
%% 三维曲面绘制2
function task6()
[x,y] = meshgrid(0:0.01:1);
z = sqrt(1-x.^2-y.^2);
for i=1:size(z,1)
for j=1:size(z,2)
if ~isreal(z(i,j))
z(i,j)=nan;
end
end
end
figure;
surf(x,y,z,'EdgeColor','none');
view(79,35);
end
%% 三维曲面绘制3
function task7()
[x,y,z] = meshgrid(0:0.01:1);
value = x.^2+y.^2+z.^2;
figure;
patch(isosurface(x,y,z,value,1));
view(68,25);
end
%% 三维点集绘制1
function task8()
[x,y] = meshgrid(0:0.05:1);
z = sqrt(1-x.^2-y.^2);
z(x.^2+y.^2>=1) = nan;
figure;
plot3(x,y,z,'LineStyle','none','Marker','o','MarkerSize',8,'MarkerEdgeColor',[0 0.4470 0.7410],'MarkerFaceColor',[0.3010 0.7450 0.9330]);
view(135,39);
end
%% 三维点集绘制2
function task9()
[s,t] = meshgrid(0:0.1:pi/2);
x = cos(s).*cos(t);
y = cos(s).*sin(t);
z = sin(s);
figure;
plot3(x,y,z,'LineStyle','none','Marker','o','MarkerSize',8,'MarkerEdgeColor',[0 0.4470 0.7410],'MarkerFaceColor',[0.3010 0.7450 0.9330]);
view(135,39);
end
%% 三维点集绘制3
function task10()
V = (0:1:16)./15;
x = nchoosek(V,2);
for i=1:size(x,1)
for j=1:size(x,2)
x(i,j) = x(i,j)-(j-1)/15;
end
end
s = zeros(size(x,1),3);
s(:,1) = x(:,1);
s(:,2) = x(:,2)-x(:,1);
s(:,3) = 1-x(:,2);
for i=1:size(x,1)
s(i,:) = s(i,:)./sqrt(sum(s(i,:).^2));
end
figure;
plot3(s(:,1),s(:,2),s(:,3),'LineStyle','none','Marker','o','MarkerSize',8,'MarkerEdgeColor',[0 0.4470 0.7410],'MarkerFaceColor',[0.3010 0.7450 0.9330]);
view(135,39);
end