1、d),d = sym( pi*3(1/3) )vpa(abs(a-d) , vpa(abs(b-d) , vpa(abs(c-d)%习题2-5syms a11 a12 a13 a21 a22 a23 a31 a32 a33A = a11 a12 a13;a21 a22 a23;a31 a32 a33a=det(A)B=inv(A)C=subexpr(B)RS,w=subexpr(B,w%习题2-6syms ksyms a positive% fk =ak * heaviside(k)fk =ak s=symsum(fk,k,0,inf) %习题2-7clear allsyms x positi
2、vefk =2/(2*k+1)*(x-1)/(x+1)(2*k+1)s1=simple(s)%习题2-8clear all, syms ty=abs(sin(t)df=diff(y),class(df)df1=limit(df,t,0,leftdf2=subs(df,t,sym(pi/2)%习题2-9clear all, syms x;f=exp(-abs(x).*abs(sin(x);fint=int(f,x,-5*pi,1.7*pi), digits(64), vpa(fint)class(fint)%习题2-10clear all,syms x y,f=x.2+y.2, fint=(in
3、t(int(f,y,1,x.2),x,1,2), double(fint)%习题2-11clear all, syms t x; f=sin(t)/t, yx=int(f,t,0,x), ezplot(yx,0 2*pi)fint=subs(yx,x,4.5),%或yxd=int(f,t,0,4.5),fint=double(yxd)hold on, plot(4.5,fint,*r%习题2-12% clear all, syms x n; f=(sin(x)n; yn=int(f,x,0,pi/2), class(yn)clear all, syms x n; syms n positive
4、 ;% y(1/3)=?yn1=subs(yn,n,sym(1/3),vpa(yn1) %或yn=limit(yn,n,1/3),vpa(yn)%或yy=int(sin(x).(1/3),x,0,pi/2) ,vpa(yy)%习题2-23clear, syms x y SS = dsolve(Dy*y/5+x/4=0,ezplot(subs(S(1),C3,1),-2,2 -2,2,1), hold onezplot(subs(S(2),1),-2,2 -2,2,1)%解为 S = % 2(1/2)*(C3 - (5*x2)/8)(1/2)% -2(1/2)*(C3 - (5*x2)/8)(1
5、/2),1),-2,2 -2,2,1) % 用此两条指令绘圆,在 y=0处有间隙ezplot(subs(y2-(S(1)2, , 1),-2,2 -2,2,2) %用椭圆方程绘图不产生间隙colormap(0 0 1) %用ezplot(fun)绘图时,如果fun中只有一个参数,绘图的颜色是蓝色;如果fun中有两个参数,绘图的颜色是绿色,此指令设置图形颜色为蓝。grid on S1=subs(S(1),1)subs(S1,x,1.6(1/2)y=double(solve(S1)t=linspace(y(2),y(1),100)S2=subs(S(2),figureplot(t,subs(S1,
6、x,t), hold onplot(t,subs(S2,x,t)axis(-2,2 -2,2) %习题2-24x=dsolve(Dx=a*t2+b*tx(0)=2%习题2-25f,g=dsolve(Df=3*f+4*gDg=-4*f+3*gf(0)=0g(0)=1第三章%习题3-1 a=0:2*pi/9:2*pi b=linspace(0,2*pi,10) %习题3-2rand( twister,0),A=rand(3,5)I1,J1=find(A0.5)subindex_A=sub2ind(size(A),I1,J1)subindex_A=find(AI,J=ind2sub(size(A),
7、subindex_A)index_A=I J%122_2rand(,0),A=rand(3,5),B=A0.5,C=find(B)ci,cj=ind2sub(size(A),C) %此法太繁Ci,Cj=find(B)%122_5t=linspace(1,10,100) (1) y=1-exp(-0.5.*t).*cos(2.*t),plot(t,y)(2) L=length(t)for k=1:L, yy(k)=1-exp(-0.5*t(k)*cos(2*t(k), end, plot(t,yy)%122_6clear,format long, rand(,1),A=rand(3,3),B=d
8、iag(diag(A),C=A.*(B)%或C=A-B%或C=triu(A,1)+tril(A,-1)%习题3-3% s=sign(randint(1,1000,123)-.5);% n=sum(s=-1)state,123),s=sign(rand(1,1000)-.5),n=sum(s=-1)%习题3-4A=1 2;3 4B1=A.(0.5), B2=A(0.5),A1=B1.2A2=B22A1-Anorm(A1-A)A1-A2norm(A1-A2)A=sym(1 2;3 4A1=simple(B1.2)A2=simple(B22)vpa(A1-A)vpa(A1-A2)%习题3-5%(1)
9、L=length(t)L yy(k)=1-exp(-0.5*t(k)*cos(2*t(k) endfigure(1),plot(t,yy)%(2)y=1-exp(-0.5.*t).*cos(2.*t),figure(2),plot(t,y) figure(3),subplot(1,2,1),plot(t,y)subplot(1,2,2),plot(t,yy)%习题3-6clear,format long, B=diag(diag(A),C=A.*(B)%或C=A-B%或C=triu(A,1)+tril(A,-1)%习题3-7clear,x=-3*pi:pi/15:3*pi; y=x;X,Y=m
10、eshgrid(x,y); % X=ones(size(y)*x;Y=y*ones(size(x);warning off;Z=sin(X).*sin(Y)./X./Y;% (1)“非数”数目m=sum(sum(isnan(Z); %k=Z(isnan(Z);m=length(k)% (2)绘图surf(X,Y,Z); shading interp% (3)“无裂缝”图形的全部指令:y=x;X=X+(X=0)*eps;Y=Y+(Y=0)*eps;shading interp%习题3-8clearfor k=10:-1:1; A=reshape(1:10*k,k,10); Sa(k,:)=sum
11、(A,1);Sa if k=1)=A; else)=sum(A); end第四章%习题4-1load prob_data401;diff_y=diff(y)./diff(t);gradient_y=gradient(y)./gradient(t);% plot(t(1:end-1),diff_y,t,gradient_y)figure(1)subplot(1,2,1),plot(t,y,t(1:end-1),diff_y)subplot(1,2,2),plot(t,y,t,gradient_y)%上面结果不好因自变量 采样间距太小,将间距增大后结果较好N=20diff_y1=(diff(y(1
12、:N:end)./diff(t(1:end);gradient_y1=(gradient(y(1:end)./gradient(t(1:t1=t(1:end);length(t1)figure(2)subplot(1,2,1),plot(t,y,t1(1:end-1),diff_y1)subplot(1,2,2),plot(t,y,t1,gradient_y1)%习题4-2d=0.5; tt=0:d:10; t=tt+(tt=0)*eps; y=sin(t)./t; s=d*trapz(y) % 计算出积分值ss=d*(cumtrapz(y) %计算梯形法累计积分并绘积 分曲线plot(t,y
13、,t,ss,r),hold on%用find指令计算y(4.5),并绘出该点y4_5=ss(find(t=4.5)%插值法计算y(4.5),并绘出该点yi=interp1(t,ss,4.5), plot(4.5,yi,r+% yi=interp1(t,ss,4.2 4.3 4.5), plot(4.2 4.3 4.5,yi,% clf%用精度可控指令quad 即Simpson法,quadl 即Lobatto法计算y(4.5)yy=quad(sin(t)./t,0,4.5)yy=quadl(warning off % 取消警告性提示时用%此法可用,但有警告性提示,取消提示加 warning of
14、ftt=0:0.1: warning offfor i=1:101 q(i)=quad(,0,tt(i);plot(tt, q)y=quad(%符号解法,匿名函数求y(4.5)f=(x)(int(sin(t)/t,0,x),vpa(f(4.5)%符号解法syms x t y1 y2 y1i, y1=sin(t)./t, y1i=int(y1,t,0,x), y2=subs(y1i,x,4.5)hold on ,plot(4.5,y2,*m%习题4-3d=pi/20; x=0:pi; fx=exp(sin(x).3);s=d*trapz(fx) % 梯形法计算积分值%用精度可控指令quad 即S
15、impson法,quadl 即Lobatto法计算积分s1=quad(exp(sin(x).3),0,pi)s2=quadl(%符号计算解法% s3=vpa(int(exp(sin(x)3),0,pi)s3=vpa(int(s4=vpa(int(sym(),0,pi)%习题4-4exp(-abs(x).*abs(sin(x),-5*pi,1.7*pi,1e-10),-5*pi,1.7*pi)在Matlab6.5版本上下式计算结果多加了值1exp(-abs(x)*abs(sin(x),-5*pi,1.7*pi)syms x;s3=vpa(int(exp(-abs(x)*abs(sin(x),-5
16、*pi,1.7*pi)sin(x),-pi/2,pi/2)% 梯形法计算积分值d=pi/1000; x=-5*pi:1.7*pi; fx=exp(-abs(x).*abs(sin(x);s=d*trapz(fx)%习题4-5x1=-5;x2=5;%采用内联函数或字符串函数求极值yx=inline(sin(5*t).2.*exp(0.06*t.2)-1.5.*t.*cos(2*t)+1.8.*abs(t+0.5)xn0,fval=fminbnd(yx,x1,x2)%绘函数图并标出最小值点t=x1:x2; plot(t,yx(t),hold on ,plot(xn0,fval,r*%字符串函数求极
17、值xn0,fval=fminbnd(sin(5.*x).2.*exp(0.06.*x.2)-1.5.*x.*cos(2.*x)+1.8.*abs(x+0.5),-5,5)% yx=% xn0,fval=fminbnd(yx,x1,x2)下一条指令即字符串函数在无论在2010a版本还是Matlab6.5版本上不适用,因为对字符串函数只是别符号x,不识别t% xn0,fval=fminbnd(sin(5.*t).2.*exp(0.06.*t.2)-1.5.*t.*cos(2.*t)+1.8.*abs(t+0.5)下一条指令即匿名函数在Matlab6.5版本上不适用% yx=(t)(sin(5*t)
18、.2.*exp(0.06*t.2)-1.5.*t.*cos(2*t)+1.8.*abs(t+0.5) %本条指令即匿名函数在Matlab6.5版本上不适用% yx=(t)(sin(5.*x).2.*exp(0.06.*x.2)-1.5.*x.*cos(2.*x)+1.8.*abs(x+0.5) %本条指令即匿名函数在Matlab6.5版本上不适用%习题4-6tspan=0,0.5; %y0=1;0; %tt,yy=ode45(DyDt_6,tspan,y0);y0_5=yy(end,1)% figure(1)% plot(tt,yy(:,1)% xlabel(),title(x(t) % fi
19、gure(2)% plot(yy(:,1),yy(:,2) %位移),ylabel(速度函数DyDt_6另存为一个文件function ydot=DyDt_6(t,y)mu=3;ydot=y(2);mu*y(2)-2*y(1)+1;D2y-3*Dy+2*y = 1y(0) = 1Dy(0) = 0ys0_5=subs(S,0.5)%习题4-7A=magic(8)B=orth(A)rref(A)rref(B)A = 64 2 3 61 60 6 7 57 9 55 54 12 13 51 50 16 17 47 46 20 21 43 42 24 40 26 27 37 36 30 31 33
20、32 34 35 29 28 38 39 25 41 23 22 44 45 19 18 48 49 15 14 52 53 11 10 56 8 58 59 5 4 62 63 1B = -0.35355339059327 0.54006172486732 0.35355339059327 -0.35355339059327 -0.38575837490523 -0.35355339059327 -0.35355339059327 -0.23145502494314 -0.35355339059327 -0.35355339059327 0.07715167498105 0.35355339
21、059327 -0.35355339059327 -0.07715167498105 0.35355339059327 -0.35355339059327 0.23145502494314 -0.35355339059327 -0.35355339059327 0.38575837490523 -0.35355339059327 -0.35355339059327 -0.54006172486732 0.35355339059327ans = 1 0 0 1 1 0 0 1 0 1 0 3 4 -3 -4 7 0 0 1 -3 -4 4 5 -7 0 0 0 0 0 0 0 0%习题4-8A
22、= sym(gallery(5)v, lambda = eig(A)cond(A)clear all, A=gallery(5)V,D,s=condeig(A)V,D=eig(A)jordan(A) 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0%习题4-9clear all,A=magic(3)b=ones(3,1)x=Abx = 0.06666666666667 x=inv(A)*brref(A,b) 1.00000000000000 0 0 0.06666666666667 0 1.00000000000000 0 0.06666666
23、666667 0 0 1.00000000000000 0.06666666666667%习题4-10解不唯一 A=magic(4)b=ones(4,1) 1.00000000000000 0 0 1.00000000000000 0.05882352941176 0 1.00000000000000 0 3.00000000000000 0.11764705882353 0 0 1.00000000000000 -3.00000000000000 -0.05882352941176 0.05882352941176 0.11764705882353 -0.05882352941176 0 xg=null(A) xg = 0.22360679774998 0.67082039324994 -0.67082039324994 -0.22360679774998x+xg 也是方程的解 0.28243032716174 0.78846745207347 -0.72964392266170
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1