MATLAB课后习题.docx
《MATLAB课后习题.docx》由会员分享,可在线阅读,更多相关《MATLAB课后习题.docx(33页珍藏版)》请在冰豆网上搜索。
MATLAB课后习题
5、利用rand函数产生(0,1)间的均匀分布的10*10随机矩阵A,然后统计A中大于等于的元素的个数。
解:
A=rand(10);
B=A>=;
C=sum(B);
count=sum(C)
运行结果(每次运行结果是不同的,仅作参考):
count=32
6、利用randn函数产生均值为0,方差为1的10*10随机矩阵A,然后统计A中大于且小于的元素的个数。
解:
A=randn(10);
B=(A<&(A>;
C=sum(sum(B))
运行结果(每次运行结果是不同的,仅作参考):
C=48
1、解:
ifand(a<1,b<=
语句1;
elseifand(a<1,b>
语句2;
elseifand(a>=1,b<=
语句3;
else
语句4;
2、有一矩阵A,找出矩阵中值等于1的元素,并将它们重新排列成列向量B。
解:
A=2*rand(4);
k=find(A<=1);
A(k)=[];%删除下标为k的元素
B=A'
运行结果(每次运行结果是不同的,仅作参考)
B=
3、在一测量矩阵A(100*3)中,存在有奇异值(假设大于100的置认为是奇异值),编程实
现删去奇异值所在的行。
解:
A=120*randn(10,3);
[i,j]=find(A>100);
A(i,:
)=[]%删去存在奇异值的行
4、在给定的100*100矩阵中,删去整行为0的行,删去整列为0的列。
解:
A=diag([1234],1)
B=any(A)
[i,j]=find(B==0)
A(:
i)=[]%删除全为0的列
B=any(A')
[i,j]=find(B==0)
A(j,:
)=[]%删除全为0的行
运行结果:
初始值:
A=
01000
00200
00030
00004
00000
操作后:
A=
1000
0200
0030
0004
1、将窗口分割成四格,分别绘制正弦、余弦、正切和余切函数曲线,并加上适当的标注。
程序为:
x=0:
pi/50:
2*pi;
k=[1265176101];
x(k)=[];%删除正切和余切的奇异点
figure
(1)
subplot(2,2,1)
plot(x,sin(x),'k--'),gridon
legend('\ity=sin(x)')
title('y=sin(x)')
xlabel('x'),ylabel('y')
subplot(2,2,2)
plot(x,cos(x),'r--'),gridon
legend('\ity=cos(x)')
title('y=con(x)')
xlabel('x'),ylabel('y')
subplot(2,2,3)
plot(x,tan(x),'k'),gridon
legend('\ity=tan(x)')
title('y=tan(x)')
xlabel('x'),ylabel('y')
subplot(2,2,4)
plot(x,cot(x),'b-'),gridon
legend('\ity=cot(x)')
title('y=cot(x)')
xlabel('x'),ylabel('y')
运行如下:
2、绘制多峰函数peaks和三角函数多条曲线。
多峰函数peaks:
[x,y]=meshgrid(-3:
:
3);
z=peaks(x,y);
x1=x(1,:
);
figure
(1)
plot(x1,z),gridon
title('二维多峰函数')
图形为:
[x,y]=meshgrid(-3:
:
3);
z=peaks(x,y);
figure
(1)
plot3(x,y,z),gridon
title('三维多峰函数')
三角函数多条曲线:
程序为:
t=-pi:
pi/20:
pi;
y1=sinh(t);%双曲正弦
y2=cosh(t);%双曲余弦
figure
(1)
subplot(2,1,1)
plot(t,y1,'r--',t,y2,'k-'),gridon
legend('\ity1=sinh(t)','\ity2=cosh(t)')
title('三角函数1')
xlabel('t'),ylabel('y')
subplot(2,1,2)
plot(t,sin(t),'k-'),gridon
holdon%保持原有图像函数
plot(t,cos(t),'r--')
legend('\ity2=cos(t)','\ity1=sin(t)')
title('三角函数2')
xlabel('t'),ylabel('y')
运行图形为:
3、将图形窗口分成两个,分别绘制以下函数在[-3,3]区间上的曲线,并利用axis调整轴刻度,使他们具有相同缩放尺度。
y1=2x+5;y2=x2-3x+1。
程序为:
x=-3:
:
3;
y1=2*x+5;
y2=x.^2-3*x+1;
figure
(1)
subplot(2,2,1)
plot(x,y1,'r-'),gridon
legend('\ity1=2*x+5')
title('y1=2x+5')
xlabel('x'),ylabel('y1')
subplot(2,2,2)
plot(x,y2,'k-'),gridon
legend('\ity2=x.^2-3*x+1')
title('y2=x^2-3x+1')
xlabel('x'),ylabel('y2')
subplot(2,2,3)
plot(x,y1,'r-'),gridon
legend('\ity1=2*x+5')
title('调整后的y1=2x+5')
axis([-33-1010])
xlabel('x'),ylabel('y1')
subplot(2,2,4)
plot(x,y2,'k-'),gridon
legend('\ity2=x.^2-3*x+1')
title('调整后的y2=x^2-3x+1')
axis([-33-1010])%调整坐标轴
xlabel('x'),ylabel('y2')
运行后的图形:
4、绘制饼图。
程序为:
x=[19033454245];
explode=[01000];
figure
(1)
subplot(2,1,1)
colormaphsv
pie(x,explode)
gtext('生活费')
gtext('资料费')
gtext('电话费')
gtext('衣服')
gtext('其它')
title('二维饼图')
subplot(2,1,2)
colormaphsv
pie3(x,explode)
title('三维饼图')
图形为:
5、画出函数z=(x-2)2+()2+sin(xy)的三维曲线和网格曲线。
程序为:
[x,y]=meshgrid(0:
:
10);%为三维绘图产生x,y数据矩阵
z=(x-2).^2+.^2;
figure
(1)
subplot(2,1,1)
mesh(x,y,z),gridon%绘制网格曲线
title('网格曲线')
subplot(2,1,2)
plot3(x,y,z),gridon
title('三维曲线')
6、画出下列函数的曲面及等高线图z=x2+y2+sin(xy)。
程序为:
[x,y]=meshgrid(0:
pi/10:
2*pi);
z=x.^2+y.^2+sin(x*y);
figure
(1)
subplot(2,1,1)
surfc(x,y,z),gridon
title('曲面和等高线')
subplot(2,1,2)
[c,h]=contour(x,y,z);
set(h,'showtext','on','textstep',get(h,'levelstep')*2);
title('等高线')
1、将图形窗口分成两个,分别绘制正割和余割曲线,并加上标注。
程序为:
x1=0:
pi\10:
2*pi;
figure
(1)
subplot(2,1,1)
plot(x,sec(x),'k-'),gridon
legend('\ity=sec(x)')
title('y=sec(x)')
xlabel('x'),ylabel('y')
subplot(2,1,2)
plot(x,csc(x),'k-'),gridon
legend('\ity=csc(x)')
title('y=csc(x)')
xlabel('x'),ylabel('y')
运行后图形为:
2、画出对数和指数曲线并加上标注。
x=:
:
10;
y1=log10(x);
y2=exp(x);
figure
(1)
subplot(2,1,1)
plot(x,y1,'k-'),gridon
legend('\ity1=log-{10}(x)')
title('y1=log-{10}(x)')
xlabel('x'),ylabel('y1')
subplot(2,1,2)
plot(x,y2,'k-'),gridon
legend('\ity2=exp(x)')
title('y2=exp(x)')
xlabel('x'),ylabel('y2')
3、设有函数y=exp(x+5)+x.^3,在半对数坐标系中绘制曲线。
程序为:
x=1:
:
10;
y=exp(x+5)+x.^3;
figure
(1)
subplot(2,1,1)
plot(x,y,'r-'),gridon
legend('\ity=exp(x+5)+x.^3')
title('平面坐标')
xlabel('x'),ylabel('y')
subplot(2,1,2)
semilogx(x,y,'k-'),gridon%半对数坐标轴
legend('\ity=exp(x+5)+x.^3')
title('半对数坐标')
xlabel('x'),ylabel('y')
4、画出各种大小和形状的球和柱体。
绘制柱体的程序为:
t=0:
pi/10:
2*pi;
figure
(1)
subplot(2,1,1)
[x,y,z]=cylinder(2+cos(t));
surf(x,y,z),axissquare
title('复杂柱面体')
subplot(2,1,2)
cylinder,axissquare
title('简单柱体')
绘制球的程序为:
figure
(1)
subplot(2,1,1)
sphere
axisequal
title('半径为1的球')
subplot(2,1,2)
[x,y,z]=sphere;
x=2*x;
y=2*y;
z=2*z;
surf(x,y,z),axissquare
title('半径为2的球')
运行后的图形:
5、绘制三维条形图:
程序为:
Y=cool(7);
figure
(1)
subplot(2,2,1),bar3(Y,'detached'),title('Detached')
subplot(2,2,2),bar3(Y,,'detached'),title('Width=')
subplot(2,2,3),bar3(Y,'grouped'),title('Grouped')
subplot(2,2,4),bar3(Y,'stacked'),title('Stacked')
运行后的图形为:
6、绘制二维条形图
程序为:
Y=round(rand(5,3)*10);
figure
(1)
subplot(2,2,1),bar(Y,'group'),title('Group')
subplot(2,2,2),bar(Y,'stack'),title('Stack')
subplot(2,2,3),barh(Y,'stack'),title('Stack')
subplot(2,2,4),bar(Y,,title('Width=')
运行后的图形:
1、编写M函数实现:
求一个数是否为素数,在编写一主程序,要求通过键盘输入一个整数,然后完成判断其是否为素数。
解:
functionprime(x)
n=fix(sqrt(x));
fori=2:
n
ifrem(x,i)==0
a='fasle'
return
elsea='true'
end
end
运行结果:
>>x=56;
>>prime(x)
a=
fasle
2、编写程序完成从表示字符的响亮中删去空格,并求出字符个数。
解:
function[nstr,n]=del(str)
nstr=[];
k=find(str~='');
nstr=str(k);
n=length(nstr);
end
运行后为:
str='drhyfghgtesdhgfds';
>>[nstr,n]=del(str)
nstr=
drhyfghgtesdhgfds
n=
17
3、编写M函数统计十进制数值中’0‘的个数,然后编写脚本文件,实现统计所有自然数1~2006中0的个数。
解:
M函数为:
functiony=geshu(x)
s=num2str(x);
n=length(s);
m=0;
ifs
(1)=='0'
disp('xiserror');
return
end
fori=2:
n
ifs(i)=='0'
m=m+1;
end
end
y=m;
脚本文件为'jiu4':
sum=0;
forx=1:
2006
y=geshu(x);
sum=sum+y;
end
disp(sum)
运行结果为:
>>jiu4
504
4、利用menu函数输入选择参数ch。
当ch=1时,产生[-10,10]之间均匀分布的随机数;当ch=2时,产生[-5,5]之间均匀分布的随机数;当ch=3时,产生[-1,1]之间均匀分布的随机数;当ch=4时,产生均值为0,方差为1的正态分布随机数。
要求使用switch函数。
解:
s=menu('ch','1','2','3','4');
n=[];
switchs
case1,n=20*rand(3)-10
case2,n=10*rand(3)-5
case3,n=2*rand(3)-1
case4,n=randn(3)
otherwisedisp('error')
end
运行后:
按下2后:
n=
5、求阵列x的平均值和标准差
解:
function[mean1,stdev]=stat2(x)
[m,n]=size(x);
ifm==1
m=n;
end
s1=sum(x);s2=sum(x.^2);
mean1=s1/m;
stdev=sqrt(s2/m-mean1.^2);
运行后:
>>x=rand(4,4)+2;
>>[mean1,stdev]=stat2(x)
mean1=
stdev=
6、测试程序执行时间
%
tic
i=0;
fort=0:
.01:
100
i=i+1;
y(i)=sin(t);
end
toc
%
tic
t=0:
.01:
100;
y=sin(t);
Toc
运行后:
Elapsedtimeisseconds.
Elapsedtimeisseconds.
1、产生menu选择输出颜色
解:
s=menu('colorselection','red','green','blue','yellow','black')
switchs
case1,scolor='red';
case2,scolor='green';
case3,scolor='blue';
case4,scolor='yellow';
case5,scolor='black';
otherwisedisp('error')
end
Scolor
2、企业发放的奖金按个人完成的利润(I)提成。
分段提成比例wei即如王某完成25万元利润时,个人可得
y=10x10%+10x5%+5x2%(万元)
据此编写程序,求企业职工的奖金。
解
functionbonus=bon(I)
n=fix(I/100000)
if(n>4)
n=4;
end
bon1=100000*;
bon2=*(200000-100000);
bon3=*(400000-200000);
switchn
case0,bonus=I*100000;
case1
bonus=bon1+*(I-100000);
case{2,3}
bonus=bon1+bon2+*(I-200000);
case4,bonus=bon1+bon2+bon3+*(I-400000);
end
运行后:
>>I=1700000;
>>bonus=bon(I)
n=
17
bonus=
32000
3、有一分数序列2/1,3/2,5/3/,8/5……求前15项和。
解:
s=1;t=2;sum=0;
x=t/s;
sum=sum+x;
fori=1:
15
z=t;t=s+t;s=z;
x=t/s;
sum=sum+x;
end
sum
运行后:
>>qiuhe
sum=
4、约瑟夫环
解:
n=input('pleaseinputn:
');
m=input('pleaseinputm:
');
b=1:
n;
i=1;c=0;s=0;
whilesifb(i)~=0
c=c+1;
ifc==m
s=s+1;a(s)=b(i);b(i)=0;
c=0;
end
end
ifi==n
i=0;
end
i=i+1;
end
a
运行后:
>>yuese
pleaseinputn:
12
pleaseinputm:
3
a=
Columns1through8
369124817
Columns9through16
211510316520
Columns17through23
11921019151
5、编写程序计算x在(-3,3)上,并画出曲线。
解:
functiony=func2(x)
n=length(x);
fori=1:
n;
if(x(i)>=-3)&&(x(i)<-1)
y(i)=[-x(i).^2-4*x(i)-3]/2;
elseif(x(i)>=-1)&&(x(i)<1)
y(i)=-x(i).^2+1;
else(x(i)>=1)&&(x(i)<3)
y(i)=[-x(i).^2+4*x(i)-3]/2;
end
end
脚本为:
x=-3:
.01:
3;
y=func2(x);
figure
(1)
plot(x,y),gridon
title('y=func2(x)')
xlabel('x'),ylabel('y')
运行后:
1、求矩阵与的逆矩阵和行列式。
解:
a=[535;374;798];
b=[242;679;836];
c1=inv(a)
c2=det(a)
d1=inv(b)
d2=det(b)
运行后:
c1=
c2=
d1=
d2=
2、解方程组
解:
A=[321;1-13;24-4];
b=[76-2];
A\b'
运行后:
ans=
2、对一组数据进行分别采用y1(t)=c1+c2exp(-t),y2(t)=d1+d2t.*exp(-t)拟合.
解:
t=[12345678910]';
y=[]';
a=[ones(size(t))exp(-t)];
C=a\y;
b=[ones(size(t))t.*exp(-t)];
D=b\y;
T=[10:
-1:
1]';
y1=[ones(size(T))exp(-T)]*C;
y2=[ones(size(T))T.*exp(-T)]*D;
plot(T,y1,'r--',T,y2,'k-',t,y,'o');
legend('\ity1(t)=c1+c2exp(-t)','\ity2(t)=d1+d2t.*exp(-t)')
title('曲线拟合')
xlabel('\itt'),ylabel('\ity')
运行后:
4、矩阵,分别对a进行特征值分解、奇异值分解、LU分解、QR分解。
解:
>>[v,d]=eig(a,b)
v=
d=
00
00
00
>>a=[912;563;827];
>>[u,s,v]=svd(a)
u=
s=
00
00
00
v=
>>[l,u]=lu(a)
l=
00
0
u=
0
00
>>[q,r]=qr(a)
q=
r=
0
00
5、求解微分方程。
解:
functiondy=funf(t,y)
dy=[5*y
(1)-5*y
(2)-6*y(3);3*y
(1)-2*y
(2)+5*y(3);2*y
(1)-y
(2)-4*y(3)];
脚本文件:
x0=[1,-4,5]';
tspan=[30,100];
[t,x]=ode45('funf',tspan,x0);
plot3(x(:
1),x(:
2),x(:
3)),gridon
title('微分方程曲线')
运行后:
微分方程组x’=10(-x+y);y’=28x-y-xz;z’=xy-8z/3,x0=[12,2,9],求微分方程在[0,30]上的解,并画出系统轨迹。
解:
脚本文件:
二维图:
三维图:
2、分别用多项式和指数函数进行拟合。
y1(t)=c1+c2t+c3t2,y2(t)=d1+d2exp(t)
解:
t=[0]';
y=[]';
B1=[ones(s