The MATLAB Notebook.docx
《The MATLAB Notebook.docx》由会员分享,可在线阅读,更多相关《The MATLAB Notebook.docx(24页珍藏版)》请在冰豆网上搜索。
TheMATLABNotebook
数组数组的生成
冒泡法生成
x1=1:
pi
x2=0:
0.5:
pi
x3=pi:
-0.5:
0
x1=
123
x2=
00.50001.00001.50002.00002.50003.0000
x3=
3.14162.64162.14161.64161.14160.64160.1416
利用linspace()指令生成一维数组
y1=linspace(0,8,5)
y2=linspace(pi,0,6)
y1=
02468
y2=
3.14162.51331.88501.25660.62830
二维数组创建
d=eye(3)
a=6*ones(4,4)
c=diag(diag(a))
d=
100
010
001
a=
6666
6666
6666
6666
c=
6000
0600
0060
0006
数值数组的寻访
一维数组的寻访
x=[2.00001.03651.78953.0000+20000i]
x1=x(3)
x2=x([124])
x3=x(2:
end)
x4=x(4:
-1:
1)
x=
1.0e+004*
0.00020.00010.00020.0003+2.0000i
x1=
1.7895
x2=
1.0e+004*
0.00020.00010.0003+2.0000i
x3=
1.0e+004*
0.00010.00020.0003+2.0000i
x4=
1.0e+004*
0.0003+2.0000i0.00020.00010.0002
二维数组的寻访
A=rand(3,5)
A1=A(1,:
)
A2=A(1:
2,2:
5)
A3=A([1,3],[2,5])
A=
0.95010.48600.45650.44470.9218
0.23110.89130.01850.61540.7382
0.60680.76210.82140.79190.1763
A1=
0.95010.48600.45650.44470.9218
A2=
0.48600.45650.44470.9218
0.89130.01850.61540.7382
A3=
0.48600.9218
0.76210.1763
数组与矩阵运算比较
A=[-1,-2,-3;-4,-5,-6;-7,-8,-9]
B=[1,2,3;4,5,6;7,8,9]
C=A+B*i
CN=C.'
CM=C'
ABN=A.*B
ABM=A*B
A=
-1-2-3
-4-5-6
-7-8-9
B=
123
456
789
C=
-1.0000+1.0000i-2.0000+2.0000i-3.0000+3.0000i
-4.0000+4.0000i-5.0000+5.0000i-6.0000+6.0000i
-7.0000+7.0000i-8.0000+8.0000i-9.0000+9.0000i
CN=
-1.0000+1.0000i-4.0000+4.0000i-7.0000+7.0000i
-2.0000+2.0000i-5.0000+5.0000i-8.0000+8.0000i
-3.0000+3.0000i-6.0000+6.0000i-9.0000+9.0000i
CM=
-1.0000-1.0000i-4.0000-4.0000i-7.0000-7.0000i
-2.0000-2.0000i-5.0000-5.0000i-8.0000-8.0000i
-3.0000-3.0000i-6.0000-6.0000i-9.0000-9.0000i
ABN=
-1-4-9
-16-25-36
-49-64-81
ABM=
-30-36-42
-66-81-96
-102-126-150
数组的关系运算和逻辑运算
数组的关系运算
A=1:
9,B=10-A,r0=(A<4),r1=(A==B),r2=(A>B)
A=
123456789
B=
987654321
r0=
111000000
r1=
000010000
r2=
000001111
数组的逻辑运算
A=[0,2,3,4;1,3,5,0];B=[1,0,5,3;1,5,0,5]
C=A&B
D=A|B
E=~A
B=
1053
1505
C=
0011
1100
D=
1111
1111
E=
1000
0001
字符串数组创建
a='sumsption',b='学院'
a=
sumsption
b=
学院
size(a)
ans=
19
b=a(end:
-1:
1)
b=
noitpsmus
A='机械电子工程',size(A)
A=
机械电子工程
ans=
16
B='''机械电子工程''',size(B)
B=
'机械电子工程'
ans=
18
B=[A,'研究院']
B=
机械电子工程研究院
串转换函数
a=rand(2,2),b='example'
c=abs(b)
d=char(c)
e=num2str(a)
a=
0.40570.9169
0.93550.4103
b=
example
c=
10112097109112108101
d=
example
e=
0.405710.9169
0.935470.41027
|构架数组
student_rec.number=1;
student_rec.name='AlanShearer';
student_rec.height=180;
student_rec.test=[100,80,75;77,60,92;67,28,90;100,89,78];
student_rec
student_rec.test
student_rec=
number:
1
name:
'AlanShearer'
height:
180
test:
[4x3double]
ans=
1008075
776092
672890
1008978
t=(0:
pi/50:
2*pi)';
k=0.4:
0.1:
1;
X=cos(t)*k;
plot(X)
t=(0:
pi/50:
2*pi)';
k=0.4:
0.1:
1;
X=cos(t)*k;
plot(t,X)
t=(0:
pi/100:
pi)';
y1=sin(t)*[1,-1];
y2=sin(t).*sin(9*t);
plot(t,y1,'m:
',t,y2,'g')
t=(0:
pi/100:
pi)';
y2=sin(t).*sin(9*t);
t1=pi*(0:
9)/9;
y3=sin(t1).*sin(9*t1);
plot(t,y2,'b',t1,y3,'bp')
t=0:
0.01:
pi;
B=[1,3]';w=[2,5]';
y=sin(w*t).*exp(-B*t);
plot(t,y(1,:
),'-.',t,y(2,:
))
legend('w=2,B=1,','w=5,B=3')
xlabel('t'),ylabel('y')
title('y=sin(wt)*exp(-Bt)')
t=0:
0.02:
3;
xi=0.5;wn=5;
sxi=sqrt(1-xi^2);
sita=atan(sxi/xi);
wd=wn*sxi;
y1=1-exp(-xi*wn*t).*sin(wd*t+sita)/sxi;
y2=wn*exp(-xi*wn*t).*sin(wd*t)/sxi;
plotyy(t,y1,t,y2)
t=(pi*(0:
1000)/1000)';
y1=sin(t);y2=sin(10*t);y12=sin(t).*sin(10*t);
subplot(2,2,1),plot(t,y1);axis([0,pi,-1,1])
subplot(2,2,2),plot(t,y2);axis([0,pi,-1,1])
subplot('position',[0.2,0.05,0.6,0.45])
plot(t,y12,'b-',t,[y1,-y1],'r:
');axis([0,pi,-1,1])
ezplot('x^2+x*y+y^2-10')
axis([-4,4,-4,4])
x=-4:
4;y=x;
[X,Y]=meshgrid(x,y);
Z=X.^2+Y.^2;
subplot(1,3,1),surf(X,Y,Z);
subplot(1,3,2),mesh(X,Y,Z);
subplot(1,3,3),plot3(x,y,x.^2+y.^2);boxon
clf
[x,y]=meshgrid(-3:
0.1:
3,-2:
0.1:
2);
z=(x.^2+2*x).*exp(-x.^2-y.^2-x.*y);
subplot(1,2,1),mesh(x,y,z),
axis([-3,3,-2,2,-0.5,1.0])
title('透视')
hiddenoff
subplot(1,2,2),mesh(x,y,z)
title('消隐')
hiddenon
axis([-3,3,-2,2,-0.5,1.0])
t=(0:
0.02:
2)*pi;
x=sin(t);y=cos(t);z=cos(2*t);
subplot(1,2,1),plot3(x,y,z,'b-',x,y,z,'bd')
view([-82,58])
title('视点控制'),boxon
subplot(1,2,2),plot(x,y,z,'b-',x,y,z,'bd')
boxon,title('无视点控制')
?
?
?
Errorusingplot
Stringargumentisanunknownoption.
subplot(1,3,1),surf(peaks),alpha(0)
title('完全透明')
colormap(summer);
subplot(1,3,2),surf(peaks),alpha(0.5)
title('半透明')
colormap(summer)
subplot(1,3,3),surf(peaks),alpha
(1)
title('完全不透明')
colormap(summer)
clf
[x,y]=meshgrid(-3:
0.1:
3,-2:
0.1:
2);
z=(x.^2-1.4*x).*exp(-x.^2-y.^2+x.*y);
figure
(1)
surf(x,y,z),axis([-3,3,-2,2,-0.5,1.0])
title('shadingflat'),shadingflat
figure
(2)
surf(x,y,z),axis([-3,3,-2,2,-0.5,1.0])
title('shadinginterp'),shadinginterp
figure(3)
surf(x,y,z),axis([-3,3,-2,2,-0.5,1.0])
title('shadingfacted'),shadingfacted
?
?
?
Errorusingshading(line106)
Shadingmethodsareflat,faceted,andinterp.
clf
ezsurf('x.^2+y.^2',[-10,10],100)
shadingflat
clf;
t=[0:
0.1:
18]';
forx=0.2:
0.2:
0.8
b=sqrt([1-x^2]);
z=atan(b/x);
y1=-t*x;y2=t*b+z;
y=1-exp(y1).*sin(y2)/b;
plot(x,y),holdon
end
a
(1)=1;a
(2)=1;i=2;
whilea(i)<=10000
a(i+1)=a(i-1)+a(i);
i=i+1;
end;
i,a(i)
i=
21
ans=
10946
t=[0:
0.1:
18]';
forx=0.2:
0.2:
0.8
b=sqrt([1-x^2]);z=atan(b/x);
y1=-t*x;y2=t*b+z;y=1-exp(y1).*sin(y2)/b;
switchround(10*x)
case2
plot(t,y,'r-'),holdon
case4
plot(t,y,'b*'),hold
case6
plot(t,y,'k.'),holdon
otherwise
plot(t,y,'g+'),holdon
end
end
xlabel('t(秒)'),ylabel('y'),title('二阶系统阶跃响应')
legend('{\xi}=0.2','{\xi}=0.4','{\xi}=0.6','{\xi}=0.8')
Currentplotreleased
Warning:
Ignoringextralegendentries.
{_>Inlegendat291}_
clear,N=4;
A=magic(3);
try
A_N=A(N,:
)
catch
A_end=A(end,:
)
end
lasterr
A_end=
492
ans=
Indexexceedsmatrixdimensions.
figure
(1)
sa=circle(10,'y')
figure
(2)
circle(10,'k')
?
?
?
Undefinedfunction'circle'forinputargumentsoftype'double'.
functiony1=mainfun(a,s)
t=(0:
a)/a*2*pi;
y1=subfun(4,s);
functiony2=subfun(a,s)
t=(0:
a)/a*2*pi;
ss='a*exp(i*t)';
switchs
case{'base','caller'}
y2=evalin(s,ss);
case'self'
y2=eval(ss);
end
clear
a=30;t=(0:
a)/a*2*pi;
sss={'base','caller','self'};
fork=1:
3
y0=mainfun(8,sss{k});
subplot(1,3,k)
plot(real(y0),imag(y0),'r','lineWidth',3)
axissquare
end
?
?
?
Undefinedfunction'mainfun'forinputargumentsoftype'double'.
t=pi;
evel('theta=t/2,y1=sin(theta)');
?
?
?
Undefinedfunction'evel'forinputargumentsoftype'char'.