matlab实验代码(总)


% 使用两种方法,创建一稀疏矩阵
% 使用函数sparse,可以用一组非零元素直接创建一个稀疏矩阵。该函数调用格式为:
% S=sparse(i,j,s,m,n)
% 其中i和j都为矢量,分别是指矩阵中非零元素的行号与列号,
% s是一个全部为非零元素矢量,元素在矩阵中排列的位置为(i,j)
% m为输出的稀疏矩阵的行数,n为输出的稀疏矩阵的列数。
%方法1
A9=[0 0 1;0 3 0;2 4 0]
B9=sparse(A9)
C9=full(B9)
%方法2
A10=sparse([1 3 2 4],[2 3 1 4],[1 2 3 4],4,4)
C10=full(A10)

A11=[1 2 3];B11=[4 5 6];
C11=3.^A11
D11=A11.^B11

%使用函数,实现矩阵左旋90°或右旋90°的功能。
A=[ 1 2 3 ; 4 5 6 ; 7 8 9 ]
B=rot90(A,1)
C=rot90(A,-1)

%求S=2^0+2^1+2^2+2^3+2^4+……+2^10的值(提示:利用求和函数与累乘积函数。)
A=2*ones(1,10)%10个2
B=cumprod(A)%平方
C=sum(B)+1%加上2^0

%建立一个字符串向量,删除其中的大写字母(提示:利用find函数和空矩阵。)
str='AAAbCcd'

b=find(str>='A' & str<='Z');

str(b)=[];


% 输入一个百分制成绩,要求输出成绩等级A、B、C、D、E。其中90分~100分为A,80分~89分为B,70分~79为C,60分~69分为D,60分以下为E。
switch(score)
    case num2cell(90:0.5:100) 
    	disp(['成绩等级为:A']);
    case num2cell(80:0.5:89.5) 
    	disp(['成绩等级为:B']);
    case num2cell(70:0.5:79.5) 
    	disp(['成绩等级为:C']);
    case num2cell(60:0.5:69.5) 
    	disp(['成绩等级为:D']);
    case num2cell(0:0.5:59.5) 
    	disp(['成绩等级为:E']);
    otherwise
   	 disp(['输入成绩不合理!']);
end

%设计程序,完成两位数的加、减、乘、除四则运算,
%即产生两个两位随机整数,再输入一个运算符号,做相应的运算,显示相应的结果,并要求结果显示类似于“a=x+y=34”。

x=fix(100*rand());
y=fix(100*rand());
opt=input('操作:','s');
switch(opt)
case '+'
	fprintf('%d=%d+%d
',x+y,x,y);
case '-'
	fprintf('%d=%d-%d
',x-y,x,y);
case '*'
	fprintf('%d=%d*%d
',x*y,x,y);
case '/'
	fprintf('%d=%d/%d
',x/y,x,y);
otherwise
	fprintf('输入错误!');
end
%------------分段函数
x=[-5.0,-3.0,1.0,2.0,2.5,3.0,5.0];
y=(x.*x+x-6).*(x<0&(x~=-3))+(x.*x-5.*x+6).*(x>=0&x<10&x~=2&x~=3)+(x.*x-x-1).*(x==-3|x==2|x==3|x>=10)

%------------插值函数interp1;
x=0:1:4*pi;
y=sin(x).*exp(-x./5);
xi=0:0.3:4*pi;
y1=interp1(x,y,xi,'nearset');
y2=interp1(x,y,xi,'linear');
y3=interp1(x,y,xi,'spline');
y4=interp1(x,y,xi,'cubic');
plot(x,y,'-m',xi,y1,'-r',xi,y2,'-g',xi,y3,'-b',xi,y4,'-y');

%------------插值函数interp2
x=[13 15 17 19 20];
y=[0 1]';
z=[270 290 330 350 380;250 270 310 330 360];
x2=[13:20];
y2=y;
z2=interp2(x,y,z,x2,y2);
plot(x2,z2)

%------------曲线拟合
%例1
x=[2009:2015];
y=[70 122 144 152 174 196 202];
k=polyfit(x,y,1);%求系数
x2=2009:2017;
y2=polyval(k,x2);
plot(x,y,'-r',x2,y2,'-b');

%例2
load census.mat;
k=polyfit(cdate,pop,2);
x=[1790:10:2010];
y=polyval(k,x);
y2=polyval(k,cdate);
plot(cdate,pop,'*',cdate,y2,'r',x,y,'b-');

%------------plot二维绘图
x=-2:0.1:2;
y=exp(x);
x2=0:0.1:5;
y2=log(x2);
plot(x,y,'-r',x2,y2,'-b');
legend('y=e^x','y=logx');
xlabel('x');
ylabel('y');
title('二维图');
grid on;

x=0:pi/100:pi/2;
y=tan(x);
plot(x,y,'-r+');
xlabel('X');
ylabel('Y');
title('正切函数');
legend('y=tan(x)');
axis([0 pi/2 0 5]);

t=-1*pi:pi/20:pi;
x=t.*cos(3*t);
y=t.*sin(t).^2;
plot(x,y,'-r*');
legend(strvcat('x=tcos(3t)','y=tsin^2t'));

%--------曲面图和线框图
x=meshgrid(-1.5*pi:1.5*pi);
y=x;
z=cos(x).*sin(y)./y;
figure(1);
surf(z);
legend('曲面图');
figure(2);
mesh(z);
legend('线框图');

%-------特殊图形
data=rand(3,3);

subplot(2,2,1);
bar(data);
title('条状图');

subplot(2,2,2);
area(data);
title('面积图');

subplot(2,2,3);
pie(data);
title('饼图');

subplot(2,2,4);
stem(data);
title('火柴图');

原文地址:https://www.cnblogs.com/aeipyuan/p/12285301.html