t =0:0.001:2*pi;
x = cos(t);
y = sin(t);
plot(x,y);
hold on;
axis equal;[a,b]= ginput(3);
plot(a,b,'x');%给三个点标点
plot(a,b);%给三个点连线,但是只有两条线
line([a(1) a(3)],[b(1) b(3)]);%连上第三条线
sum= sqrt((a(1)-a(2)).^2+(b(1)-b(2)).^2)+sqrt((a(1)-a(3)).^2+(b(1)-b(3)).^2)+sqrt((a(3)-a(2)).^2+(b(3)-b(2)).^2);
disp(sum);
2.求10的阶乘。
function s = jiecheng(n);if n<=0
error('warning');returnelse
s =1;for i =1:n
s = s * i;
end
end
3.验证魔方矩阵的奇妙,即各行各列和对角线的元素之和都相同。
function f =magverfier(n);if n >2
x = magic(n);for j =1:n
row =0;for i =1:n
row = row + x(i,j);%计算各行的的总和
end
row
end
for i =1:n
col =0;for j =1:n
col = col + x(i,j);%计算各列的总和
end
col
end
diagsum =sum(diag(x)); %计算对角线之和
diagsum
else
end
4.绘制带洞孔的山峰表面。
close all[X,Y,Z]= peaks(30);%使用山峰函数返回30*30的矩阵z,用于参数绘图的矩阵x和y
x =X(1,:);%提取矩阵的第一行
y =Y(:,1);%提取矩阵的第一列
i = find(y>0.8& y<1.2);%查找这个范围的元素,返回查找结果的索引
j = find(x>-0.6& x<0.5);
Z(i,j)= nan*Z(i,j);%将查找的结果对应的元素设置为NaN
surf(X,Y,Z);
5.改变图片的格式。
a = imread('E:\dnf photos\1.jpg');
imwrite(a,'sss.bmp','bmp');
b=rgb2gray(a);
imwrite(b,'aaa.bmp','bmp');
subplot(1,2,1),imshow(a);
hold on
subplot(1,2,2),imshow(b);