MATLAB 程序设计与应用第二版 刘卫国 实验部分.docx
《MATLAB 程序设计与应用第二版 刘卫国 实验部分.docx》由会员分享,可在线阅读,更多相关《MATLAB 程序设计与应用第二版 刘卫国 实验部分.docx(15页珍藏版)》请在冰豆网上搜索。
![MATLAB 程序设计与应用第二版 刘卫国 实验部分.docx](https://file1.bdocx.com/fileroot1/2022-11/23/9f2fd055-7ba2-455d-a35f-568cbcfe6430/9f2fd055-7ba2-455d-a35f-568cbcfe64301.gif)
MATLAB程序设计与应用第二版刘卫国实验部分
实验一
1.
(1)
z1=2*sin(85*pi/180)/(1+exp
(2))
(2)
>>x=[2,1+2i;-0.45,5];
>>z2=(1/2)*log(x+sqrt(1+x*x))
(3)
>>a=-3.0:
0.1:
3.0;
>>z3=((exp(0.3*a)-exp(-0.3*a))/2).*sin(a+0.3)+log((0.3+a)/2)
(4)
t=0:
0.5:
2.5;
>>z4=(t.*t).*(t>=0&t<1)+(t.*t-1).*(t>=1&t<2)+(t.*t-2.*t+1).*(t>=2&t<3)
2.
(1)
>>A=[12,34,-4;34,7,87;3,65,7];
>>B=[1,3,-1;2,0,3;3,-2,7];
>>A+6*B
(2)
>>A*B
(3)
>>A^3
(4)
>>A/B
(5)
>>[A,B]
3.
(1)
>>A=[1,2,3,4,5;6,7,8,9,10;11,12,13,14,15;16,17,18,19,20;21,22,23,24,25];
>>B=[3,0,16;17,-6,9;0,23,-4;9,7,0;4,13,11];
>>C=A*B
(2)
>>D=C(3:
end,2:
end)
(3)
Namevalueclass
A<5×5double>double
B<5×3double>double
C<5×3double>double
D[520397;705557;…]double
4.
(1)
>>X=100:
999;
>>length(find(~rem(X,21)))
(2)
>>y='ahHGJjskjJjsdsaSa';
>>M=y(find(y<='z'&y>='a'))
实验三
1x=input('请输入x的值')
ifx<0&x~=-3
y=x^2+x-6;
elseifx>=0&x<5&x~=2&x~=3
y=x^2-5*x+6;
else
y=x^2-x-1;
end
y
2
(1)
x=input('请输入成绩')
ifx>=90&x<=100
disp('等级为A')
elseifx>=80&x<=89
disp('等级为B')
elseifx>=70&x<=79
disp('等级为C')
elseifx>=60&x<=69
disp('等级为D')
elseifx>=0&x<=59
disp('等级为E')
else
disp('成绩有误')
end
(2)
x=input('请输入成绩')
switch(x)
case{90,100}
disp('等级为A')
case{80,89}
disp('等级为B')
case{70,79}
disp('等级为C')
case{60,69}
disp('等级为D')
case{0,59}
disp('等级为E')
otherwise
disp('成绩错误')
end
>>
3
Num=input('输入员工的工号:
');
Time=input('该工号员工的工作时数:
');
salary=input('该员工底薪:
');
ifTime>=120
salary=salary*(1+0.15);
elseifTime<=60
salary=salary-700;
elseifTime>60&Time<120
salary=Time*84;
end
salary=salary
实验四
1:
程序:
n=input('输入n:
');
fora=1:
n;
b(a)=1/(a^2);
end
pi=sqrt(6*sum(b))
2:
.
程序:
sum=0;
forn=1:
200;
b(n)=1/(2*n-1);
sum=sum+b(n);
if(sum>=3)
break;
end
end
n-1
4.
程序:
f
(1)=1;max=0;
f
(2)=0;min=0;
f(3)=1;c=0;
zhengshu=0;
fushu=0;
zero=0;
forn=4:
100
f(n)=f(n-1)-2*f(n-2)+f(n-3);
end
form=1:
100
if(f(m)>max)
max=f(m);
end
if(f(m)min=f(m);
end
c=sum(f);
if(f>0)
zhengshu=zhengshu+1;
end
if(f(m)<0)
fushu=fushu+1;
end
if(f(m)==0)
zero=zero+1;
end
if(f(m)>0)
zhengshu=zhengshu+1;
end
if(f(m)<0)
fushu=fushu+1;
end
if(f(m)==0)
zero=zero+1;
end
disp('最大值')
max
disp('最小值')
min
disp('和')
c
disp('正数的个数')
zhengshu
disp('负数的个数')
fushu
disp('零的个数')
zero
实验五
1,函数文件:
文件名:
Yunsuan
程序:
function[z,d,s,c]=yunsuan(a,b)
z=exp(a+b*i);
d=log(a+b*i);
s=sin(a+b*i);
c=cos(a+b*i);
disp(['复数',num2str(a),'+',num2str(b),'i的指数为:
',num2str(z),',对数为:
',num2str(d),',正弦为:
',num2str(s),',余弦为:
',num2str(c)])
函数调用:
程序:
clear
x=input('请输入复数实部的值');
y=input('请输入复数虚部的值');
[x,y]=yunsuan(x,y);
2函数文件:
文件名:
zhu
程序:
function[p,q,w]=zhu(p,q,w)
A=[p*cos(w),-p,-sin(w),0;p*sin(w),0,cos(w),0;0,q,-sin(w),0;0,0,-cos(w),1];
B=[0;9.8*p;0;9.8*q];
X=A\B
函数调用:
程序:
clear
m1=input('m1=');
m2=input('m2=');
th=input('theta=');
theta=pi*th/180;
[m1,m2,theta]=zhu(m1,m2,theta);
4函数文件:
文件名:
hanshu
程序:
function[y]=hanshu(x)
y=1/((x-2).^2-0.1)+1./((x-3).^4+0.01);
实验六
1、
程序:
、
x=0:
0.02*pi:
2*pi;
y=(0.5+3*sin(x)./(1+x.*x)).*cos(x)
plot(x,y)
2、
(1)
程序:
x=(0:
0.02*pi:
2*pi)';
y1=x.*x;
y2=cos(2*x);
y3=y1.*y2;
plot(x,y1,'r-'),holdon
plot(x,y2,'b:
'),holdon
plot(x,y3,'gp'),holdoff
(2)
程序:
x=(0:
0.02*pi:
2*pi)';
y1=x.*x;
y2=cos(2*x);
y3=y1.*y2;
subplot(2,2,1)
stairs(x,y1);
plot(x,y1,'r-'),holdon
subplot(2,2,2)
stairs(x,y2);
plot(x,y2,'b:
'),holdon
subplot(2,2,3)
stairs(x,y3);
plot(x,y3,'gp'),holdoff
(3)
(1)绘制条形图
程序:
x=(0:
0.2*pi:
2*pi)';
y1=x.*x;
y2=cos(2*x);
y3=y1.*y2;
subplot(2,2,1)
stairs(x,y1);
bar(x,y1)
subplot(2,2,2)
stairs(x,y2);
bar(x,y1)
subplot(2,2,3)
stairs(x,y3);
bar(x,y3)
(2)绘制梯形图
程序:
x=(0:
0.05*pi:
2*pi)';
y1=x.*x;
y2=cos(2*x);
y3=y1.*y2;
subplot(2,2,1)
stairs(x,y1);
subplot(2,2,2)
stairs(x,y2);
subplot(2,2,3)
stairs(x,y3);
(4)杆图:
程序:
x=(0:
0.05*pi:
2*pi)';
y1=x.*x;
y2=cos(2*x);
y3=y1.*y2;
subplot(2,2,1)
stem(x,y1);
subplot(2,2,2)
stem(x,y2);
subplot(2,2,3)
stem(x,y3);
(5)填充图
程序:
x=(0:
0.05*pi:
2*pi)';
y1=x.*x;
y2=cos(2*x);
y3=y1.*y2;
subplot(2,2,1)
fill(x,y1,'r');
subplot(2,2,2)
fill(x,y2,'b');
subplot(2,2,3)
fill(x,y3,'g');
3、:
程序:
x=linspace(-5,5,1000);
y=[];
forx0=x
ifx0<=0
y=[y,(x0+sqrt(pi))./exp
(2)];
else
y=[y,0.5*log(x0+sqrt(1+x0.*x0))];
end
end
plot(x,y)
4、
程序:
a=input('请输入a的值')
b=input('请输入b的值')
n=input('请输入n的值')
theta=0:
0.01:
2*pi;
rho=a.*sin(b+n.*theta)
polar(theta,rho)
5、
程序:
x=-linspace(-5,5,21);
y=linspace(0,10,31);
[x,y]=meshgrid(x,y)
z=cos(x).*cos(y).*exp(-0.25*sqrt(x.^2+y.^2))
subplot(1,2,1);
title('surf(x,y,z)');
surf(x,y,z);
subplot(1,2,2);
title('surfc(x,y,z)');
surfc(x,y,z);
实验八
1、
程序:
x=rand(1,30000);
EX=mean(x)%返回向量x的算术平均值
S=std(x)%返回向量x的标准方差
MAX=max(x)%返回向量x的最大元素
MIN=min(x)%返回向量x的最小元素
p=sum(x>0.5)/30000%满足条件的百分比
2、
程序:
p=100*rand(100,5);
[MAXOBJECT,U]=max(p)%求每门课的最高分,U记录最大元素的行号
[MAXOBJECT,V]=min(p)%求每门课的最低分,V记录最小元素的行号
EX=mean(p)%每门课的平均分
S=std(p)%每门课的标准方差
SUM_1=sum(p,2);%求每名学生的总成绩
[MAXSUM,W]=max(SUM)%求五门课总分的最高分,W记录最高分学生的序号
[MINSUM,Y]=min(SUM)%求五门课总分的最低分,Y记录最低分学生的序号
[zcj,xsxh]=sort(SUM_1);
zcj(end:
-1:
1);
xsxh(end:
-1:
1);
实验九
2、程序:
I1=quad('sqrt(cos(t.^2)+4*sin(2*t).^2+1)',0,2*pi)
I2=quad('(log(1+x))./(1+x.*x)',0,1)
3、
程序:
A=[6,5,-2,5;9,-1,4,-1;3,4,2,-2;3,-9,0,2]
b=[-4;13;1;11]
x=inv(A)*b
实验十
1、
程序:
x=sym('6');
y=sym('5');
z=(x+1)/(sqrt(3+x)-sqrt(y))
2、分解因式
(1)
程序:
symsxy;
A=x^4-y^4;
factor(A)
(2)
程序:
factor(sym('5135'))
3、化简表达式
(1)
程序:
symsbeta1beta2
y=sin(beta1)*cos(beta2)-cos(beta1)*sin(beta2)
simple(y)
(2)
程序:
symsx
y=(4*x^2+8*x+3)/(2*x+1)
simple(y)
5、用符号方法求下列极限或导数
(1)
程序:
symsx
f=(x*(exp(sin(x))+1)-2*(exp(tan(x))-1))/(sin(x))
limit(f)
(2)
程序:
symsx
y=(sqrt(pi)-sqrt(acos(x)))/(sqrt(x+1));
limit(f,x,-1,'right')
(3)
程序:
symsx
y=(1-cos(2*x))/x;
y1=diff(y)
y2=diff(y,x,2)
6、用符号方法求下列积分
(1)
程序:
symsx
f=1/(1+x^4+x^8)
int(f)
(2)
程序:
symsx
f=1/(((asin(x))^2)*sqrt(1-x^2))
int(f)
(3)
程序:
symsx
f=(x^2+1)/(x^4+1)
int(f,x,0,inf)
(4)
程序:
symsx
f=exp(x)*(1+exp(x))^2
y=int(f,x,0,log
(2))
double(y)