立即学习:https://edu.csdn.net/course/play/5974/112229?utm_source=blogtoedu
for 循环变量=开始值:[步长]:终止值
语句体
end
for i=1:5
i
end
for i=2:4:14
i
end
a=5;
while a<10
a
a=a+1;
end
for i=1:5
if i==3
continue
end
i
end
for i=1:5
if i==3
break
end
i
end
try
语句1
catch
语句2
end
a=ones(3,3)
a(14,:)
det(rand(2,4)) 取行列式
a=6
a
try
det(rand(2,4))
catch
disp(laster)
end
try
a(14,:)
catch
disp(‘发生错误,具体为:’)
disp(lasterr)
end
try
a(14,:)
catch
disp(‘error’)
disp(lasterr)
end
新建脚本
for i=1:100
i
end
ctrl+s保存在默认文件夹下
clear all
创建新文档
函数
输出 输入
function [ c ] = myadd( a,b )
%UNTITLED3 此处显示有关此函数的摘要
% 此处显示详细说明
% c=a+b
c=a+b
end
ctrl+S(区分大小写)
% 注释
myadd(4,6 )
x=1:10
y=sin(x)
line(x,y)
x=1:0.01:10
y=sin(x)
line(x,y)
F5 运行
ctrl+R 快速注释
plot函数
plot(x)
plot(x,y)
plot(x1,y1,x2,y2,…,xn,yn)
x=43 91 18 26 15
plot(x)
x=0:0.1:2*3.14
y=sin(x)
plot(x,y)
x=1:5
y=rand(4,5)
plot(x,y)
x1=0:0.1:2*3.14
y1=sin(x1)
x2=3.14:0.1:3*3.14
y2=cos(x2)
plot(x1,y1,x2,y2)
plot(x,y1x,y2)
polar(theta,rho) 极坐标
a-2*pi:0.001:2*pi; %设定角度
b=(1-sin(a)); %设定对应角度的半径
polar(a,b) %绘图
pi
polar([a a a],[b c d])
plot(x,y,线的格式)
‘-‘ solid line(default)
‘- -‘ dashed line
‘:’ dotted line
‘-.’ dash-dotted line
‘linestyle’,‘none’ no line
plot(x,y, ‘linestyle’,‘none’)
plot(x,y,线的颜色)
‘yellow’ ‘y’ [1 1 0]
‘magenta’ ‘m’ [1 0 1]
‘cyan’ ‘c’ [0 1 1]
‘red’ ‘r’ [1 0 0]
‘green’ ‘g’ [0 1 0]
‘blue’ ‘b’ [0 0 1]
‘white’ ‘w’ [1 1 1]
‘black’ ‘k’ [0 0 0]
a=0:0.1:2*pi
b=sin(a)
plot(a,b,’color’,’red’)
plot(a,b,’:m’)
plot(a,b,’- -k’)
plot(x,y,数据点的格式)
‘o’ circle
‘+’ plus sign
‘*’ asterisk
‘.’ point
‘x’ cross
‘square’or’s’ square
‘diamond’or’d’ diamond
‘^’ upward-pointing triangle
‘v’ downward-pointing triangle
‘>’ right-pointing triangle
‘<’ left-pointing triangle
‘pentagram’or’p’ five-pointed star(pentagram)
‘hexagram’or’h’ six-pointed star(hexagram)
‘none’ no markers
plot(x,y,'marker','+')
plot(x,y, ,'o')
plot(x,y, ,'-ro')
plot(x,y,格式)
plot(x,y,属性名称,属性值)
‘linewidth’ 0.5(default)
‘markeredgecolor’ auto(default)
‘markerfacecolor’ none(default)
‘markersize’ 6(default)
x=0:0.1:2*pi;
y=sin(x);
polt(x,y,’linewidth’,6);
polt(x,y,’o’,’ markeredgecolor’,’r’);
polt(x,y,’o’,’ markerfacecolorr’,’r’);
polt(x,y,’ markersize’,12);
subplot(x,y,i)
x=0:0.1:2*pi;
y1=sin(x);
y2=cos(x);
y3=x;
y4=x.^2;
subplot(2,2,1),plot(x,y1);
subplot(2,2,2),plot(x,y2);
subplot(2,2,3),plot(x,y3);
subplot(2,2,4),plot(x,y4);
hold on
hold off
x=0:0.1:2*pi;
y1=sin(x);
y2=cos(x);
y3=1-sin(x);
plot(x,y1);
hold on;
plot(x,y2);
hold off;
plot(x,y3);
坐标轴范围
axis([x轴范围,y轴范围])
axis auto
axis manual 叠加绘图
axis tight 紧凑型
坐标轴比例
axis equal 比例相同
axis square 方形
axis normal 默认
关闭
axis off
x=0:0.1:4*pi;
y=sin(x);
x2=-100:100;
y2=cos(x2)
plot(x,y);axis tight;
plot(x,y); axis ([-100 100 -100 100]);
plot(x,y);axis manual;
hold on;
plot(x2,y2);
x=0:0.1:10*pi;
y=sin(x);
plot(x,y);axis equal;
plot(x,y);axis square;
plot(x,y);axis nomal;
plot(x,y);axis tight nomal;
plot(x,y);axis off;
ctrl+t 取消注释
set
set(gca,’xTick’,刻度)
set(gca,’xTickLabel’,刻度标签)
x=0:0.1:2*pi;
y=sin(x);
plot(x,y);
set(gca,’xTick’,[0 pi/2 pi 3*pi/2 2*pi]) ;
set(gca,’xTickLabel’,{‘0’ ‘pi/2’ ‘pi’ ‘3*pi/2’ ‘2*pi’})
全对数/半对数
25 VS 0.1
semilogx X轴使用对数
semilogy Y轴使用对数
loglog XY轴均采用对数
x=0:0.1:10;
y=exp(x);
subplot(2,2,1),plot(x,y);
subplot(2,2,2), semilogx(x,y);
subplot(2,2,3), semilogy(x,y);
subplot(2,2,4),loglog(x,y);
plotyy
x=1:100;
y1=rand(1,100)*1000;
y2=x.^2;
plot(x,y1,x,y2);
plotyy(X1,Y1,X2,Y2)
[AX,H1,H2]=plotyy(参数)
AX AX(1)左侧坐标句柄,AX(2)右侧坐标句柄
H1 左侧坐标图线句柄
H2 右侧坐标图线句柄
x=1:100;
y1=rand(1,100);
y2=x.^2;
[ax ha hb]=plotyy(x,y1,x,y2);
set(ha,’color’,’g’);
set(hb,’color’,’m’);
ylabel(ax(1),’随机数’);
ylabel(ax(2),’平方’);
axis on/axis off 坐标轴
box on/box off 边界线
grid on/grid off 网格线
x=1:0.1:2*pi;
y=sin(x);
subplot(2,2,1),plot(x,y);
subplot(2,2,2),plot(x,y),axis off;
subplot(2,2,3),plot(x,y),box off;
subplot(2,2,4),plot(x,y),grid on;
for 循环变量=开始值:[步长]:终止值
语句体
end
for i=1:5
i
end
for i=2:4:14
i
end
a=5;
while a<10
a
a=a+1;
end
for i=1:5
if i==3
continue
end
i
end
for i=1:5
if i==3
break
end
i
end
try
语句1
catch
语句2
end
a=ones(3,3)
a(14,:)
det(rand(2,4)) 取行列式
a=6
a
try
det(rand(2,4))
catch
disp(laster)
end
try
a(14,:)
catch
disp(‘发生错误,具体为:’)
disp(lasterr)
end
try
a(14,:)
catch
disp(‘error’)
disp(lasterr)
end
新建脚本
for i=1:100
i
end
ctrl+s保存在默认文件夹下
clear all
创建新文档
函数
输出 输入
function [ c ] = myadd( a,b )
%UNTITLED3 此处显示有关此函数的摘要
% 此处显示详细说明
% c=a+b
c=a+b
end
ctrl+S(区分大小写)
% 注释
myadd(4,6 )
x=1:10
y=sin(x)
line(x,y)
x=1:0.01:10
y=sin(x)
line(x,y)
F5 运行
ctrl+R 快速注释
plot函数
plot(x)
plot(x,y)
plot(x1,y1,x2,y2,…,xn,yn)
x=43 91 18 26 15
plot(x)
x=0:0.1:2*3.14
y=sin(x)
plot(x,y)
x=1:5
y=rand(4,5)
plot(x,y)
x1=0:0.1:2*3.14
y1=sin(x1)
x2=3.14:0.1:3*3.14
y2=cos(x2)
plot(x1,y1,x2,y2)
plot(x,y1x,y2)
polar(theta,rho) 极坐标
a-2*pi:0.001:2*pi; %设定角度
b=(1-sin(a)); %设定对应角度的半径
polar(a,b) %绘图
pi
polar([a a a],[b c d])
plot(x,y,线的格式)
‘-‘ solid line(default)
‘- -‘ dashed line
‘:’ dotted line
‘-.’ dash-dotted line
‘linestyle’,‘none’ no line
plot(x,y, ‘linestyle’,‘none’)
plot(x,y,线的颜色)
‘yellow’ ‘y’ [1 1 0]
‘magenta’ ‘m’ [1 0 1]
‘cyan’ ‘c’ [0 1 1]
‘red’ ‘r’ [1 0 0]
‘green’ ‘g’ [0 1 0]
‘blue’ ‘b’ [0 0 1]
‘white’ ‘w’ [1 1 1]
‘black’ ‘k’ [0 0 0]
a=0:0.1:2*pi
b=sin(a)
plot(a,b,’color’,’red’)
plot(a,b,’:m’)
plot(a,b,’- -k’)
plot(x,y,数据点的格式)
‘o’ circle
‘+’ plus sign
‘*’ asterisk
‘.’ point
‘x’ cross
‘square’or’s’ square
‘diamond’or’d’ diamond
‘^’ upward-pointing triangle
‘v’ downward-pointing triangle
‘>’ right-pointing triangle
‘<’ left-pointing triangle
‘pentagram’or’p’ five-pointed star(pentagram)
‘hexagram’or’h’ six-pointed star(hexagram)
‘none’ no markers
plot(x,y,'marker','+')
plot(x,y, ,'o')
plot(x,y, ,'-ro')
plot(x,y,格式)
plot(x,y,属性名称,属性值)
‘linewidth’ 0.5(default)
‘markeredgecolor’ auto(default)
‘markerfacecolor’ none(default)
‘markersize’ 6(default)
x=0:0.1:2*pi;
y=sin(x);
polt(x,y,’linewidth’,6);
polt(x,y,’o’,’ markeredgecolor’,’r’);
polt(x,y,’o’,’ markerfacecolorr’,’r’);
polt(x,y,’ markersize’,12);
subplot(x,y,i)
x=0:0.1:2*pi;
y1=sin(x);
y2=cos(x);
y3=x;
y4=x.^2;
subplot(2,2,1),plot(x,y1);
subplot(2,2,2),plot(x,y2);
subplot(2,2,3),plot(x,y3);
subplot(2,2,4),plot(x,y4);
hold on
hold off
x=0:0.1:2*pi;
y1=sin(x);
y2=cos(x);
y3=1-sin(x);
plot(x,y1);
hold on;
plot(x,y2);
hold off;
plot(x,y3);
坐标轴范围
axis([x轴范围,y轴范围])
axis auto
axis manual 叠加绘图
axis tight 紧凑型
坐标轴比例
axis equal 比例相同
axis square 方形
axis normal 默认
关闭
axis off
x=0:0.1:4*pi;
y=sin(x);
x2=-100:100;
y2=cos(x2)
plot(x,y);axis tight;
plot(x,y); axis ([-100 100 -100 100]);
plot(x,y);axis manual;
hold on;
plot(x2,y2);
x=0:0.1:10*pi;
y=sin(x);
plot(x,y);axis equal;
plot(x,y);axis square;
plot(x,y);axis nomal;
plot(x,y);axis tight nomal;
plot(x,y);axis off;
ctrl+t 取消注释
set
set(gca,’xTick’,刻度)
set(gca,’xTickLabel’,刻度标签)
x=0:0.1:2*pi;
y=sin(x);
plot(x,y);
set(gca,’xTick’,[0 pi/2 pi 3*pi/2 2*pi]) ;
set(gca,’xTickLabel’,{‘0’ ‘pi/2’ ‘pi’ ‘3*pi/2’ ‘2*pi’})
全对数/半对数
25 VS 0.1
semilogx X轴使用对数
semilogy Y轴使用对数
loglog XY轴均采用对数
x=0:0.1:10;
y=exp(x);
subplot(2,2,1),plot(x,y);
subplot(2,2,2), semilogx(x,y);
subplot(2,2,3), semilogy(x,y);
subplot(2,2,4),loglog(x,y);
plotyy
x=1:100;
y1=rand(1,100)*1000;
y2=x.^2;
plot(x,y1,x,y2);
plotyy(X1,Y1,X2,Y2)
[AX,H1,H2]=plotyy(参数)
AX AX(1)左侧坐标句柄,AX(2)右侧坐标句柄
H1 左侧坐标图线句柄
H2 右侧坐标图线句柄
x=1:100;
y1=rand(1,100);
y2=x.^2;
[ax ha hb]=plotyy(x,y1,x,y2);
set(ha,’color’,’g’);
set(hb,’color’,’m’);
ylabel(ax(1),’随机数’);
ylabel(ax(2),’平方’);
axis on/axis off 坐标轴
box on/box off 边界线
grid on/grid off 网格线
x=1:0.1:2*pi;
y=sin(x);
subplot(2,2,1),plot(x,y);
subplot(2,2,2),plot(x,y),axis off;
subplot(2,2,3),plot(x,y),box off;
subplot(2,2,4),plot(x,y),grid on;