第二章
1、
x=[2,4];
y=x.^3+(x-0.98).^2./(x+1.35).^3-5*(x+1./x) 2、
y=cos(pi/3)-(9-sqrt(2))^(1/3)
3、
a=3;A=4; b=a.^2; B=b.^2-1; c=a+A-2*B; C=a+2*B+c
4、x=[1 2 3;4 5 6;7 8 9]
Desktop->Workspace,双击变量x
5、 clear
x=magic(3) y=randn(3,3) xy=[x,y] yx=[x,y] z=xy(:,1:2) 6、 clear
x=eye(4,4)
y=triu(x) 7、 clear
x=rand(4,5) y=x([1,2],:) z=(y>=0.3).*y 8、 clear
x=randn(5,5) y=inv(x) 9、 clear
x=randn(5,5) z=x^5 10、 clear
A=[1,4,8;-3,6,-5;2,-7,-12]; B=[5,4,3;6,-2,3;-1,3,-9]; C=A*B D=A.*B 11、 clear
x=linspace(0,2*pi,125);
y=cos(x).*(0.5+3*sin(x)./(1+x.^2)); plot(x,y)
12、
z=-45:1:45;
x=z.*sin(3*z);y=z.*cos(3*z); plot3(x,y,z);
13、
x=-2:0.1:2,y=x;
[x,y]=meshgrid(x,y); z=x.^2.*exp(-x.^2-y.^2) surf(x,y,z);
14、
x=-2:0.1:2,y=x;
[x,y]=meshgrid(x,y); z=x.^2.*exp(-x.^2-y.^2); surf(x,y,z);
z1=0.05*x-0.05*y+0.1; hold on,mesh(x,y,z1);
15、 (1)
n=2;alfa=0;
t=0:0.1:10;x=cos(t);y=sin(n*t+alfa); subplot(2,2,1);plot(t,x,t,y); (2)
n=2;alfa=0;
t=0:0.1:10;x=cos(t);y=sin(n*t+alfa); subplot(2,2,1);plot(t,x,t,y); n=2;alfa=pi/3;
t=0:0.1:10;x=cos(t);y=sin(n*t+alfa);
subplot(2,2,2);plot(t,x,t,y); n=2;alfa=pi/2;
t=0:0.1:10;x=cos(t);y=sin(n*t+alfa); subplot(2,2,3);plot(t,x,t,y); n=2;alfa=pi;
t=0:0.1:10;x=cos(t);y=sin(n*t+alfa); subplot(2,2,4);plot(t,x,t,y);
球体 clear
for i=1:100
for j=1:100
x(i,j)=i/100*cos(j*2*pi/100); y(i,j)=i/100*sin(j*2*pi/100);
z(i,j)=sqrt(1.001-x(i,j)^2-y(i,j)^2); end end
surf(x,y,z);hold on;surf(x,y,-z);axis equal
棱锥
clear
for i=1:100
for j=1:100
x(i,j)=i/100*cos(j*2*pi/4); y(i,j)=i/100*sin(j*2*pi/4); z(i,j)=sqrt(x(i,j)^2+y(i,j)^2); end end
surf(x,y,-z);
求最大值
x=[67 87 56 99 43] max=0; for i=1:5
if max>=x(i) max=max; else max=x(i) end end max
求最小值
x=[67 87 56 99 43] min=inf; for i=1:5
if min<=x(i) min=min; else min=x(i) end end min
求和
x=[67 87 56 99 43]; sum=0; for i=1:5
sum=sum+x(i) end sum
第三章
1、
h0=[446,714,950,1422,1634]; t0=[7.04,4.28,3.40,2.54,2.13]; t1=interp1(h0,t0,500,'linear')
2、
x0=[1,0,-1];y0=[0,1,0]; p=polyfit(x0,y0,3); x=-1:0.1:1;
y=polyval(p,x);
plot(x,y,-x,-y),axis equal
3、
clear
x0=[1.0,1.1,1.2,1.3,1.4];
y0=[0.25,0.2268,0.2066,0.1890,0.1736]; p=polyfit(x0,y0,3); p1=polyder(p);
y=polyval(p1,[1.0,1.2]) 4、
p=[3 4 7 2 9 12]; r=roots(p) 5、
r=[-3 -5 -8 -9];
p=poly(r)
6、
function ypie=fun1(x,y) ypie=x.^2./y-x.*cos(y);
[x,y]=ode45('fun1',[0,5],1) plot(x,y)
7、
function y=fun2(x)
y=x.^4-3*x^3+5*cos(x)+8;
fplot('fun2',[1,5])
hold on;x=0:5;y=0*x;plot(x,y) a=fzero(@fun2,2) b=fzero(@fun2,3)
8、
a=[2 4 9;4 2 4;9 4 18]; [v,gama]=eig(a)
9、
function xpie=fun3(t,x) f=exp(-t)
xpie=[0 1 0 0;1 0 0 -1;0 0 0 1;0 -1 -1 0]*x+[0;1;0;0]*f
[t,x]=ode45('fun3',[0,5],[0 0 0 0]'); plot(t,x(:,1),'b',t,x(:,3),'r')
10、
function f=fun4(x,y) f=4*(x-y)-x.^2-y.^2;
v=-2:0.2:2;
[x,y]=meshgrid(v) z=fun4(x,y); surf(x,y,z)
求极小值 (有误) function f=fun5(x,y) x=v(1);y=v(2)
f=-(4*(x-y)-x.^2-y.^2);
v=-50:2:50;
[x,y]=meshgrid(v) z=fun4(x,y); surf(x,y,z)
min=fminsearch('fun4',[0,0])
11、
function f=fun6(x,y) f=sin(y)+exp(x)-x.*y.^2
ezplot('fun6')
12、
function f=fun7(x,y)
f=(x-y).^2.*(sin(x+y)).^2
s=dblquad('fun7',pi,2*pi,0,pi)
14、
a=randn(4,4) [l,u]=lu(a)
a=randn(4,4)
[u,gama,v]=svd(a)
15、
b=[3 0.5 4];a=[3 4 5 6]; [r,p,k]=residue(b,a)
b=[3 0.5 4];a=[3 4 5 6]; [r,p,k]=residue(b,a) t=0:0.1:100;y=0*t; for i=1:3
y=y+r(i)*exp(p(i)*t) end
plot(t,y)
b=[3 0.5 4];a=[3 4 5 6]; impulse(b,a)
b=[3 0.5 4];a=[3 4 5 6]; step(b,a)
第四章
1、
n=1:11;x=cos(pi/6*n); subplot(2,1,1);stem(x); y=abs(fft(x));
subplot(2,1,2);stem(y);
clear
n=0:11;x=cos(pi/6*n); subplot(2,1,1);stem(x); y=abs(fft(x));
subplot(2,1,2);stem(y); 2、(有误)
n=0:19;x=5*0.6.^n;
subplot(3,1,1);stem(n,x); for i=-20:39
xl(i+21)=x(mod(i+40,20)+1); end
n1=-20:39;
subplot(3,1,2);stem(n1,x1); x2=x1(10:29);
subplot(3,1,3);stem(n,x2)
新方法
m=10; e=0:19;
c=0.6;k=5; a=k*c.^e;
a=a';
b=circshift(a,m); L=length(a)-1; n=0:L;
subplot(2,1,1); stem(n,a);
axis([0,L,min(a),max(a)]); subplot(2,1,2); stem(n,b);
axis([0,l,min(a),max(a)])
3、
x=[0.8 0.8 0.8 0.8 0.8 0.8 0.8 0.8 0.8 0.8 0.8 0.8 0 0 0 0 0 0 0 0];
h=[1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0] y=conv(x,h); stem(y) 4、
x=[0.8 0.8 0.8 0.8 0.8 0.8 0.8 0.8 0.8 0.8 0.8 0.8 0 0 0 0 0 0 0 0];
h=[1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0] y=conv(x,h);
subplot(2,1,1);stem(y)
xx=fft(x); hh=fft(h);
yy=ifft(xx.*hh);
subplot(2,1,2);stem(yy) 5、
b=[2 3];a=[1 0.4 1]; [z p k]=tf2zp(b,a)
6、
b=[4 15.6 6 2.4 -6.4]; a=[3 2.4 6.3 -11.4 6]; [z p k]=tf2zp(b,a) 7、 b=[18];
a=[18 3 -4 -1];
[r p k]=residue(b,a) 8、
b=[0.2 0.3 1];a=[1 0.4 1]; freqs(b,a) 9、
Fs=2000;wc=[100 200]/(Fs/2);N=10; [b,a]=butter(N,wc,'z'); %freqz(b,a)
dimpulse(b,a,100) 10、
wp=100;ws=200;rp=2;rs=15;Fs=500; wp=wp/(Fs/2);ws=ws/(Fs/2); [N,wc]=buttord(wp,ws,rp,rs); [b,a]=butter(N,wc,'z') freqz(b,a) 11、
b=fir1(48,[0.35 0.65]);a=1; freqz(b,a)
12、
b=fir1(37,0.3);a=1; freqz(b,a) 13、 低通:
F=[0:1/56:1];
A=[ones(1,50),zeros(1,56-49)]; b=fir2(56,F,A);a=1; freqz(b,a)
带通:
F=[0:1/56:1];
A=[ones(1,25),zeros(1,25),ones(1,7)]; b=fir2(56,F,A);a=1; freqz(b,a)
高通
F=[0:1/56:1];
A=[zeros(1,50),ones(1,56-49)]; b=fir2(56,F,A);a=1; freqz(b,a)
%滤波器设计(椭圆滤波器)
wp1=300;wp2=700;ws1=301;ws2=699; rp=0.1;rs=50;Fs=2000;
wp=[wp1]/(Fs/2);ws=[ws1]/(Fs/2); [N,wc]=ellipord(wp,ws,rp,rs,'z') [b,a]=ellip(N,rp,rs,wc,'high')
%滤波器特性分析
freqz(b,a)
%信号采样及时频变换 Fs=2000;t=0:1/Fs:10;
yt=sin(2*pi*200*t)+sin(2*pi*500*t)+sin(2*pi*800*t); figure(2);subplot(3,1,1);plot(t,yt)
figure(2);subplot(3,1,2);plot(t,yt);axis([0,0.1,-5,5]) yf=abs(fft(yt));
f=(1:length(yf))/length(yf)*Fs; figure(2);subplot(3,1,3);plot(f,yf) %信号滤波及时频分析 yt1=filter(b,a,yt);
figure(3);subplot(3,1,1);plot(t,yt1)
figure(3);subplot(3,1,2);plot(t,yt1);axis([0,0.1,-5,5]) yf1=abs(fft(yt1));
f=(1:length(yf1))/length(yf1)*Fs; figure(3);subplot(3,1,3);plot(f,yf1)