字符串 ch=['I"m','A','STUDENT'] subch=ch(1:5) revch=ch(end:-1:1) k=find(ch>='A'&ch<='Z') ch(k)=ch(k)-('A'-'a') char(ch)
最大值最小值 循环a=[。。。];max=a(1);min=a(1);for i=1:20 if a(i)>max max=a(i);end if a(i)<min min=a(i);end end max min 函数方式for i=1:20 a(i)=input('please input a number: ');M=max(a);m=min(a);end M m
分段函数 If语句 a=input('输入a=');b=input('输入b=');c=input('输入c=');x=input('输入x=') if x>=0.5 & x<=1.5 y=a*x^2+b*x+c;else if x>=1.5 & x<=3.5 y=a*(sin(b))^c+x;
else if x>=3.5 & x<=5.5 y=log(abs(b+(c/x)));end y switch语句 a=input('输入a=');
b=input('输入b=');c=input('输入c=');x=input('输入x=');switch (1) case x>=0.5 & x<=1.5
y=a*x^2+b*x+c; case x>=1.5 & x<=3.5 y=a*(sin(b))^c+x;otherwise x>=3.5 & x<=5.5
y=log(abs(b+(c/x)));end y 2的次方和 循环s=0;n=63; for i=0:n s=s+2^i; end s
函数n=0:63; f=2.^n; s=sum(f) 编辑函数并调用 函数function y = fx( x ) y=1./((x-2).^2+0.1)+1./((x-3).^4+0.01);end在命令窗口输入x=[1,2;3,4;5,6]; y=fx(x) 绘制正切和正弦函数加图例 x=0:0.1:1;y1=sin(x);y2=tan(x);
plot(x,y1,'k-.',x,y2,'k-.');xlabel('X轴');ylabel('Y轴');title('三角函数');text(0.5,0.4,'y=sin(x)');text(0.8,1.1,'y=tan(x)');legend('y1','y2'); 绘制参数方程 t=0:0.1:2*pi;x=sin(3*t).*cos(t);y=sin(3*t).*sin(t);plot(x,y,'r:'); 用plot3绘制曲线 t=0:0.1:20;x=exp(-t).*cos(t);y=exp(-t).*sin(t);z=t;plot3(x,y,z);grid on; 同一窗口绘制t=0:0.1:2*pi;y=sin(t);y1=sin(t+0.25);y2=sin(t+0.5);plot(t,y,t,y1,t,y2);legend('y','y1','y2');
同一窗口分图 t=0:0.1:2*pi;y=sin(t);subplot(2,2,1);plot(t,y);y1=sin(t+0.25);
subplot(2,2,2);plot(t,y1);y2=sin(t+0.5);subplot(2,1,2);plot(t,y2);不同窗口绘制 t=0:0.1:2*pi;y=sin(t);plot(t,y);legend('y=sin(t)');t=0:0.1:2*pi;y=sin(t+0.25);plot(t,y);legend('y=sin(t+0.25)');t=0:0.1:2*pi;y=sin(t+0.5);plot(t,y);legend('y=sin(t+0.5)');
同一坐标系绘制x=0:0.1:2*pi;y1=x.^2;y2=cos(2*x);y3=y1.*y2;
plot(x,y1,'k',x,y2,'r--',x,y3,'b-.');以子图绘制 x=0:0.1:2*pi;y1=x.^2;y2=cos(2*x);
y3=y1.*y2;subplot(2,2,1);plot(x,y1,'k');subplot(2,2,2);plot(x,y2,'r--');subplot(2,1,2);plot(x,y3,'b-.');矩阵各行各列最大值和整个矩阵最大最小值 A=[13,-56,78;25,63,-235;78,25,563;1,0,-1];max(A,[],2) min(A,[],2) max(A) min(A)
max(max(A)) min(min(A)) 100个学生的5门成绩 t=45+50*rand(100,5); p=fix(t); [x,m]=max(p) [y,k]=min(p) mu=mean(p) sig=std(p) s=sum(p,2) [X,h]=max(s) [Y,n]=min(s) [zcj,xsxh]=sort(s) 计算f(x)=3x5+4x3-5x2-7.2x+5的全部根并构造g(x)比较