河北大学MATLAB习题答案分解.docx

上传人:b****5 文档编号:12597637 上传时间:2023-04-20 格式:DOCX 页数:47 大小:27.34KB
下载 相关 举报
河北大学MATLAB习题答案分解.docx_第1页
第1页 / 共47页
河北大学MATLAB习题答案分解.docx_第2页
第2页 / 共47页
河北大学MATLAB习题答案分解.docx_第3页
第3页 / 共47页
河北大学MATLAB习题答案分解.docx_第4页
第4页 / 共47页
河北大学MATLAB习题答案分解.docx_第5页
第5页 / 共47页
点击查看更多>>
下载资源
资源描述

河北大学MATLAB习题答案分解.docx

《河北大学MATLAB习题答案分解.docx》由会员分享,可在线阅读,更多相关《河北大学MATLAB习题答案分解.docx(47页珍藏版)》请在冰豆网上搜索。

河北大学MATLAB习题答案分解.docx

河北大学MATLAB习题答案分解

MATLAB习题参考答案

1、利用基本矩阵产生3*3和15*8的单位矩阵、全1矩阵、全0矩阵、均匀分布随机阵([-1,1]之间)、正态分布随机阵(均值为1,方差为4)。

解:

A1=eye(3);A2=ones(3);A3=zeros(3);A4=2*rand(3)-1;A5=2*randn(3)+1;

B1=eye(15,8);B2=ones(15,8);B3=zeros(15,8);B4=2*rand(15,8)-1;B5=2*randn(15,8)+1;

结果:

由于数据是随机产生的,所以在没有给出运行结果。

2、利用diag等函数产生下列矩阵:

a=[008;0-75;230]b=[204;050;708]

然后利用reshape函数将它们变换成行向量。

解:

产生a的程序:

b=diag([8-72]);

c=b+diag([53],-1);

a=fliplr(c)

产生b的程序:

s=[228];

t=[437];

v=diag(s);

p=diag(t)+fliplr(v);

b=fliplr(p)

运行结果:

a=

008

0-75

230

b=

204

050

708

利用reshape函数将它们变换成行向量:

reshape(a,1,9)

ans=

0020-73850

3、产生一均匀分布在(-5,5)之间的随机阵(50*2),要求精确到小数点后一位。

解:

A=5-round(100*rand(50,2))/10

部分数据结果:

A=

2.40004.2000

-0.10002.7000

-4.6000-3.3000

-0.5000-0.4000

3.50004.2000

4、编程实现当t∈[-π,π],间隔为1°时求解正弦和余弦值。

解:

t=(-1*pi:

1/180:

pi);

y1=sin(t)

y2=cos(t)

部分数据结果:

Columns10through18(y1)

-0.0500-0.0555-0.0611-0.0666-0.0722-0.0777-0.0832-0.0888-0.0943

5、利用rand函数产生(0,1)间的均匀分布的10*10随机矩阵A,然后统计A中大于等于0.6的元素的个数。

解:

A=rand(10);

B=A>=0.6;

C=sum(B);

count=sum(C)

运行结果(每次运行结果是不同的,仅作参考):

count=32

6、利用randn函数产生均值为0,方差为1的10*10随机矩阵A,然后统计A中大于-0.5且小于0.5的元素的个数。

解:

A=randn(10);

B=(A<0.5)&(A>-0.5);

C=sum(sum(B))

运行结果(每次运行结果是不同的,仅作参考):

C=48

 

1、解:

ifand(a<1,b<=0.5)

语句1;

elseifand(a<1,b>0.5)

语句2;

elseifand(a>=1,b<=0.5)

语句3;

else

语句4;

2、有一矩阵A,找出矩阵中值等于1的元素,并将它们重新排列成列向量B。

解:

A=2*rand(4);

k=find(A<=1);

A(k)=[];%删除下标为k的元素

B=A'

运行结果(每次运行结果是不同的,仅作参考)

B=

1.4769

1.8348

1.5310

1.1524

1.3667

1.0932

1.2889

1.2952

1.3580

3、在一测量矩阵A(100*3)中,存在有奇异值(假设大于100的置认为是奇异值),编程实

现删去奇异值所在的行。

解:

A=120*randn(10,3);

[i,j]=find(A>100);

A(i,:

)=[]%删去存在奇异值的行

运行结果(每次运行结果是不同的,仅作参考):

A=

49.5355-23.7550-73.0269

-118.435439.3214-88.4472

91.1482-28.5962-209.9855

21.2336-74.0239-9.5871

-15.818472.132222.0444

71.442911.077034.8948

 

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:

0.15:

3);

z=peaks(x,y);

x1=x(1,:

);

figure

(1)

plot(x1,z),gridon

title('二维多峰函数')

图形为:

[x,y]=meshgrid(-3:

0.15:

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:

0.1:

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+(y-1.2)2+sin(xy)的三维曲线和网格曲线。

程序为:

[x,y]=meshgrid(0:

0.5:

10);%为三维绘图产生x,y数据矩阵

z=(x-2).^2+(y-1.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=0.01:

0.1:

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:

0.01:

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,0.25,'detached'),title('Width=0.25')

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,1.5),title('Width=1.5')

运行后的图形:

 

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=

4.22740.43663.3897

3.00374.8478-0.6674

-2.14052.1568-0.2938

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=

2.52072.39222.64982.2539

 

stdev=

0.17130.18920.17250.2027

6、测试程序执行时间

%tech1.m

tic

i=0;

fort=0:

.01:

100

i=i+1;

y(i)=sin(t);

end

toc

%tech2.m

tic

t=0:

.01:

100;

y=sin(t);

Toc

运行后:

Elapsedtimeis0.015217seconds.

Elapsedtimeis0.000508seconds.

 

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

运行后:

按下red后:

s=

1

scolor=

red

2、企业发放的奖金按个人完成的利润(I)提成。

分段提成比例为

=

即如王某完成25万元利润时,个人可得

y=10x10%+10x5%+5x2%(万元)

据此编写程序,求企业职工的奖金。

解:

functionbonus=bon(I)

n=fix(I/100000)

if(n>4)

n=4;

end

bon1=100000*0.1;

bon2=0.05*(200000-100000);

bon3=0.02*(400000-200000);

switchn

case0,bonus=I*100000;

case1

bonus=bon1+0.05*(I-100000);

case{2,3}

bonus=bon1+bon2+0.02*(I-200000);

case4,bonus=bon1+bon2+bon3+0.01*(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=

26.1881

4、约瑟夫环

解:

n=input('pleaseinputn:

');

m=input('pleaseinputm:

');

b=1:

n;

i=1;c=0;s=0;

whiles

ifb(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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1