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=[0 0 8;0 -7 5;2 3 0] b=[2 0 4;0 5 0;7 0 8] 然后利用reshape函数将它们变换成行向量。 解:
产生a的程序:
b=diag([8 -7 2]); c=b+diag([5 3],-1); a=fliplr(c)
产生b的程序:
s=[2 2 8]; t=[4 3 7]; v=diag(s);
p=diag(t)+fliplr(v); b=fliplr(p) 运行结果: a =
0 0 8 0 -7 5 2 3 0 b =
2 0 4 0 5 0 7 0 8
利用reshape函数将它们变换成行向量:reshape(a,1,9) ans =
0 0 2 0 -7 3 8 5 0
3、产生一均匀分布在(-5,5)之间的随机阵(50*2),要求精确到小数点后一位。
解:
A=5-round(100*rand(50,2))/10 部分数据结果: A =
2.4000 4.2000 -0.1000 2.7000 -4.6000 -3.3000
1
Matlab程序设计实验报告
-0.5000 -0.4000
3.5000 4.2000
4、编程实现当t∈[-π,π],间隔为1°时求解正弦和余弦值。 解:
t=(-1*pi:1/180:pi); y1=sin(t) y2=cos(t) 部分数据结果:
Columns 10 through 18(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、 解:
if and(a<1,b<=0.5) 语句1;
elseif and(a<1,b>0.5) 语句2;
elseif and(a>=1,b<=0.5) 语句3; else 语句4;
2、 有一矩阵A,找出矩阵中值等于1的元素,并将它们重新排列成列向量B。 解:
A=2*rand(4);
2
Matlab程序设计实验报告
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.4354 39.3214 -88.4472 91.1482 -28.5962 -209.9855 21.2336 -74.0239 -9.5871 -15.8184 72.1322 22.0444 71.4429 11.0770 34.8948
4、 在给定的100*100矩阵中,删去整行为0的行,删去整列为0的列。 解:
A=diag([1 2 3 4],1) B=any(A)
[i,j]=find(B==0) A(:,i)=[] %删除全为0的列 B=any(A')
[i,j]=find(B==0)
A(j,:)=[] %删除全为0的行 运行结果: 初始值:A =
0 1 0 0 0 0 0 2 0 0
3
Matlab程序设计实验报告
0 0 0 3 0 0 0 0 0 4 0 0 0 0 0
操作后:A =
1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4
1、将窗口分割成四格,分别绘制正弦、余弦、正切和余切函数曲线,并加上适当的标注。 程序为:
x=0:pi/50:2*pi;
k=[1 26 51 76 101];
x(k)=[];%删除正切和余切的奇异点 figure(1)
subplot(2,2,1)
plot(x,sin(x),'k--'),grid on legend('\\ity=sin(x)') title('y=sin(x)')
xlabel('x'), ylabel('y')
subplot(2,2,2)
plot(x,cos(x),'r--'),grid on legend('\\ity=cos(x)') title('y=con(x)')
xlabel('x'), ylabel('y')
subplot(2,2,3)
plot(x,tan(x),'k'),grid on legend('\\ity=tan(x)') title('y=tan(x)')
xlabel('x'), ylabel('y') subplot(2,2,4)
plot(x,cot(x),'b-'),grid on legend('\\ity=cot(x)') title('y=cot(x)')
xlabel('x'), ylabel('y') 运行如下:
4
Matlab程序设计实验报告
2、绘制多峰函数peaks和三角函数多条曲线。 多峰函数peaks:
[x,y]=meshgrid(-3:0.15:3); z=peaks(x,y); x1=x(1,:); figure(1)
plot(x1,z),grid on title('二维多峰函数') 图形为:
[x,y]=meshgrid(-3:0.15:3); z=peaks(x,y); figure(1)
plot3(x,y,z),grid on title('三维多峰函数')
5
Matlab程序设计实验报告
三角函数多条曲线: 程序为:
t=-pi:pi/20:pi;
y1=sinh(t); %双曲正弦 y2=cosh(t); %双曲余弦 figure(1)
subplot(2,1,1)
plot(t,y1,'r--',t,y2,'k-'),grid on
legend('\\ity1=sinh(t)','\\ity2=cosh(t)') title('三角函数1')
xlabel('t'), ylabel('y') subplot(2,1,2)
plot(t,sin(t),'k-'),grid on
hold on %保持原有图像函数 plot(t,cos(t),'r--')
legend('\\ity2=cos(t)','\\ity1=sin(t)') title('三角函数2')
xlabel('t'), ylabel('y') 运行图形为:
3、将图形窗口分成两个,分别绘制以下函数在[-3,3]区间上的曲线,并利用axis调整轴刻度,
6
Matlab程序设计实验报告
使他们具有相同缩放尺度。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-'),grid on legend('\\ity1=2*x+5') title('y1=2x+5')
xlabel('x'), ylabel('y1') subplot(2,2,2)
plot(x,y2,'k-'),grid on 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-'),grid on legend('\\ity1=2*x+5') title('调整后的y1=2x+5') axis([-3 3 -10 10])
xlabel('x'), ylabel('y1')
subplot(2,2,4)
plot(x,y2,'k-'),grid on legend('\\ity2=x.^2-3*x+1') title('调整后的y2=x^2-3x+1') axis([-3 3 -10 10]) %调整坐标轴 xlabel('x'), ylabel('y2') 运行后的图形:
7
Matlab程序设计实验报告
4、绘制饼图。 程序为:
x=[190 33 45 42 45]; explode=[0 1 0 0 0]; figure(1)
subplot(2,1,1) colormap hsv pie(x,explode) gtext('生活费') gtext('资料费') gtext('电话费') gtext('衣服') gtext('其它') title('二维饼图')
subplot(2,1,2) colormap hsv 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),grid on %绘制网格曲线 title('网格曲线')
8
Matlab程序设计实验报告
subplot(2,1,2)
plot3(x,y,z),grid on 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), grid on
title('曲面和等高线')
subplot(2,1,2)
[c,h]=contour(x,y,z);
set(h,'showtext','on','textstep',get(h,'levelstep')*2); title('等高线') 运行后的图形:
9
Matlab程序设计实验报告
1、将图形窗口分成两个,分别绘制正割和余割曲线,并加上标注。 程序为:
x1=0:pi\\10:2*pi; figure(1)
subplot(2,1,1)
plot(x,sec(x),'k-'),grid on legend('\\ity=sec(x)') title('y=sec(x)')
xlabel('x'), ylabel('y') subplot(2,1,2)
plot(x,csc(x),'k-'),grid on 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-'),grid on
legend('\\ity1=log-{10}(x)') title('y1=log-{10}(x)') xlabel('x'), ylabel('y1')
subplot(2,1,2)
plot(x,y2,'k-'),grid on legend('\\ity2=exp(x)') title('y2=exp(x)')
xlabel('x'), ylabel('y2')
运行后图形为:
10
Matlab程序设计实验报告
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-'),grid on
legend('\\ity=exp(x+5)+x.^3') title('平面坐标')
xlabel('x'), ylabel('y')
subplot(2,1,2)
semilogx(x,y,'k-'),grid on %半对数坐标轴 legend('\\ity=exp(x+5)+x.^3') title('半对数坐标')
xlabel('x'), ylabel('y') 运行后图形为:
11
Matlab程序设计实验报告
4、画出各种大小和形状的球和柱体。 绘制柱体的程序为: t=0:pi/10:2*pi; figure(1)
subplot(2,1,1)
[x,y,z]=cylinder(2+cos(t)); surf(x,y,z),axis square title('复杂柱面体') subplot(2,1,2)
cylinder, axis square title('简单柱体')
绘制球的程序为: figure(1)
subplot(2,1,1) sphere
axis equal
title('半径为1的球') subplot(2,1,2) [x,y,z]=sphere; x=2*x; y=2*y; z=2*z;
surf(x,y,z),axis square title('半径为2的球')
运行后的图形:
12
Matlab程序设计实验报告
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、绘制二维条形图 程序为:
13
Matlab程序设计实验报告
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函数实现:求一个数是否为素数,在编写一主程序,要求通过键盘输入一个整数,然后完成判断其是否为素数。
14
Matlab程序设计实验报告
解:
function prime(x) n=fix(sqrt(x)); for i=2:n
if rem(x,i)==0 a='fasle' return
else a='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='dr hy fgh gtesd hgfds'; >> [nstr,n]=del(str)
nstr =
drhyfghgtesdhgfds n =
17
3、编写M函数统计十进制数值中’0‘的个数,然后编写脚本文件,实现统计所有自然数1~2006中0的个数。 解:
M函数为:
15
Matlab程序设计实验报告
function y=geshu(x) s=num2str(x); n=length(s); m=0;
if s(1)=='0'
disp('x is error'); return end
for i=2:n
if s(i)=='0' m=m+1; end end y=m;
脚本文件为 'jiu4': sum=0;
for x=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=[]; switch s
case 1,n=20*rand(3)-10 case 2,n=10*rand(3)-5 case 3,n=2*rand(3)-1 case 4,n=randn(3)
otherwise disp('error') end 运行后:
16
Matlab程序设计实验报告
按下2后: n =
4.2274 0.4366 3.3897 3.0037 4.8478 -0.6674 -2.1405 2.1568 -0.2938 5、求阵列x的平均值和标准差 解:
function [mean1,stdev]=stat2(x) [m,n]=size(x); if m==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.5207 2.3922 2.6498 2.2539
stdev =
0.1713 0.1892 0.1725 0.2027
6、测试程序执行时间 % tech1.m tic
17
Matlab程序设计实验报告
i=0;
for t=0:.01:100 i=i+1;
y(i)=sin(t); end toc
% tech2.m tic
t=0:.01:100; y=sin(t); Toc
运行后:
Elapsed time is 0.015217 seconds. Elapsed time is 0.000508 seconds.
1、产生menu选择输出颜色 解:
s=menu('color selection','red','green','blue','yellow','black')switch s
case 1,scolor='red'; case 2,scolor='green'; case 3,scolor='blue'; case 4,scolor='yellow'; case 5,scolor='black'; otherwise disp('error') end Scolor 运行后:
18
Matlab程序设计实验报告
按下red后: s =
1
scolor = red
2、企业发放的奖金按个人完成的利润(I)提成。分段提成比例KI为KI=??10%,I?10万元??5%,10?I?20万元? ???2%,20?I?40万元????1%,I?40万元??即如王某完成25万元利润时,个人可得
y=10 x 10% + 10 x 5% + 5 x 2% (万元)
据此编写程序,求企业职工的奖金。 解:
function bonus=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); switch n
19
Matlab程序设计实验报告
case 0,bonus=I*100000; case 1
bonus=bon1+0.05*(I-100000); case {2,3}
bonus=bon1+bon2+0.02*(I-200000);
case 4,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; for i=1:15
z=t;t=s+t;s=z; x=t/s;
sum=sum+x; end sum
运行后:
>> qiuhe
sum =
26.1881
4、约瑟夫环 解:
n=input('please input n:');
20
Matlab程序设计实验报告
x(i)=x(k); x(k)=y; end
disp(['第',num2str(cnt),'趟排序后:',num2str(x)]); cnt=cnt+1; end
运行方式:
在MATLAB命令窗口输入: >> x=[2 45 6 78 21 9]; >> select_order(x)
运行结果:
初始关键字: 2 45 6 78 21 9 第1趟排序后:2 45 6 78 21 9 第2趟排序后:2 6 45 78 21 9 第3趟排序后:2 6 9 78 21 45 第4趟排序后:2 6 9 21 78 45 第5趟排序后:2 6 9 21 45 78
4、绘图工具的应用
?x?yz?xe,当x和y的取值范围均为-2到2时,用建立子窗口的方法在同一个图形窗口
22中绘制出三维线图、网线图、表面图和带渲染效果的表面图。 程序为:
[x,y]=meshgrid([-2:.2:2]); %产生\格点\矩阵 z=x.*exp(-x.^2-y.^2);
mesh(x,y,z) %网线图 subplot(2,2,1)
plot3(x,y,z) %创建子图 title('plot3 (x,y,z)') subplot(2,2,2) mesh(x,y,z)
title('mesh (x,y,z)') subplot(2,2,3)
surf(x,y,z) %三维着色表面图 title('surf (x,y,z)') subplot(2,2,4)
surf(x,y,z), shading interp %插值 title('surf (x,y,z), s hading interp')
运行后结果为:
41
Matlab程序设计实验报告
5、多项式拟合
分别采用二阶和三阶多项式进行拟合 程序为:
t=[1 2 3 4 5 6 7 8 9 10]';
y=[15.0 39.5 66.0 85.5 89.0 67.5 12.0 -86.4 -236.9 -448.4]'; a=[ones(size(t)) t t.^2]; C=a\\y;
b=[ones(size(t)) t t.^2 t.^3]; D=b\\y;
T=[1:0.25:10]';
y1=[ones(size(T)) T T.^2]*C;
y2=[ones(size(T)) T T.^2 T.^3]*D; plot(T,y1,'r--',T,y2,'k-',t,y,'o'); legend('\\ity1','\\ity2') title('多项式拟合')
xlabel('\\itt'),ylabel('\\ity')
运行结果为:
42
Matlab程序设计实验报告
43
百度搜索“70edu”或“70教育网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,70教育网,提供经典综合文库matlab7.0x课后习题答案在线全文阅读。
相关推荐: