MATLAB实验报告1 1.docx
《MATLAB实验报告1 1.docx》由会员分享,可在线阅读,更多相关《MATLAB实验报告1 1.docx(7页珍藏版)》请在冰点文库上搜索。
MATLAB实验报告11
实验报告
课程名称
MATLAB语言及应用
实验机房
院系
专业
班级
学号
姓名
机器号
实验日期
任课教师
实验学时
2
实验成绩
一.实验名称:
MATLAB程序阅读、调试、运行。
二.实验目的和要求
1、阅读程序,并给程序中的语句添加注释。
2、调试、运行程序,并给出运行结果。
三.实验内容
(一)给下列程序添加注释、调试运行,并给出运行结果。
1、x=[123;456;789]
y=0:
0.1:
1
w=eye(3)
u=rand(3)
u1=rand(2,3)
q=randn(3)
q1=randn(2,3)
s=magic(3)
ss=zeros(3)
uu=ones(3)
2、A=[1234;5678;9101112];
A(2,3)=5
B=A(2,1:
3)
A=[AB']
A(:
2)=[]
A=[A;4321
A([14],:
)=[]
B=reshape(A,4,2)
3、一元二次方程ax2+bx+c=0的根。
程序如下:
a=input('a=?
');
b=input('b=?
');
c=input('c=?
');
d=b*b-4*a*c;
x=[(-b+sqrt(d))/(2*a),(-b-sqrt(d))/(2*a)];
disp(['x1=',num2str(x
(1)),',x2=',num2str(x
(2))])
4、输入一个字符,若为大写字母,则输出其对应的小写字母;若为小写字母,则输出其对应的大写字母;若为数字字符则输出其对应的数值,若为其他字符则原样输出。
c=input('请输入一个字符','s');
ifc>='A'&c<='Z'
disp(setstr(abs(c)+abs('a')-abs('A')));
elseifc>='a'&c<='z'
disp(setstr(abs(c)-abs('a')+abs('A')));
elseifc>='0'&c<='9'
disp(abs(c)-abs('0'));
else
disp(c);
end
5、某商场对顾客所购买的商品实行打折销售,标准如下(商品价格用price来表示):
price<200没有折扣
200≤price<5003%折扣
500≤price<10005%折扣
1000≤price<25008%折扣
2500≤price<500010%折扣
5000≤price14%折扣
输入所售商品的价格,求其实际销售价格。
程序如下:
price=input('请输入商品价格');
switchfix(price/100)
case{0,1}
rate=0;
case{2,3,4}
rate=3/100;
casenum2cell(5:
9)
rate=5/100;
casenum2cell(10:
24)
rate=8/100;
casenum2cell(25:
49)
rate=10/100;
otherwise
rate=14/100;
end
price=price*(1-rate)
6、从键盘输入若干个数,当输入0时结束输入,求这些数的平均值和它们之和。
程序如下:
sum=0;
cnt=0;
val=input('Enteranumber(endin0):
');
while(val~=0)
sum=sum+val;
cnt=cnt+1;
val=input('Enteranumber(endin0):
');
end
if(cnt>0)
sum
mean=sum/cnt
end
7、求[100,200]之间第一个能被21整除的整数。
程序如下:
forn=100:
200
ifrem(n,21)~=0
continue
end
break
end
N38
8、若一个数等于它的各个真因子之和,则称该数为完数,如6=1+2+3,所以6是完数。
求[1,500]之间的全部完数。
程序如下:
form=1:
500
s=0;
fork=1:
m/2
ifrem(m,k)==0
s=s+k;
end
end
ifm==s
disp(m);
end
end
(二)给下列图形程序添加注释、调试运行,并给出运行结果:
1、t=0:
pi/100:
2*pi;
y=sin(t);
subplot(1,2,1);plot(t,y)
y2=sin(t-0.25);
y3=sin(t-0.5);
subplot(1,2,2);plot(t,y,t,y2,t,y3);gridon
2、t=0:
pi/20:
2*pi;
[x,y]=meshgrid(t);
subplot(2,2,1)
plot(sin(t),cos(t))
axisequal
subplot(2,2,2)
z=sin(x)+cos(y);
plot(t,z)
axis([02*pi-22])
subplot(2,2,3)
z=sin(x).*cos(y);
plot(t,z)
axis([02*pi-11])
subplot(2,2,4)
z=(sin(x).^2)-(cos(y).^2);
plot(t,z)
axis([02*pi-11])
3、x1=(1:
100)/100*pi*2;
x2=((1:
100)-20)/100*pi*2;
y1=sin((1:
100)/100*pi*2);
y2=cos((1:
100)/100*pi*2);
x3=[x1;x2];
y3=[y1;y2];
subplot(2,2,1);plot(x1,y1,'r*')
subplot(2,2,2);plot(x2,y2,'bo'),gridon
subplot(2,2,3);plot(x3,y3)
subplot(2,2,4);plot(x1,y3*0.5),gridminor
4、[X,Y]=meshgrid(-8:
5:
8);
R=sqrt(X.^2+Y.^2)+eps;
Z=sin(R)./R;C=gradient(Z);
subplot(131);surf(Z)
subplot(132);surf(X,Y,Z);axis([-88-88-0.51])
subplot(133);surf(X,Y,Z,C);axis([-88-88-0.51])
5、x=0:
0.2:
12;
y1=Bessel(1,x);
y2=Bessel(2,x);
y3=Bessel(3,x);
figure
(1)
subplot(2,2,1)
h=plot(x,y1,x,y2,x,y3);
set(h,‘LineWidth’,2,{‘LineStyle’},{‘--’;‘:
’,‘-.’})
set(h,{‘Color’},{‘R’;‘G’;‘B’})
axis([012–0.51])
gridon
xlabel(‘Time’)
ylabel(‘Amplitude’)
legend(h,‘First’,‘Second’,‘Third’)
title(‘BesselFunctions’)
[y,ix]=min(y1);
text(x(ix),y,‘FirstMin\rightarrow’,…
‘HorizontalAlignment’,‘right’)
四.运行结果
五.实验中存在问题及解决办法
六.实验结果
五.实验中存在问题及解决办法
六.实验结果
五.实验中存在问题及解决办法
六.实验结果
五.实验中存在问题及解决办法
六.实验结果