Matlab代码1运行结果
- 格式:doc
- 大小:46.50 KB
- 文档页数:2
MATLAB实验代码及运行结果第一部分MATLAB语言编程、科学绘图与基本数学问题求解第二题>> A=[1 2 3 4;4 3 2 1;2 3 4 1;3 2 4 1]A =1 2 3 44 3 2 12 3 4 13 24 1>>B=[1+4j,2+3j,3+2j,4+1j;4+1j,3+2j,2+3j,1+4j;2+3j,3+2j,4+1j,1+4j;3+2j,2+3j,4+1j,1+4j]B =1.0000 + 4.0000i2.0000 +3.0000i 3.0000 + 2.0000i4.0000 + 1.0000i4.0000 + 1.0000i 3.0000 + 2.0000i 2.0000 + 3.0000i 1.0000 + 4.0000i2.0000 +3.0000i 3.0000 + 2.0000i4.0000 + 1.0000i 1.0000 + 4.0000i3.0000 + 2.0000i 2.0000 + 3.0000i4.0000 + 1.0000i 1.0000 + 4.0000i >> A(5,6)=5A =1 2 3 4 0 04 3 2 1 0 02 3 4 1 0 03 24 1 0 00 0 0 0 0 5第三题>> A=magic(8);A =64 2 3 61 60 6 7 579 55 54 12 13 51 50 1617 47 46 20 21 43 42 2440 26 27 37 36 30 31 3332 34 35 29 28 38 39 2541 23 22 44 45 19 18 4849 15 14 52 53 11 10 568 58 59 5 4 62 63 1 B=A(2:2:end,:)B =9 55 54 12 13 51 50 1640 26 27 37 36 30 31 3341 23 22 44 45 19 18 488 58 59 5 4 62 63 1 第四题>> format long;sum(2.^[0:63])ans =1.844674407370955e+19>> sum(sym(2).^[0:63])ans =18446744073709551615第五题(1)>> x=[-1:0.001:1];y=sin(1./x);plot(x,y)(2)>>t=[-pi:0.005:pi];y=sin(tan(t))-tan(sin(t));plot(t,y)第六题[x,y]=meshgrid(-2:0.1:2);z=1./(sqrt((1-x).^2+y.^2))+1./(sqrt((1+x).^2+y.^2));subplot(2 24),surf(x,y,z),shadingflat;subplot(221),surf(x,y,z),view(0,90);subplot(222),surf(x,y,z),view(90,0);subplot(22 3),surf(x,y,z),view(0,0)>>Warning: Divide by zero.Warning: Divide by zero.xx=[-2:0.1:-1.2,-1.1:0.02:-0.9,-0.8:0.1:0.8,0.9:0.02:1.1,1.2:0.1:2];yy=[-1:0.1:-0.2,-0.1:0 .02:0.1,0.2:0.1:1];[x,y]=meshgrid(xx,yy);z=1./((sqrt(1-x).^2+y.^2))+1./(sqrt((1+x).^2+y. ^2));subplot(224),surf(x,y,z),shadingflat;zlim([0,15]);subplot(221),surf(x,y,z),view(0,90);subplot(222),surf(x,y,z),view(90,0) ;subplot(223),surf(x,y,z),view(0,0)第七题(1)>> syms x;f=(3^x+9^x)^(1/x);limit(f,x,inf)ans =9(2)>> syms x y;f=x*y/(sqrt(x*y+1)-1);limit(limit(f,y,0),x,0)ans =2(3)>> syms x y;f=(1-cos(x^2+y^2))/((x^2+y^2)*exp(x^2+y^2));limit(limit(f,y,0),x,0) ans =第八题第一步新建脚本function result=paradiff(y,x,t,n)if mod(n,1)~=0,error('n should positive innteger,please correct')else if n==1,result=diff(y,t)/diff(x,t);else result=diff(y,x,t,n-1)/diff(x,t);end,end(1)syms t;x=log(cos(t));y=cos(t)-t*sin(t);f=simplify(paradiff(y,x,t,1))f =(cos(t)*(2*sin(t) + t*cos(t)))/sin(t)(2)syms t;f=(cos(t)*(2*sin(t) + t*cos(t)))/sin(t);f1=diff(f)f1 =(cos(t)*(3*cos(t) - t*sin(t)))/sin(t) - t*cos(t) - (cos(t)^2*(2*sin(t) + t*cos(t)))/sin(t)^2 - 2*sin(t)t=pi/3;f1 =(cos(t)*(3*cos(t) - t*sin(t)))/sin(t) - t*cos(t) - (cos(t)^2*(2*sin(t) + t*cos(t)))/sin(t)^2 - 2*sin(t)f1 =-2.6651第九题>> syms x y t;f=int(exp(-t^2),t,0,x*y);F=(x/y)*(diff(f,x,2))-2*(diff(diff(f,x,1),y,1))+diff(f,y,2)F =2*x^2*y^2*exp(-x^2*y^2) - 2*x^3*y*exp(-x^2*y^2) - 2*exp(-x^2*y^2)第十题(1)>> syms n m;limit(symsum(1/((2*m)^2-1),m,1,n),n,inf)1/2(2)>> syms m n;limit(symsum(n*(1/(n^2+m*pi)),m,1,n),n,inf)ans =1第十一题(1)>> syms t;syms a positive;x=a*(cos(t)+t*sin(t));y=a*(sin(t)-t*cos(t));I=int((x^2+y^2)*sqrt(diff(y,t)^2+diff (x,t)^2),t,0,2*pi)I =2*pi^2*a^3*(2*pi^2 + 1)(2)>> syms t x y;syms a c b positive;x=c*cos(t)/a;y=c*sin(t)/b;F=[y*x^3+exp(y),x*y^3+x*exp(y)-2*y];ds=[diff(x,t); diff(y,t)]; I=int(F*ds,t,pi,0)I =(2*c*(15*b^4 - 2*c^4))/(15*a*b^4)第十二题>> syms a b c d e;A=[a^4,a^3,a^2,a,1;b^4,b^3,b^2,b,1;c^4,c^3,c^2,c,1;d^4,d^3,d^2,d,1;e^4,e^3,e^2,e,1];simplify (det(A))ans =(a - b)*(a - c)*(a - d)*(b - c)*(a - e)*(b - d)*(b - e)*(c - d)*(c - e)*(d - e)第十三题>> A=[2 0.5 -0.5 0.5;0 -1.5 0.5 -0.5;2,0.5 -4.5 0.5;2 1 -2 -2];[V,J]=jordan(sym(A))V =[ 0, 1/8, 1/2, 3/8][ 0, 0, -2, -3][ -1/6, 1/24, -1/2, 1/8][ -1/6, 1/24, -5/2, 9/8]J =[ -4, 0, 0, 0][ 0, 2, 0, 0][ 0, 0, -2, 1][ 0, 0, 0, -2]第十四题编写函数代码function X=lyapsym(A,B,C)if nargin==2,C=B;B=A';end[nr,nc]=size(C);A0=kron(A,eye(nc))+kron(eye(nr),B');tryC1=C';x0=-inv(A0)*C1(:);X=reshape(x0,nc,nr)';catch,error('singular matrix found.'),end>> A=[3 -6 -4 0 5;1 4 2 -2 4;-6 3 -6 7 3;-13 10 0 -11 0;0 4 0 3 4];B=[3 -2 1;-2 -9 2;-2 -1 9];C=-[-2 1 -1;4 1 2;5 -6 1;6 -4 -4;-6 6 -3];X=lyap(A,B,C),norm(A*X+X*B+C);X=lyapsym(sym(A),B,C),norm(A*X+X*B+C)X =4.0569 14.5128 -1.5653-0.0356 -25.0743 2.7408-9.4886 -25.9323 4.4177-2.6969 -21.6450 2.8851-7.7229 -31.9100 3.7634X =[ 434641749950/107136516451, 4664546747350/321409549353, -503105815912/321409549353][ -3809507498/107136516451, -8059112319373/321409549353, 880921527508/321409549353][ -1016580400173/107136516451, -8334897743767/321409549353, 1419901706449/321409549353][ -288938859984/107136516451, -6956912657222/321409549353, 927293592476/321409549353][ -827401644798/107136516451, -10256166034813/321409549353, 1209595497577/321409549353](2)>> A=[3 -6 -4 0 5;1 4 2 -2 4;-6 3 -6 7 3;-13 10 0 -11 0;0 4 0 3 4];B=[3 -2 1;-2 -9 2;-2 -1 9];C=-[-2 1 -1;4 1 2;5 -6 1;6 -4 -4;-6 6 -3];X=lyap(A,B,C),norm(A*X+X*B+C)X =4.0569 14.5128 -1.5653-0.0356 -25.0743 2.7408-9.4886 -25.9323 4.4177-2.6969 -21.6450 2.8851-7.7229 -31.9100 3.7634ans =3.4356e-13第十五题>> syms t;A=[-4.5,0,0.5,-1.5;-0.5,-4,0.5,-0.5;1.5,1,-2.5,1.5;0,-1,-1,-3];B=simplify(expm(A*t))B =[ (exp(-5*t)*(exp(2*t) - t*exp(2*t) + t^2*exp(2*t) + 1))/2, (exp(-5*t)*(2*t*exp(2*t) - exp(2*t) + 1))/2, (t*exp(-3*t)*(t + 1))/2, -(exp(-5*t)*(exp(2*t) + t*exp(2*t) - t^2*exp(2*t) - 1))/2][ (exp(-5*t)*(t*exp(2*t) - exp(2*t) + 1))/2, (exp(-5*t)*(exp(2*t) + 1))/2, (t*exp(-3*t))/2, (exp(-5*t)*(t*exp(2*t) - exp(2*t) + 1))/2][ (exp(-5*t)*(exp(2*t) + t*exp(2*t) - 1))/2, (exp(-5*t)*(exp(2*t) - 1))/2, (exp(-3*t)*(t + 2))/2, (exp(-5*t)*(exp(2*t) + t*exp(2*t) - 1))/2][ -(t^2*exp(-3*t))/2, -t*exp(-3*t), -(t*exp(-3*t)*(t + 2))/2, -(exp(-3*t)*(t^2 - 2))/2]>> C=simplify(sin(A*t))C =[ -sin((9*t)/2), 0, sin(t/2), -sin((3*t)/2)][ -sin(t/2), -sin(4*t), sin(t/2), -sin(t/2)][ sin((3*t)/2), sin(t), -sin((5*t)/2), sin((3*t)/2)][ 0, -sin(t), -sin(t), -sin(3*t)]>> D=simplify(expm(A*t)*sin(A^2*expm(A*t)*t))D =[ (exp(-5*t)*sin((t*exp(-5*t)*(9*t*exp(2*t) - 15*exp(2*t) + 25))/2)*(2*t*exp(2*t) - exp(2*t) + 1))/2 + (exp(-5*t)*sin((t*exp(-3*t)*(9*t^2 - 12*t + 2))/2)*(exp(2*t) + t*exp(2*t) - t^2*exp(2*t) - 1))/2 + (exp(-5*t)*sin((t*exp(-5*t)*(17*exp(2*t) - 21*t*exp(2*t) + 9*t^2*exp(2*t) + 25))/2)*(exp(2*t) - t*exp(2*t) + t^2*exp(2*t) + 1))/2 + (t*exp(-3*t)*sin((t*exp(-5*t)*(3*exp(2*t) + 9*t*exp(2*t) - 25))/2)*(t + 1))/2, (exp(-5*t)*sin((t*exp(-5*t)*(9*exp(2*t) + 25))/2)*(2*t*exp(2*t) - exp(2*t) + 1))/2 + (exp(-5*t)*sin((t*exp(-5*t)*(18*t*exp(2*t) - 21*exp(2*t) + 25))/2)*(exp(2*t) - t*exp(2*t) + t^2*exp(2*t) + 1))/2 + (exp(-5*t)*sin(3*t*exp(-3*t)*(3*t - 2))*(exp(2*t) + t*exp(2*t) - t^2*exp(2*t) - 1))/2 + (t*exp(-3*t)*sin((t*exp(-5*t)*(9*exp(2*t) - 25))/2)*(t + 1))/2, (exp(-5*t)*sin((3*t*exp(-3*t)*(3*t - 2))/2)*(2*t*exp(2*t) - exp(2*t) + 1))/2 - (exp(-5*t)*sin((t*exp(-3*t)*(- 9*t^2 + 3*t + 4))/2)*(exp(2*t) - t*exp(2*t) + t^2*exp(2*t) + 1))/2 + (exp(-5*t)*sin((t*exp(-3*t)*(9*t^2 + 6*t - 10))/2)*(exp(2*t) + t*exp(2*t) - t^2*exp(2*t) - 1))/2 + (t*exp(-3*t)*sin((3*t*exp(-3*t)*(3*t + 4))/2)*(t + 1))/2, (exp(-5*t)*sin((t*exp(-5*t)*(9*t*exp(2*t) - 15*exp(2*t) + 25))/2)*(2*t*exp(2*t) - exp(2*t) + 1))/2 - (exp(-5*t)*sin((t*exp(-5*t)*(exp(2*t) + 21*t*exp(2*t) - 9*t^2*exp(2*t) - 25))/2)*(exp(2*t) - t*exp(2*t) + t^2*exp(2*t) + 1))/2 - (exp(-5*t)*sin((t*exp(-3*t)*(- 9*t^2 + 12*t + 16))/2)*(exp(2*t) + t*exp(2*t) - t^2*exp(2*t) - 1))/2 + (t*exp(-3*t)*sin((t*exp(-5*t)*(3*exp(2*t) + 9*t*exp(2*t) - 25))/2)*(t + 1))/2][ (exp(-5*t)*sin((t*exp(-5*t)*(1 7*exp(2*t) - 21*t*exp(2*t) + 9*t^2*exp(2*t) + 25))/2)*(t*exp(2*t) - exp(2*t) + 1))/2 - (exp(-5*t)*sin((t*exp(-3*t)*(9*t^2 - 12*t + 2))/2)*(t*exp(2*t) - exp(2*t) + 1))/2 + (t*exp(-3*t)*sin((t*exp(-5*t)*(3*exp(2*t) + 9*t*exp(2*t) - 25))/2))/2 + (exp(-5*t)*sin((t*exp(-5*t)*(9*t*exp(2*t) - 15*exp(2*t) + 25))/2)*(exp(2*t) + 1))/2, (exp(-5*t)*sin((t*exp(-5*t)*(18*t*exp(2*t) - 21*exp(2*t) + 25))/2)*(t*exp(2*t) - exp(2*t) + 1))/2 - (exp(-5*t)*sin(3*t*exp(-3*t)*(3*t - 2))*(t*exp(2*t) - exp(2*t) + 1))/2 + (t*exp(-3*t)*sin((t*exp(-5*t)*(9*exp(2*t) - 25))/2))/2 + (exp(-5*t)*sin((t*exp(-5*t)*(9*exp(2*t) + 25))/2)*(exp(2*t) + 1))/2, (exp(-3*t)*sin((3*t*exp(-3*t)*(3*t - 2))/2))/2 + (exp(-5*t)*sin((3*t*exp(-3*t)*(3*t - 2))/2))/2 + (exp(-3*t)*sin((t*exp(-3*t)*(- 9*t^2 + 3*t + 4))/2))/2 - (exp(-5*t)*sin((t*exp(-3*t)*(- 9*t^2 + 3*t + 4))/2))/2 + (exp(-3*t)*sin((t*exp(-3*t)*(9*t^2 + 6*t - 10))/2))/2 - (exp(-5*t)*sin((t*exp(-3*t)*(9*t^2 + 6*t - 10))/2))/2 + (t*exp(-3*t)*sin((3*t*exp(-3*t)*(3*t + 4))/2))/2 - (t*exp(-3*t)*sin((t*exp(-3*t)*(- 9*t^2 + 3*t + 4))/2))/2 - (t*exp(-3*t)*sin((t*exp(-3*t)*(9*t^2 + 6*t - 10))/2))/2, (exp(-5*t)*sin((t*exp(-3*t)*(- 9*t^2 + 12*t + 16))/2)*(t*exp(2*t) - exp(2*t) + 1))/2 - (exp(-5*t)*sin((t*exp(-5*t)*(exp(2*t) + 21*t*exp(2*t) - 9*t^2*exp(2*t) - 25))/2)*(t*exp(2*t) - exp(2*t) + 1))/2 + (t*exp(-3*t)*sin((t*exp(-5*t)*(3*exp(2*t) + 9*t*exp(2*t) - 25))/2))/2 + (exp(-5*t)*sin((t*exp(-5*t)*(9*t*exp(2*t) - 15*exp(2*t) + 25))/2)*(exp(2*t) + 1))/2][ (exp(-5*t)*sin((t*exp(-5*t)*(17*ex p(2*t) - 21*t*exp(2*t) + 9*t^2*exp(2*t) + 25))/2)*(exp(2*t) + t*exp(2*t) - 1))/2 - (exp(-5*t)*sin((t*exp(-3*t)*(9*t^2 - 12*t + 2))/2)*(exp(2*t) + t*exp(2*t) - 1))/2 + exp(-3*t)*sin((t*exp(-5*t)*(3*exp(2*t) + 9*t*exp(2*t) - 25))/2)*(t/2 + 1) + (exp(-5*t)*sin((t*exp(-5*t)*(9*t*exp(2*t) - 15*exp(2*t) + 25))/2)*(exp(2*t) - 1))/2, exp(-3*t)*sin((t*exp(-5*t)*(9*exp(2*t) - 25))/2)*(t/2 + 1) + (exp(-5*t)*sin((t*exp(-5*t)*(9*exp(2*t) + 25))/2)*(exp(2*t) - 1))/2 + (exp(-5*t)*sin((t*exp(-5*t)*(18*t*exp(2*t) - 21*exp(2*t) + 25))/2)*(exp(2*t) + t*exp(2*t) - 1))/2 - (exp(-5*t)*sin(3*t*exp(-3*t)*(3*t - 2))*(exp(2*t) + t*exp(2*t) - 1))/2, exp(-3*t)*sin((3*t*exp(-3*t)*(3*t + 4))/2)*(t/2 + 1) - (exp(-5*t)*sin((t*exp(-3*t)*(9*t^2 + 6*t - 10))/2)*(exp(2*t) + t*exp(2*t) - 1))/2 - (exp(-5*t)*sin((t*exp(-3*t)*(- 9*t^2 + 3*t + 4))/2)*(exp(2*t) + t*exp(2*t) - 1))/2 + (exp(-5*t)*sin((3*t*exp(-3*t)*(3*t - 2))/2)*(exp(2*t) - 1))/2, (exp(-5*t)*sin((t*exp(-3*t)*(- 9*t^2 + 12*t + 16))/2)*(exp(2*t) + t*exp(2*t) - 1))/2 + exp(-3*t)*sin((t*exp(-5*t)*(3*exp(2*t) + 9*t*exp(2*t) - 25))/2)*(t/2 + 1) + (exp(-5*t)*sin((t*exp(-5*t)*(9*t*exp(2*t) - 15*exp(2*t) + 25))/2)*(exp(2*t) - 1))/2 - (exp(-5*t)*sin((t*exp(-5*t)*(exp(2*t) + 21*t*exp(2*t) - 9*t^2*exp(2*t) - 25))/2)*(exp(2*t) + t*exp(2*t) - 1))/2][exp(-3*t)*sin((t*exp(-3*t)*(9*t^2 - 12*t + 2))/2)*(t^2/2 - 1) - (t^2*exp(-3*t)*sin((t*exp(-5*t)*(17*exp(2*t) - 21*t*exp(2*t) + 9*t^2*exp(2*t) + 25))/2))/2 - t*exp(-3*t)*sin((t*exp(-5*t)*(9*t*exp(2*t) - 15*exp(2*t) + 25))/2) - (t*exp(-3*t)*sin((t*exp(-5*t)*(3*exp(2*t) + 9*t*exp(2*t) - 25))/2)*(t + 2))/2, exp(-3*t)*sin(3*t*exp(-3*t)*(3*t - 2))*(t^2/2 - 1) - t*exp(-3*t)*sin((t*exp(-5*t)*(9*exp(2*t) + 25))/2) - (t^2*exp(-3*t)*sin((t*exp(-5*t)*(18*t*exp(2*t) - 21*exp(2*t) + 25))/2))/2 - (t*exp(-3*t)*sin((t*exp(-5*t)*(9*exp(2*t) - 25))/2)*(t + 2))/2, -(exp(-3*t)*(2*sin((t*exp(-3*t)*(9*t^2 + 6*t - 10))/2) - t^2*sin((t*exp(-3*t)*(- 9*t^2 + 3*t + 4))/2) - t^2*sin((t*exp(-3*t)*(9*t^2 + 6*t - 10))/2) + 2*t*sin((3*t*exp(-3*t)*(3*t - 2))/2) + 2*t*sin((3*t*exp(-3*t)*(3*t + 4))/2) + t^2*sin((3*t*exp(-3*t)*(3*t + 4))/2)))/2, exp(-3*t)*sin((t*exp(-3*t)*(- 9*t^2 + 12*t + 16))/2) - (t^2*exp(-3*t)*sin((t*exp(-3*t)*(- 9*t^2 + 12*t + 16))/2))/2 - t*exp(-3*t)*sin((t*exp(-5*t)*(3*exp(2*t) + 9*t*exp(2*t) - 25))/2) - t*exp(-3*t)*sin((t*exp(-5*t)*(9*t*exp(2*t) - 15*exp(2*t) + 25))/2) - (t^2*exp(-3*t)*sin((t*exp(-5*t)*(3*exp(2*t) + 9*t*exp(2*t) - 25))/2))/2 + (t^2*exp(-3*t)*sin((t*exp(-5*t)*(exp(2*t) + 21*t*exp(2*t) - 9*t^2*exp(2*t) - 25))/2))/2]第二部分数学问题求解与数据处理第一题(1)>> syms a t;f=sin(a*t)/t;F=simplify(laplace(f))F =atan(a/s)(2)>> syms a t;f=t^5*sin(a*t);F=simplify(laplace(f))F =(240*a*s*(3*a^4 - 10*a^2*s^2 + 3*s^4))/(a^2 + s^2)^6(3)>> syms a t;f=t^8*cos(a*t);F=simplify(laplace(f))F =(40320*s*(9*a^8 - 84*a^6*s^2 + 126*a^4*s^4 - 36*a^2*s^6 + s^8))/(a^2 + s^2)^9 第二题(1)>> syms a s b t;f=1/(sqrt(s*s)*(s^2-a^2)*(s+b));f1=ilaplace(f,s,t)f1 =-ilaplace(1/((b + s)*(a^2 - s^2)*(s^2)^(1/2)), s, t)(2)>> syms s a b t;f=sqrt(s-a)-sqrt(s-b);f1=ilaplace(f,s,t)f1 =ilaplace((s - a)^(1/2), s, t) - ilaplace((s - b)^(1/2), s, t)(3)>> syms a b s t;f=log((s-a)/(s-b));f1=ilaplace(f,s,t)f1 =exp(b*t)/t - exp(a*t)/t第三题(1)>> syms x t;f=x^2*(3*pi-abs(2*x));f1=ifourier(f,x)f1 =-(24/x^4 + 6*pi^2*dirac(x, 2))/(2*pi)(2)>> syms t;f=t^2*(t-2*pi);f1=ifourier(f,t)f1 =(pi*dirac(t, 3)*2*i + 4*pi^2*dirac(t, 2))/(2*pi)第四题(1)>> syms a T K;f=cos(K*a*T);f1=ztrans(f);f2=iztrans(f1)f2 =cos(K*T*n)(2)>> syms a T K;f=(K*T)^2*exp(-a*K*T);f1=ztrans(f);f2=iztrans(f1)f2 =K^2*T^2*kroneckerDelta(n, 0) - K^2*T^2*exp(-K*T)*(exp(K*T)*kroneckerDelta(n, 0) - exp(-K*T)^n*exp(K*T))(3)>> syms a K T;f=(a*T*K-1+exp(-a*T*K))/a;f1=ztrans(f);f2=iztrans(f1)f2 =exp(-K*T)^n/n - 1/n - K*T*(kroneckerDelta(n, 0) - 1) + K*T*kroneckerDelta(n, 0)第五题(1)>> syms x;f=exp(-(x+1)^2+pi/2)*sin(5*x+2);t=solve(f)t =-2/5>> subs(f,x,-2/5)ans =(2)>> syms x y;f=(x^2+y^2+x*y)*exp(-x^2-y^2-x*y);x1=solve('(x^2+y^2+x*y)*exp(-x^2-y^2-x*y)','x') x1 =- y/2 + (3^(1/2)*y*i)/2- y/2 - (3^(1/2)*y*i)/2>> simplify(subs(f,x,x1))ans =第六题>> syms c x;solve(diff(int((exp(x)-c*x)^2,x,0,1),c))ans =3第七题写函数: function [c,ce]=c6exmcon(x)ce=[];c=[x(1)+x(2);x(1)*x(2)-x(1)-x(2)+1.5;-10-x(1)*x(2)];>> clear P;P.nonlcon=@c6exmcon;P.solver='fmincon';P.options=optimset;P.objective=@(x)exp(x(1))*(4*x( 1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);...ff=optimset;ff.TolX=1e-20;ff.TolFum=1e-20;P.options=ff;P.lb=[-10;-10];P.ub=-P.lb;P.x0=[0;0];[x,f1,fla g]=fmincon(P)Local minimum found that satisfies the constraints.Optimization completed because the objective function is non-decreasing infeasible directions, to within the default value of the function tolerance,and constraints are satisfied to within the default value of the constraint tolerance.<stopping criteria details>x =1.1825-1.7398f1 =3.0608flag =1第八题N=10;[x1,x2,x3,x4,x5,x6,x7]=ndgrid(1:N,1:N,4:N,1:N,2:N,5:N,6:N);i=find((-x1<=0)&(-x2<=0)&(-x3< =0)&(-x4<=0)&(-x5<=0)&(-x6<=0)&(-x7<=0)&(3534*x1+2356*x2+1767*x3+589*x4+528*x5+451* x6+304*x7<=119567));x1=x1(i);x2=x2(i);x3=x3(i);x4=x4(i);x5=x5(i);x6=x6(i);x7=x7(i);f=592*x1+381 *x2+273*x3+55*x4+48*x5+37*x6+23*x7;[fmax,ii]=sort(f);index=ii(1);x=[x1(index),x2(index),x3(in dex),x4(index),x5(index),x6(index),x7(index)]x =1 1 4 12 5 6第九题syms x y;y1=dsolve('D2y-(2-1/x)*Dy+(1-1/x)*y=x^2*exp(-5*x)','x');simplify(y1)ans =(exp(-5*x)*(30*x - 6*ei(-6*x)*exp(6*x) + 36*x^2 + 1296*C7*exp(6*x) + 1296*C8*exp(6*x)*log(x) + 11))/1296>> y=dsolve('D2y-(2-1/x)*Dy+(1-1/x)*y=x^2*exp(-5*x)','y(1)=sym(pi)','y((pi))=1','x');simplify(y)ans =(exp(-5*x)*(30*x + 6*log(x) + 108*x^2*log(x) + 216*x^3*log(x) - 6*ei(-6*x)*exp(6*x) + 36*x*log(x) + 36*x^2 + 11))/1296 - (exp(-5*x)*log(x)*(216*x^3 + 108*x^2 + 36*x + 6))/1296 + (exp(-6)*exp(x)*(6*ei(-6)*exp(6) + 1296*exp(5)*sym(pi) - 77))/1296 - (exp(-6*pi)*exp(-6)*exp(x)*log(x)*(11*exp(6) - 77*exp(6*pi) + 30*pi*exp(6) + 36*pi^2*exp(6) - 1296*exp(5*pi)*exp(6) + 6*ei(-6)*exp(6*pi)*exp(6) + 1296*exp(6*pi)*exp(5)*sym(pi) - 6*exp(6*pi)*exp(6)*ei(-6*pi)))/(1296*log(pi))第十题(1)>> syms t;x=dsolve('D2x+2*t*Dx+t^2*x=t+1');simplify(x)ans =C4*exp(-(t*(t - 2))/2) + C5*exp(-(t*(t + 2))/2) - (2^(1/2)*pi^(1/2)*erf(2^(1/2)*((t*i)/2 - i/2))*exp(-(t - 1)^2/2)*i)/2(2)>> syms x y;y=dsolve('Dy+2*x*y=x*exp(-x^2)')y =(exp(-x^2)*(C2*exp(-2*t*x) + 1))/2第十一题>>f=@(t,x)[-x(2)-x(3);x(1)+0.2*x(2);0.2+(x(1)-5.7)*x(3)];[t,x]=ode45(f,[0,100],[0;0;0]);plot(t,x) ;grid ;view(0,90)第十二题>>f=inline(['[x(2);-x(1)-x(3)-(3*x(2))^2+(x(4))^3+6*x(5)+2*t;','x(4);x(5);-x(5)-x(2)-exp(-x(1))-t]'],'t','x'); [t1,x1]=ode45(f,[1,0],[2,-4,-2,7,6]'); [t2,x2]=ode45(f,[1,2],[2,-4,-2,7,6]'); t=[t1(end:-1:1);t2];x=[x1(end:-1:1,:);x2];plot(x(:,1),x(:,3));plot(t,x)>> plot(x(:,1),x(:,3))第十三题[t,x,y]=sim('untitled12',[0,10]);plot(t,x)figure;plot(t,y)第十四题t=0:0.2:2; y=t.^2.*exp(-5*t).*sin(t);plot(t,y,'o')ezplot('t.^2.*exp(-5*t).*sin(t)',[0,2]);hold on;x1=0:0.01:2;y1=interp1(t,y,x1,'spline');plot(x1,y1)。
close all %关闭打开了的所有图形窗口clc %清屏命令clear %清除工作空间中所有变量%定义时间范围t=[0:pi/10:8*pi];y=sin(t);plot(t,y,'b:square')close allclcclear%定义时间范围t=[0:pi/20:9*pi];grid onhold on %允许在同一坐标系下绘制不同的图形plot(t,sin(t),'r:*')plot(t,cos(t))plot(t,-cos(t),'k')%grid on %在所画出的图形坐标中添加栅格,注意用在plot之后4-1:close allclcclear%定义时间范围t=[0:pi/20:9*pi];hold on %允许在同一坐标系下绘制不同的图形plot(t,sin(t),'r:*')plot(t,cos(t))plot(t,-cos(t),'k')grid on %在所画出的图形坐标中添加栅格,注意用在plot之后hold off %覆盖旧图,自动把栅格去掉,且若要在加入栅格就%必须把grid on加在plot后面plot(t,-sin(t))grid on%主程序exp2_10.mglobal a %声明变量a为全局变量x=1:100;a=3;c=prods(x) %调用子程序prods.m%子程序prods.m% function result=prods(x)% global a% result=a*sum(x);%声明了与主程序一样的全局变量a,以便在子程序中可以%使用主程序中定义的变量答案:15150exmdl2_1.mclearclose allclct=[0:pi/20:5*pi];figure(1)plot(t,out)grid onxlabel('time')ylabel('magnitude')exp2_1.mclc %清屏clear %从内存中清除变量和函数more onecho on%求矩阵与矩阵的乘积,矩阵与向量的乘积A=[5 6 7;9 4 6;4 3 6]B=[3 4 5;5 7 9;7 3 1]C=A*BY=A*Xmore offecho off答案:%求矩阵与矩阵的乘积,矩阵与向量的乘积A=[5 6 7;9 4 6;4 3 6]A =5 6 79 4 64 3 6B=[3 4 5;5 7 9;7 3 1]B =3 4 55 7 97 3 1X=[5 ;7;8]X =578C=A*BC =94 83 8689 82 8769 55 53Y=A*XY =12189more offecho offexp2_2.mclcclearmore onecho on%为便于理解,在程序等执行过程中显示程序的表达式a=16;b=12;c=3;d=4;e=a+b-c*df=e/2k=e\2h=c^3g=e+f+ ...2+1-9aa=sin(g)abs(aa)bb=2+3jcc=conj(bb)rbb=real(bb) log(rbb) sqrt(rbb) exp(rbb) echo off more offa=16;b=12;c=3;d=4;e=a+b-c*de =16f=e/2f =8k=e\2k =0.1250h=c^3h =27g=e+f+ ...2+1-9g =18aa=sin(g)aa =-0.7510abs(aa)ans =0.7510bb=2+3jbb =2.0000 +3.0000i cc=conj(bb)cc =2.0000 -3.0000i rbb=real(bb)rbb =2log(rbb)ans =0.6931sqrt(rbb)ans =1.4142exp(rbb)ans =7.3891echo offans =7.3891exp2_5.m%绘制单位圆clearclose allclc%定义时间范围t=[0:0.01:2*pi];x=sin(t);y=cos(t);plot(x,y)axis([-1.5 1.5 -1.5 1.5])%限定x轴和y轴的显示范围grid onaxis('equal')%axis([xmin,xmax,ymin,ymax])函数来调整图轴的范围:答案:exp2_5_.mclearclose allclct=[0:pi/20:5*pi];plot(t,sin(t),'r:*')axis([0 5*pi -1.5 1.5 ])%给x轴和y轴命名xlabel('t(deg)')ylabel('magnitude')%给图形加标题title('sine wave from zero to 5\pi')%在指定位置创建说明性文字text(pi/2,sin(pi/2),'\bullet\leftarrow The sin(t) at t=2')%图形文字标示命令的使用clearclose allclct=[0:pi/20:5*pi];plot(t,sin(t),'r:*')axis([0 5*pi -1.5 1.5 ])%给x轴和y轴命名xlabel('t(deg)')ylabel('magnitude')%给图形加标题title('sine wave from zero to 5\pi')%在指定位置创建说明性文字text(pi/2,sin(pi/2),'\bullet\leftarrow The sin(t) at t=2') %输入特定的字符%\pi%\alpha%\leftarrow%\rightarrow%\bullet(点号)hold onplot(t,cos(t))%区分图形上不同的曲线legend('sin(t)','cos(t)')%用鼠标在特定位置输入文字gtext('文字标示命令举例') hold offexp2_6.m%图形分割命令的使用clearclose allclct=[0:pi/20:5*pi];subplot(321)plot(t,sin(t))axis([0 16 -1.5 1.5]) xlabel('t(deg)') ylabel('magnitude') grid ontitle('sin(t)') subplot(322)plot(t,-sin(t))axis([0 16 -1.5 1.5]) xlabel('t(deg)') ylabel('magnitude') grid ontitle('-sin(t)') subplot(323)plot(t,cos(t))axis([0 16 -1.5 1.5]) xlabel('t(deg)') ylabel('magnitude') grid ontitle('cos(t)') subplot(324)plot(t,-cos(t))axis([0 16 -1.5 1.5]) xlabel('t(deg)') ylabel('magnitude') grid ontitle('-cos(t)') subplot(325) subplot(326)exp2_7.mclcclear%绘制对应于每个输入x的输出y的高度条形图subplot(221)x=[1 2 3 4 5 6 7 8 9 10];y=[5 6 3 4 8 1 10 3 5 6];bar(x,y)%绘制x1在以y1为中心的区间中分布的个数条形图subplot(222)x1=randn(1,1000);%生成1000个各随机数y1=-3:0.1:3;hist(x1,y1)%绘制y2对应于x2的梯形图subplot(223)x2=0:0.1:10;y2=1./(x2.^3-2.*x2+4);stairs(x2,y2)%绘制y3对应于x3的散点图subplot(224)x3=0:0.1:10;y3=1./(x2.^3-2.*x2+4);stem(x3,y3)exp2_8.mecho off % 不显示程序内容%clear allclearclca=4;b=6disp('暂停,请按任意键继续') % disp指令可以用来显示字符pause % 暂停,直到用户按任意键echo on% 显示程序内容,注意matlab默认是不显示c=a+b% 暂时把控制权交给键盘(在命令窗口中出现k提示符), % 输入return,回车后退出,继续执行下面的语句。
matlab基本语句及语法一、基本语法1. 变量定义与赋值:在MATLAB中,可以使用等号(=)将一个数值或表达式赋值给一个变量。
例如:a = 5; 表示将数值5赋值给变量a。
2. 注释:在MATLAB中,可以使用百分号(%)来添加注释,以便于代码的阅读和理解。
例如:% 这是一条注释。
3. 函数的定义与调用:在MATLAB中,可以使用关键字function 来定义函数,并使用函数名进行调用。
例如:function result = add(a, b) 表示定义了一个名为add的函数,该函数接受两个参数a 和b,并返回一个结果result。
4. 条件语句:在MATLAB中,可以使用if语句来实现条件判断。
例如:if a > b 表示如果a大于b,则执行if语句块中的代码。
5. 循环语句:在MATLAB中,可以使用for循环和while循环来实现循环操作。
例如:for i = 1:10 表示从1循环到10,每次循环中i 的值递增1。
6. 矩阵的定义与操作:在MATLAB中,可以使用方括号([])来定义矩阵,并使用各种运算符进行矩阵的操作。
例如:A = [1 2; 3 4] 表示定义了一个2x2的矩阵A。
7. 字符串的操作:在MATLAB中,可以使用单引号('')来定义字符串,并使用加号(+)来进行字符串的拼接。
例如:str = 'Hello' + 'World' 表示将字符串'Hello'和'World'进行拼接。
8. 文件的读写:在MATLAB中,可以使用fopen、fread、fwrite 等函数来进行文件的读写操作。
例如:fid = fopen('file.txt', 'w') 表示打开一个名为file.txt的文件,并以写入模式打开。
9. 图形绘制:在MATLAB中,可以使用plot、scatter、histogram等函数来进行图形的绘制。
closeall %关闭打开了的所有图形窗口clc %清屏命令clear%清除工作空间中所有变量%定义时间范围t=[0:pi/10:8*pi];y=sin(t);plot(t,y,'b:square')closeallclcclear%定义时间范围t=[0:pi/20:9*pi];grid onhold on %允许在同一坐标系下绘制不同的图形plot(t,sin(t),'r:*')plot(t,cos(t))plot(t,-cos(t),'k')%grid on %在所画出的图形坐标中添加栅格,注意用在pl ot之后4-1:closeallclcclear%定义时间范围t=[0:pi/20:9*pi];hold on %允许在同一坐标系下绘制不同的图形plot(t,sin(t),'r:*')plot(t,cos(t))plot(t,-cos(t),'k')grid on %在所画出的图形坐标中添加栅格,注意用在pl ot之后hold off %覆盖旧图,自动把栅格去掉,且若要在加入栅格就%必须把gri d on加在pl ot后面plot(t,-sin(t))grid on%主程序exp2_10.mglobal a %声明变量a为全局变量x=1:100;a=3;c=prods(x) %调用子程序p rods.m%子程序pro d s.m% functi on result=prods(x)% global a% result=a*sum(x);%声明了与主程序一样的全局变量a,以便在子程序中可以%使用主程序中定义的变量答案:15150exmdl2_1.mclearcloseallclct=[0:pi/20:5*pi];figure(1)plot(t,out)grid onxlabel('time')ylabel('magnit ude')exp2_1.mclc %清屏clear%从内存中清除变量和函数more onecho on%求矩阵与矩阵的乘积,矩阵与向量的乘积A=[5 6 7;9 4 6;4 3 6]B=[3 4 5;5 7 9;7 3 1]C=A*BY=A*Xmore offecho off答案:%求矩阵与矩阵的乘积,矩阵与向量的乘积A=[5 6 7;9 4 6;4 3 6]A =5 6 79 4 64 3 6B=[3 4 5;5 7 9;7 3 1]B =3 4 55 7 97 3 1X=[5 ;7;8]X =578C=A*BC =94 83 8689 82 8769 55 53Y=A*XY =12189more offecho offexp2_2.mclcclearmore onecho on%为便于理解,在程序等执行过程中显示程序的表达式a=16;b=12;c=3;d=4;e=a+b-c*df=e/2k=e\2h=c^3g=e+f+ ...2+1-9aa=sin(g)abs(aa)bb=2+3jcc=conj(bb)rbb=real(bb) log(rbb) sqrt(rbb) exp(rbb) echo off more offa=16;b=12;c=3;d=4;e=a+b-c*de =16f=e/2f =8k=e\2k =0.1250h=c^3h =27g=e+f+ ...2+1-9g =18aa=sin(g)aa =-0.7510abs(aa)ans =0.7510bb=2+3jbb =2.0000 +3.0000icc=conj(bb)cc =2.0000 -3.0000irbb=real(bb)rbb =2log(rbb)ans =0.6931sqrt(rbb)ans =1.4142exp(rbb)ans =7.3891echo offans =7.3891exp2_5.m%绘制单位圆clearcloseallclc%定义时间范围t=[0:0.01:2*pi];x=sin(t);y=cos(t);plot(x,y)axis([-1.5 1.5 -1.5 1.5])%限定x轴和y轴的显示范围grid onaxis('equal')%axis([xmin,xmax,ymin,ymax])函数来调整图轴的范围:答案:exp2_5_.mclearcloseallclct=[0:pi/20:5*pi];plot(t,sin(t),'r:*')axis([0 5*pi -1.5 1.5 ])%给x轴和y轴命名xlabel('t(deg)')ylabel('magnit ude')%给图形加标题title('sine wave from zero to 5\pi')%在指定位置创建说明性文字text(pi/2,sin(pi/2),'\bullet\leftar row The sin(t) at t=2')%图形文字标示命令的使用clearcloseallclct=[0:pi/20:5*pi];plot(t,sin(t),'r:*')axis([0 5*pi -1.5 1.5 ])%给x轴和y轴命名xlabel('t(deg)')ylabel('magnit ude')%给图形加标题title('sine wave from zero to 5\pi')%在指定位置创建说明性文字text(pi/2,sin(pi/2),'\bullet\leftar row The sin(t) at t=2') %输入特定的字符%\pi%\alpha%\leftar row%\righta rrow%\bullet(点号)hold onplot(t,cos(t))%区分图形上不同的曲线legend('sin(t)','cos(t)')%用鼠标在特定位置输入文字gtext('文字标示命令举例') hold offexp2_6.m%图形分割命令的使用clearcloseallclct=[0:pi/20:5*pi];subplo t(321)plot(t,sin(t))axis([0 16 -1.5 1.5]) xlabel('t(deg)') ylabel('magnit ude') grid ontitle('sin(t)')subplo t(322)plot(t,-sin(t))axis([0 16 -1.5 1.5]) xlabel('t(deg)') ylabel('magnit ude') grid ontitle('-sin(t)') subplo t(323)plot(t,cos(t))axis([0 16 -1.5 1.5]) xlabel('t(deg)') ylabel('magnit ude') grid ontitle('cos(t)')subplo t(324)plot(t,-cos(t))axis([0 16 -1.5 1.5]) xlabel('t(deg)') ylabel('magnit ude') grid ontitle('-cos(t)') subplo t(325) subplo t(326)exp2_7.mclcclear%绘制对应于每个输入x的输出y的高度条形图subplo t(221)x=[1 2 3 4 5 6 7 8 9 10];y=[5 6 3 4 8 1 10 3 5 6];bar(x,y)%绘制x1在以y1为中心的区间中分布的个数条形图subplo t(222)x1=randn(1,1000);%生成1000个各随机数y1=-3:0.1:3;hist(x1,y1)%绘制y2对应于x2的梯形图subplo t(223)x2=0:0.1:10;y2=1./(x2.^3-2.*x2+4);stairs(x2,y2)%绘制y3对应于x3的散点图subplo t(224)x3=0:0.1:10;y3=1./(x2.^3-2.*x2+4);stem(x3,y3)exp2_8.mecho off % 不显示程序内容%clearallclearclca=4;b=6disp('暂停,请按任意键继续') % disp指令可以用来显示字符pause% 暂停,直到用户按任意键echo on% 显示程序内容,注意matl ab默认是不显示c=a+b% 暂时把控制权交给键盘(在命令窗口中出现k提示符), % 输入retu rn,回车后退出,继续执行下面的语句。
题目:深入探讨MATLAB中while 1的用法一、引言MATLAB(Matrix Laboratory)是一种强大的数学软件,广泛应用于工程、科学领域的数据分析和可视化。
在MATLAB中,while循环是一种常见的控制结构,用于根据特定条件重复执行一段代码。
本文将深入探讨MATLAB中while 1的用法,以便读者更好地理解和应用该特性。
二、while 1的基本语法在MATLAB中,while 1是一种常见的编程技巧,用于创建一个无限循环。
它的基本语法如下:```while 1循环体代码end```其中,while关键字后的条件表达式为1,表示条件始终为真,因此循环将无限执行下去。
在循环体内部,可以编写一系列需重复执行的代码逻辑。
三、while 1的应用场景1. 任务监控在实际编程中,while 1常用于任务监控的场景。
当需要不断监听外部输入或者定时执行某个任务时,可以使用while 1构建一个持续监控的循环。
2. 实时数据处理MATLAB常用于处理实时数据,而while 1循环则可以保证对实时数据进行持续性的处理和分析。
这种用法通常用于传感器数据的采集与处理,网络数据的实时传输等场景。
3. 程序测试与调试在程序测试和调试阶段,while 1循环也具有重要作用。
它能够让程序持续执行,方便程序员观察程序行为、调试代码逻辑、捕捉异常等。
4. 无限循环有些情况下,程序需要执行一个没有终止条件的循环。
此时,while 1循环可派上用场,实现程序的无限循环。
四、while 1的注意事项1. 防止死循环使用while 1循环时,务必小心防止死循环。
在循环体内部必须有合适的跳出机制,否则程序将陷入无尽的循环之中,造成系统资源的浪费和程序的崩溃。
2. 兼顾性能和效率在使用while 1循环时,要注意兼顾性能和效率。
循环体内的代码应尽量简洁高效,避免不必要的计算和重复操作,以提高程序的执行效率。
3. 注意程序运行时间无限循环可能导致程序长时间运行,因此需特别留意程序的运行时间。
MATLAB应用实验指导书1234-结果************************ MATLAB语言实验指导书************************中国矿业大学信息与电气工程学院2014年3月实验一 MATLAB 工作环境熟悉及基本运算一、实验目的:熟悉MATLAB 的工作环境,学会使用MATLAB 进行一些简单的运算。
掌握基本的矩阵运算及常用的函数。
二、实验内容:MATLAB 的启动和退出,熟悉MATLAB 的桌面(Desktop ),包括菜单(Menu )、工具条(Toolbar )、命令窗口(Command Window)、历史命令窗口、工作空间(Workspace)等;完成一些基本的矩阵操作;学习使用在线帮助系统。
三、实验步骤:1、启动MATLAB ,熟悉MATLAB 的桌面。
2、在命令窗口执行命令完成以下运算,观察workspace 的变化,记录运算结果。
(1)(365-522-70) 3 = (2)area=pi*^2 = (3)已知x=3,y=4,在MATLAB 中求z :()232y x y x z -== 576 (4)将下面的矩阵赋值给变量m1,在workspace 中察看m1在内存中占用的字节数。
m1=11514412679810115133216 执行以下命令>>m1( 2 , 3 )=10 >>m1( 11 )=6>>m1( : , 3 )= 3 10 6 15>>m1( 2 : 3 , 1 : 3 )=[ 5 11 10;9 7 6]>>m1( 1 ,4 ) + m1( 2 ,3 ) + m1( 3 ,2 ) + m1( 4 ,1)=34(5)执行命令>>help abs查看函数abs 的用法及用途,计算abs( 3 + 4i )=5 (6)执行命令>>x=0::6*pi; >>y=5*sin(x); >>plot(x,y)(7)运行MATLAB 的演示程序,>>demo ,以便对MATLAB 有一个总体了解。
1.列主元高斯消去法M文件function[x]=gauss(a,b)n=length(a);x=zeros(n,1);a=[a b];for k=1:n-1max=k;for i=k+1:nif a(i,k)>a(max,k)max=i;endendtemp=a(k,k:n+1);a(k,k:n+1)=a(max,k:n+1);a(max,k:n+1)=temp;for i=k+1:na(i,k)=-a(i,k)/a(k,k);a(i,k+1:n+1)=a(i,k+1:n+1)+a(i,k)*a(k,k+1:n+1);endendx(n,1)=a(n,n+1)/a(n,n);for i=n-1:-1:1sum=0;for j=i+1:nsum=sum+x(j,1)*a(i,j);endx(i,1)=(a(i,n+1)-sum)/a(i,i);endMatlab运行结果2.LU三角分解法M文件function y=LU(A,B);n=length(A);A=[A B];for k=1:n-1;for i=k:n;if(abs(A(i,k))==max(abs(A(k:n,k))))P(k)=i;temp=A(k,:);A(k,:)=A(i,:);A(i,:)=temp;endendfor j=k+1:n;A(j,k)=A(j,k)/A(k,k);A(j,k+1:n+1)=A(j,k+1:n+1)-A(j,k)*A(k,k+1:n+1);endendP(n)=n;L(1,1)=1;L(2:n,1)=A(2:n,1);L(1,2:n)=0;U(1,1)=A(1,1);U(2:n,1)=0;U(1,2:n)=A(1,2:n);for i=2:n;L(i,1:i-1)=A(i,1:i-1);L(i,i)=1;L(i,i+1:n)=0;U(i,1:i-1)=0;U(i,i:n)=A(i,i:n);endx(n) = A(n,n+1)/U(n,n);for k = n-1:-1:1x(k)=A(k,n+1);for p=n:-1:k+1;x(k) = x(k)-U(k,p)*x(p); endx(k)=x(k)/U(k,k);endxLUPEndMatlab运行结果3.龙贝格(Romberg)算法M文件function[t]=romberg(f,a,b,e)t=zeros(15,4);t(1,1)=(b-a)/2*(f(a)+f(b));for k=2:4sum=0;for i=1:2^(k-2)sum=sum+f(a+(2*i-1)*(b-a)/2^(k-1));endt(k,1)=0.5*t(k-1,1)+(b-a)/2^(k-1)*sum;for i=2:kt(k,i)=(4^(i-1)*t(k,i-1)-t(k-1,i-1))/(4^(i-1)-1);endendfor k=5:15sum=0;for i=1:2^(k-2)sum=sum+f(a+(2*i-1)*(b-a)/2^(k-1));endt(k,1)=0.5*t(k-1,1)+(b-a)/2^(k-1)*sum;for i=2:4t(k,i)=(4^(i-1)*t(k,i-1)-t(k-1,i-1))/(4^(i-1)-1);endif k>6if abs(t(k,4)-t(k-1,4))<edisp(['答案',num2str(t(k,4))]);break;endendendif k>=15disp(['溢出']);endMatlab运行结果4.最小二乘法M文件function[a,max,det]=zuixiaoerchengfa(x,y,r) n=length(x);c=ones(n,r+1);for i=2:r+1for j=1:nc(j,i)=x(j)^(i-1);endendA=c'*c;b=c'*y';a=inv(A)*b;det=0;max=0;for i=1:nsum=a(1);for j=2:r+1sum=sum+a(j)*x(i)^(j-1);endcc=abs(y(i)-sum);if cc>maxmax=cc;enddet=det+cc^2;enddet=sqrt(det);Matlab运行结果§2.1.1 二分法的MATLAB主程序function [k,x,wuca,yx]=erfen(a,b,abtol)a(1)=a; b(1)=b;ya=fun(a(1)); yb=fun(b(1)); %程序中调用的fun.m 为函数if ya* yb>0,disp('注意:ya*yb>0,请重新调整区间端点a和b.'), returnendmax1=-1+ceil((log(b-a)- log(abtol))/ log(2)); % ceil是向+方向取整∞for k=1: max1+1a;ya=fun(a); b;yb=fun(b); x=(a+b)/2;yx=fun(x); wuca=abs(b-a)/2; k=k-1;[k,a,b,x,wuca,ya,yb,yx]if yx==0a=x; b=x;elseif yb*yx>0b=x;yb=yx;elsea=x; ya=yx;endif b-a< abtol , return, endendk=max1; x; wuca; yx=fun(x);§2.1.2 不动点迭代法的MATLAB主程序function [k,piancha,xdpiancha,xk,yk]=diedai2(x0,tol,ddmax) x(1)=x0;for i=1: ddmaxx(i+1)=fun(x(i));piancha=abs(x(i+1)-x(i));xdpiancha=piancha/( abs(x(i+1))+eps);i=i+1;xk=x(i);yk=fun(x(i)); [(i-1) piancha xdpiancha xk yk]if (piancha<tol)|(xdpiancha< tol)k=i-1; xk=x(i);return;endendif i>ddmaxdisp('迭代次数超过给定的最大值ddmax')k=i-1; xk=x(i);yk=fun(x(i));[(i-1) piancha xdpiancha xk yk];return;endP=[(i-1),piancha,xdpiancha,xk,yk]';§2.1.3 艾特肯加速迭代法的MATLAB主程序function [k,xk,yk,p]= Aitken (x0,tol, ddmax)x(1)=x0;for i=1: ddmaxx1(i+1)=fun(x(i)); x2(i+1)=fun(x1(i+1));x(i+1)=x2(i+1)-(x2(i+1)-x1(i+1))^2/(x2(i+1)-2*x1(i+1)+ x(i));piancha=abs(x(i+1)-x(i));xdpiancha= piancha/( abs(x(i+1))+eps);i=i+1; xk=x(i);yk=fun(x(i));if (piancha<tol)|(xdpiancha<tol)k=i-1; xk=x(i); yk=fun(x(i));m=[0,1:i-1]; p=[m',x1',x2',x'];return;endendif i>ddmaxdisp('迭代次数超过给定的最大值ddmax')k=i-1; xk=x(i); yk=fun(x(i));m=[0,1:i-1]; p=[m',x1',x2',x'];return;endm=[0,1:i-1]; p=[m',x1',x2',x'];§2.1.4 牛顿切线法的MATLAB主程序function [k,xk,yk,piancha,xdpiancha]=newtonqx(x0,tol,ftol,gxmax) x(1)=x0;for i=1: gxmaxx(i+1)=x(i)-fun(x(i))/(dfun(x(i))+eps);piancha=abs(x(i+1)-x(i));xdpiancha= piancha/( abs(x(i+1))+eps); i=i+1;xk=x(i);yk=fun(x(i)); [(i-1) xk yk piancha xdpiancha]if (abs(yk)<ftol)&((piancha<tol)|(xdpiancha< tol))k=i-1; xk=x(i);[(i-1) xk yk piancha xdpiancha]return;endendif i>gxmaxdisp('请注意:迭代次数超过给定的最大值gxmax。
function varargout = keshe(varargin)gui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @keshe_OpeningFcn, ...'gui_OutputFcn', @keshe_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before keshe is made visible.function keshe_OpeningFcn(hObject, eventdata, handles, varargin)handles.output = hObject;guidata(hObject, handles);% --- Outputs from this function are returned to the command line. function varargout = keshe_OutputFcn(hObject, eventdata, handles)varargout{1} = handles.output;function edit1_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit1_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));endfunction edit2_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit2_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));end% --- Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)axes(handles.axes1);claa=get(handles.edit1,'string');b=get(handles.edit2,'string');aa=tf(str2num(a),str2num(b));axes(handles.axes1);rlocus(aa,'r');%set(handles.edit5,'string',[]);% --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)a=get(handles.edit1,'string');b=get(handles.edit2,'string');axes(handles.axes2);sys=tf(str2num(a),str2num(b));sys=feedback(sys,1);pzmap(sys)%••••••••••••••••••••% --- Executes on button press in pushbutton3.function pushbutton3_Callback(hObject, eventdata, handles)set(handles.edit1,'string',[]);set(handles.edit2,'string',[]);set(handles.edit5,'string',[]);set(handles.edit3,'string',[]);set(handles.edit4,'string',[]);set(handles.edit6,'string',[]);set(handles.edit7,'string',[]);set(handles.edit11,'string',[]);set(handles.edit8,'string',[]);set(handles.edit12,'string',[]);set(handles.edit9,'string',[]);set(handles.edit10,'string',[]);set(handles.edit13,'string',[]);axes(handles.axes1)claaxes(handles.axes2)cla% --- Executes on button press in pushbutton4.function pushbutton4_Callback(hObject, eventdata, handles)a=get(handles.edit1,'string');b=get(handles.edit2,'string');aa=tf(str2num(a),str2num(b));num=aa.num{1};den=aa.den{1};p=roots(den);z=roots(num);n=length(p);m=length(z);axes(handles.axes1)claaxes(handles.axes1);rlocus(aa,'r');hold onif n>ms=(sum(p)-sum(z))/(n-m);set(handles.edit5,'string',s);sd=[];if nargout<1for i=1:n-msd=[sd,s];endsysa=zpk([],sd,1);hold on;[r,k]=rlocus(sysa);for i=1:n-mplot(real(r(i,:)),imag(r(i,:)),'g:');endendend% --- Executes on button press in pushbutton5.function pushbutton5_Callback(hObject, eventdata, handles)a=get(handles.edit1,'string');b=get(handles.edit2,'string');aa=tf(str2num(a),str2num(b));sys=feedback(aa,1);%axes(handles.axes2);[p,z]=pzmap(sys);if real(p)<0set(handles.edit3,'string','YES')else set(handles.edit3,'string','NO')endfunction edit3_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit3_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));End% --- Executes on button press in pushbutton6.function pushbutton6_Callback(hObject, eventdata, handles)close% --- Executes on button press in pushbutton7.function pushbutton7_Callback(hObject, eventdata, handles)a=get(handles.edit1,'string');b=get(handles.edit2,'string');sys=tf(str2num(a),str2num(b));[K,s]=breakin(sys);if isempty(s)set(handles.edit4,'string','No Unit');elsett=num2str(s);set(handles.edit4,'string',tt);endfunction [K,s]=breakin(sys)sys=tf(sys);num=sys.num{1};den=sys.den{1};a=conv(den,polyder(num));b=conv(num,polyder(den));dk_ds=([zeros(1,length(b)-length(a)),a]-b);ss=roots(dk_ds);K=[];s=[];syms nums densfor i=1:length(ss)nums=poly2sym(num,'s');%得到分子多项式表达式dens=poly2sym(den,'s');Kk(i)=-subs(dens/nums,'s',ss(i));%得到某分离点所对应增益值if Kk(i)>0K=[K,Kk(i)];s=[s,ss(i)];%舍去负增益对应分离点elseS='木有分离点';endendfunction edit4_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit4_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));endfunction edit5_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit5_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));end% --- Executes on button press in pushbutton9.function pushbutton9_Callback(hObject, eventdata, handles)a=get(handles.edit1,'string');b=get(handles.edit2,'string');aa=tf(str2num(a),str2num(b));[K,Wcg]=imwk(aa);if isempty(Wcg)set(handles.edit6,'string','NULL');set(handles.edit7,'string','NULL');elsegg=num2str(Wcg);mm=num2str(K);set(handles.edit6,'string',gg);set(handles.edit7,'string',mm);end[K0,Wcg0]=imwk(aa);kt=num2str(K0);set(handles.edit8,'string',kt);set(handles.edit11,'string','0');[K1,st]=breakin(aa);kz=num2str(K1);set(handles.edit12,'string',kz);[K2,Wcg2]=imwk(aa);kzz=num2str(K2);set(handles.edit9,'string',kzz);[K3,s]=breakin(aa);set(handles.edit13,'string','0');kw=num2str(K3);set(handles.edit10,'string',kw);function [K,Wcg]=imwk(sys)sys=tf(sys);num=sys.num{1};den=sys.den{1};asys=allmargin(sys);Wcg=asys.GMFrequency;K=asys.GainMargin;function edit6_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit6_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));endfunction edit7_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit7_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));endfunction edit8_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit8_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));endfunction edit9_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit9_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));endfunction edit10_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit10_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));endfunction edit11_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit11_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));endfunction edit12_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit12_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));endfunction edit13_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit13_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));endfunction edit14_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit14_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));endfunction edit15_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit15_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));endfunction edit16_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit16_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));end% --- Executes on button press in pushbutton10.function pushbutton10_Callback(hObject, eventdata, handles)global mmc=get(handles.edit14,'string');a=get(handles.edit15,'string');b=get(handles.edit16,'string');Gk=tf(str2num(a),str2num(b));switch mmcase 1Gkd=c2d(Gk,str2num(c),'zoh');case 2Gkd=c2d(Gk,str2num(c),'foh');endset(handles.pushbutton12,'Enable','on');axes(handles.axes3);rlocus(Gkd);% --- Executes on button press in pushbutton11.function pushbutton11_Callback(hObject, eventdata, handles)set(handles.edit14,'string',1);set(handles.edit15,'string',[]);set(handles.edit16,'string',[]);set(handles.edit17,'string',[]);set(handles.edit18,'string',[]);set(handles.pushbutton12,'Enable','off');set(handles.pushbutton13,'Enable','off');axes(handles.axes3)cla% --- Executes on button press in pushbutton12.function pushbutton12_Callback(hObject, eventdata, handles)global kk;c=get(handles.edit14,'string');a=get(handles.edit15,'string');b=get(handles.edit16,'string');Gk=tf(str2num(a),str2num(b));Gkd=c2d(Gk,str2num(c));[kk,poles]=rlocfind(Gkd);set(handles.pushbutton13,'Enable','on');% --- Executes on button press in pushbutton13.function pushbutton13_Callback(hObject, eventdata, handles)global kk;set(handles.edit18,'string',kk);set(handles.edit17,'string','0');function edit17_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit17_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));endfunction edit18_Callback(hObject, eventdata, handles)% --- Executes during object creation, after setting all properties. function edit18_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));end% --- Executes on selection change in popupmenu1.function popupmenu1_Callback(hObject, eventdata, handles)global mmmm=get(hObject,'Value');% --- Executes during object creation, after setting all properties. function popupmenu1_CreateFcn(hObject, eventdata, handles)if ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundCo lor'));end。