Matlab程序设计与应用第二版刘卫国课后实验答案Word格式.docx
《Matlab程序设计与应用第二版刘卫国课后实验答案Word格式.docx》由会员分享,可在线阅读,更多相关《Matlab程序设计与应用第二版刘卫国课后实验答案Word格式.docx(10页珍藏版)》请在冰点文库上搜索。
H=hilb(5)
A-B+I='
P=pascal(5)disp(A-B+eye(3));
Hh=det(H)
A*B='
Hp=det(P)
disp(A*B);
Th=cond(H)disp('
A.*B='
Tp=cond(P)disp(A.*B);
a=abs(Th-1);
disp('
A^3='
b=abs(Tp-1);
disp(A^3);
ifa>
b
A.^3='
帕萨卡矩阵P性能更好'
disp(A.^3);
elseifa<
bdisp('
A/B='
希尔伯特矩阵H性能更好'
disp(A/B);
else
B\A='
两个矩阵性能相同'
disp(B\A);
end
[A,B]='
T3:
disp([A,B]);
a=1:
25;
[A([1,3],:
B^2]='
A=reshape(a,5,5)disp([A([1,3],:
B^2]);
行列式的值:
disp(det(A));
z=1:
矩阵的秩:
A=reshape(z,5,5)'
;
disp(rank(A));
B=[3,0,16;
17,-6,9;
0,23,-4;
9,7,0;
4,13,11];
矩阵的迹:
1
disp(trace(A));
矩阵的范数:
if(s>
=90&
s<
=100);
disp(norm(A));
T4:
elseif(s>
=80&
90);
A=[-29,6,18;
20,5,12;
-8,8,5]disp('
B'
[V,D]=eig(A)elseif(s>
=70&
80);
T5:
C'
A=[1/2,1/3,1/4;
1/3,1/4,1/5;
1/4,1/5,1/6]elseif(s>
=60&
70);
B=[0.95,0.67,0.52]'
D'
X1=A\Belse
B(3)=0.53disp('
E'
X2=A\Bend
系数矩阵A的条件数:
T2-switch:
disp(cond(A));
s=input('
pleaseenterthescore:
T6:
while(s<
0||s>
100)a=1:
thescoreisnotreasonable'
A=reshape(a,5,5)s=input('
B1=sqrtm(A)'
B1=sqrtm(A)switchfix(s/10)disp('
B2=sqrt(A)'
case{9,10}
B2=sqrt(A)disp('
B1*B1'
case{8}
B1*B1disp('
B2.*B2'
case{7}
B2.*B2disp('
实验三:
case{6}
x=-5.0:
2:
5.0case{0,1,2,3,4,5}fori=1:
length(x);
if(x(i)<
0)&
(x(i)~=-3)end
y(i)=x(i)^2+x(i)-6;
elseift=input('
请输入工时time='
(x(i)>
=0)&
(x(i)<
5)&
(x(i)~=2)&
(x(i)~=3);
ift>
120
y(i)=x(i)^2-5*x(i)+6;
w=120*84+(t-120)*1.15*84;
elseelseift<
60
y(i)=x(i)^2-x(i)-1;
w=t*84-700;
endelse
endw=84*t;
yend
T2-if:
应发工资为:
disp(w);
while(s<
100)T4:
a=10+floor(rand
(1)*89)
s=input('
b=10+floor(rand
(1)*89)
2
请输入+或-或*或/'
fori=1:
n;
while(s~='
+'
s~='
-'
*'
/'
)y=y+1/(2*i-1);
ify>
=3disp('
输入的符号错误,请重新输入'
最大n值为:
endi-1
switchsdisp('
对应的y值为:
case{'
}y-1/(2*i-1)
c=a+b;
break;
}end
endc=a-b;
T3:
case{'
}
c=a*b;
formatlong
a=input('
请输入a:
b=input('
请输入b:
c=a/b;
x
(1)=a/(1.0+b);
i=1;
x
(2)=a/(x(i)+b);
endwhile(abs(x(i+1)-x(i))>
0.00001&
i<
500)
i=i+1;
c
x(i+1)=a/(x(i)+b);
A=rand(5,6)end
n=input('
请输入n:
r1=(-b+sqrt(b^2+4*a))/2;
r2=(-b-sqrt(b^2+4*a))/2;
while(n<
1)
disp(['
x'
num2str(i+1),'
num2str(x(i+1),8)]);
输入的n有误,请重新输入'
disp(['
num2str(i),'
num2str(x(i),8)]);
enddisp(['
r1='
num2str(r1,8),'
r2='
num2str(r2,8)])
ifn>
5;
B=A(5,:
)formatshort
elseT4:
B=A(n,:
)f
(1)=1;
f
(2)=0;
f(3)=1;
i=4;
Endwhile(i<
=100)实验四:
f(i)=f(i-1)-2*f(i-2)+f(i-3);
i=i+1;
m=0;
fmax=max(f)
fori=1:
fmin=min(f)
m=m+1/i^2;
fsum=sum(f)
endfp=length(find(f>
0))pii=sqrt(6*m)fn=length(find(f<
0))T1-2:
f0=length(find(f==0))n=input('
m=1./(1:
n).^2;
j=0;
s=0;
s=sum(m);
fori=3:
50
pii=sqrt(6*s)m=i*(i-1)-1;
ifisprime(m)==1n=1000;
y=0;
m
3
s=s+m;
s(n)=10*rem(m(t),10)+fix(m(t)/10);
%
j=j+1;
挑选出的素数进行位置交换
ifisprime(s(n))==1%再挑选出交换end
end绝对素数
亲密数的对数:
num2str(j)]);
r(k)=m(t);
所有亲密素数之和:
num2str(s)]);
k=k+1;
实验五:
fn1:
n=n+1;
functionf=fn1(n)end
f=n+10*log(n.^2+5);
所有两位绝对素数:
num2str(r)])fn2:
functionf=fn2(n)y1=fn1(40)/(fn1(30)+fn1(20))%函数调用f=0;
y2=fn2(40)/(fn2(30)+fn2(20))fori=1:
n实验六:
f=f+i*(i+1);
T1:
endx=linspace(0,2*pi,101);
fx:
y=(0.5+3*sin(x)./(1+x.^2)).*cos(x);
functionf=fx(x)plot(x,y);
f=1./((x-2).^2+0.1)+1./((x-3).^4+0.01);
x=linspace(0,2*pi,100);
x=input('
请输入一个复数:
y1=x.^2;
fe=exp(x)y2=cos(2*x);
flg=log(x)y3=y1.*y2;
fsin=sin(x)subplot(2,2,1);
fcos=cos(x)plot(x,y1,'
r:
x,y2,'
y-'
x,y3,'
b-.'
subplot(2,2,2);
m1=input('
请输入m1:
plot(x,y1,'
m2=input('
请输入m2:
subplot(2,2,3);
t=input('
请输入theta:
plot(x,y2,'
A=[m1*cos(t),-m1,-sin(t),0;
m1*sin(t),0,...subplot(2,2,4);
cos(t),0;
0,m2,-sin(t),0;
0,0,-cos(t),1]plot(x,y3,'
B=[0,m1*9.8,0,m2*9.8]'
T2-3:
[a1,a2,N1,N2]='
x=linspace(0,2*pi,10);
disp([A\B]'
y2=cos(2*x);
j=1;
n=1;
k=1;
y3=y1.*y2;
fori=10:
99%挑选出10~99中所subplot(3,4,1);
有的素数bar(x,y1,'
r'
axis([0,7,0,40]);
ifisprime(i)==1subplot(3,4,2);
m(j)=i;
stairs(x,y1,'
y'
subplot(3,4,3);
endstem(x,y1,'
b'
endsubplot(3,4,4);
fort=1:
length(m);
fill(x,y1,'
g'
4
subplot(3,4,5);
theta=0:
0.001:
2*pi;
bar(x,y2,'
axis([-1,7,-1,1]);
y=a.*sin(b+n.*theta);
polar(theta,y);
subplot(3,4,6);
stairs(x,y2,'
subplot(3,4,7);
x=linspace(-5,5,21);
stem(x,y2,'
y=linspace(0,10,31);
subplot(3,4,8);
[x,y]=meshgrid(x,y);
fill(x,y2,'
z=cos(x).*cos(y).*exp(-sqrt(x.^2+y.^2)/4);
subplot(3,4,9);
subplot(1,2,1);
bar(x,y3,'
axis([0,7,-30,50]);
surf(x,y,z);
subplot(3,4,10);
subplot(1,2,2);
stairs(x,y3,'
contour3(x,y,z);
subplot(3,4,11);
T6:
stem(x,y3,'
s=0:
pi/2;
subplot(3,4,12);
t=0:
3*pi/2;
fill(x,y3,'
[s,t]=meshgrid(s,t);
x=cos(s).*cos(t);
x=linspace(-5,5,100);
y=cos(s).*sin(t);
y=[];
z=sin(s);
forx0=x;
ifx0<
=0;
shadinginterp;
y=[y,(x0+sqrt(pi))/exp
(2)];
实验八:
elseT1:
y=[y,1/2*log(x0+sqrt(1+x0^2))];
A=rand(1,30000);
enddisp('
均值为:
endmean(A)
subplot(1,2,1);
标准方差为:
plot(x,y);
std(A)
最大元素为:
max(A)
最小元素为:
length(x)min(A)
ifx(i)<
=0k=find(A>
0.5);
y(i)=(x(i)+sqrt(pi))/exp
(2);
b=length(k);
elsedisp('
大于0.5的随机数个数占总数的百分
y(i)=1/2*log(x(i)+sqrt(1+x(i)^2));
比:
endsprintf('
%f%%'
b/300)endT2:
subplot(1,2,2);
P=45+50*rand(100,5)plot(x,y);
最高分及序号:
T4:
[Y,U]=max(P,[],1)a=input('
最低分及序号:
b=input('
[Y1,U1]=min(P,[],1)n=input('
每门课平均分:
5
mean(P,1)i=i+1;
每门课的标准方差:
x=x+0.01;
endstd(P,0,1)
s=sum(P,2);
dx=diff(g)/0.01;
总分最高分:
dx
(1)
[Y2,U2]=max(s,[],1)dx(101)
总分最低分:
dx(length(g)-1)[Y3,U3]=min(s,[],1)T2:
[zcj,xsxh]=sort(s,1,'
descend'
)%descend降f1=inline('
sqrt(cos(t.^2)+4*sin(2*t).^2+1)'
)
序f2=inline('
log(1+x)./(1+x.^2)'
I1=quad(f1,0,2*pi)h=6:
18;
I2=quad(f2,0,2*pi)t1=[18.0,20.0,22.0,25.0,30.0,28.0,24.0];
t2=[15.0,19.0,24.0,28.0,34.0,32.0,30.0];
A=[6,5,-2,5;
9,-1,4,-1;
3,4,2,-2;
3,-9,0,2];
t=6.5:
17.5;
B=[-4,13,1,11]'
T1=interp1(h,t1,t,'
spline'
)x1=A\B
T2=interp1(h,t2,t,'
)[L,U]=lu(A);
x2=U\(L\B)
x=1:
10:
101;
[Q,R]=qr(A);
y=log10(x);
x3=R\(Q\B)
p=polyfit(x,y,5)fun:
x1=1:
0.01:
functionF=fun(X);
y1=log10(x1);
x=X
(1);
y2=polyval(p,x1);
y=X
(2);
plot(x1,y1,'
r-'
x1,y2,'
z=X(3);
T5:
F
(1)=sin(x)+y^2+log(z)-7;
P1=[1,2,4,0,5];
F
(2)=3*x+2^y-z^3+1;
P2=[0,0,0,1,2];
F(3)=x+y+z-5;
P3=[0,0,1,2,3];
P4=conv(P2,P3);
f=inline('
3*x+sin(x)-exp(x)'
sp1=length(P1);
fzero(f,1.5)sp2=length(P4);
X=fsolve('
fun'
[111]'
optimset('
Display'
off'
))
P=[zeros(1,sp2-sp1),P1]+P4实验十:
x=roots(P)T1:
A=[-1,1.2,-1.4;
0.75,2,3.5;
0,5,2.5];
x=sym('
6'
Ax=polyval(P,A)y=sym('
5'
AA=polyvalm(P,A)z=(x+1)/(sqrt(3+x)-sqrt(y))实验九:
symsxy;
f=inline('
det([xx^2x^3;
12*x3*x^2;
02t=sym('
5135'
6*x])'
factor(x^4-y^4)i=1;
x=1;
factor(t)
whilex<
=3.01T3:
g(i)=f(x);
symsbeta1beta2x;
6
simplify(sin(beta1)*cos(beta2)-cos(beta1)*sinsymsnx;
(beta2))S1=symsum(1/(2*n-1),n,1,10)
S2=symsum(n^2*x^(n-1),n,1,inf)simplify((4*x^2+8*x+3)/(2*x+1))
S3=symsum(n^2/5^n,n,1,inf)symsabcdefghk;
p1=[0,1,0;
1,0,0;
0,0,1];
p2=[1,0,0;
0,1,0;
1,0,1];
f=log(x);
A=[a,b,c;
d,e,f;
g,h,k];
taylor(f,x,6,1)
B=p1*p2*AT3:
B1=inv(B)x1=solve('
log(1+x)-5/(1+sin(x))=2'
)B1*B;
x2=solve('
x^2+9*sqrt(x+1)-1=0'
)tril(B)x3=solve('
3*x*exp(x)+5*sin(x)-78.5=0'
det(B)[x4,y4]=solve('
sqrt(x^2+y^2)-100=0'
3*x+5*y-
8=0'
symsxtayT4:
f1=(x*(exp(sin(x))+1)-2*(exp(tan(x))-1))/sin(x)dsolve('
D2y+4*Dy+29*y=0'
y(0)=0'
Dy(0)=15'
^3;
'
limit(f1,x,0)%
(1)T5:
f2=(sqrt(pi)-sqrt(acos(x)))/sqrt(x+1);
[x,y,z]=dsolve('
Dx=2*x-3*y+3*z'
Dy=4*x-5*y+
limit(f2,x,-1,'
right'
)%
(2)3*z'
Dz=4*x-4*y+2*z'
t'
)f3=(1-cos(2*x))/x;
diff(f3,x,1)%(3)
diff(f3,x,2)
A=[a^x,t^3;
t*cos(x),log(x)];
diff(A,x,1)&
(4)
diff(A,t,2)
diff(diff(A,x,1),t,1)
f5=(x^2-2*x)*exp(-x^2-y^2-x*y);
yx=-diff(f5,x,1)/diff(f5,y,1)%(5)
fxy=diff(diff(f5,x,1),y,1)
x=sym('
0'
y=sym('
1'
eval(fxy)
f1=1/(1+x^4+x^8);
int(f1,x)
f2=1/(asin(x))^2/sqrt(1-x^2);
int(f2,x)
f3=(x^2+1)/(x^4+1);
int(f3,x,0,inf)
f4=exp(x)*(1+exp(x))^2;
int(f4,x,0,log
(2))
实验十一:
7