MATLAB工具软件实验(1)
(1)生成一个4×4的随机矩阵,求该矩阵的特征值和特征向量。
程序:
A=rand(4)
[L,D]=eig(A)
结果:
A =
0.9501 0.8913 0.8214 0.9218
0.2311 0.7621 0.4447 0.7382
0.6068 0.4565 0.6154 0.1763
0.4860 0.0185 0.7919 0.4057
L =
-0.7412 -0.2729 - 0.1338i -0.2729 + 0.1338i -0.5413
-0.3955 -0.2609 - 0.4421i -0.2609 + 0.4421i 0.5416
-0.4062 -0.0833 + 0.4672i -0.0833 - 0.4672i 0.4276
-0.3595 0.6472 0.6472 -0.4804
D =
2.3230 0 0 0
0 0.0914 + 0.4586i 0 0
0 0 0.0914 - 0.4586i 0
0 0 0 0.2275
(2)给出一系列的a值,采用函数
22
22
1
25
x y
a a
+=
-
画一组椭圆。
程序:
a=0.5:0.5:4.5; % a的绝对值不能大于5
t=[0:pi/50:2*pi]'; % 用参数t表示椭圆方程
X=cos(t)*a;
Y=sin(t)*sqrt(25-a.^2);
plot(X,Y)
结果:
(3)X=[9,2,-3,-6,7,-2,1,7,4,-6,8,4,0,-2],
(a)写出计算其负元素个数的程序。
程序:
X=[9,2,-3,-6,7,-2,1,7,4,-6,8,4,0,-2];
L=X<0;
A=sum(L)
结果:
A =
5
(b ) 写出一段程序,使其能够找出向量x 中的最大、最小元素。
(不能使用min 和max 命令)
程序:
X=[9,2,-3,-6,7,-2,1,7,4,-6,8,4,0,-2];
xmin=999;xmax=-999;
for i=1:length(X)
if xmin>X(i)
xmin=X(i);
end
if xmax<X(i)
xmax=X(i);
end
end
[xmin,xmax]
结果:
ans =
-6 9
(4) 方波函数为f(t)=]0,[],0[11ππ-∉∈⎩⎨⎧-t t , 利用0sin(21)()21n n t f t n ∞
=+=+∑,用MATLAB 编程和绘图说明方波是奇次谐波的叠加。
程序:
k=1000; % k 值可以改动
x=-pi:0.0001:pi;
y=sin(x);
for n=1:k
y=y+sin((2*n+1)*x)/(2*n+1);
end
plot(x,y)
结果:
MATLAB 工具软件实验(2)
(1) 应用simulink 实现下列系统的仿真,并试试改变信号源后的结果。
略。
(2)利用simulink 解二阶微分方程
0.20.40.2x x x u t '''++= 其中 u(t)为单位阶跃函数
框图:
结果:
(3)求矩阵11122122a a A a a ⎡⎤=⎢⎥⎣⎦
的行列式、逆和特征值。
程序:
syms a11 a12 a21 a22;
A=[a11,a12;a21,a22]
B=det(A),C=inv(A),D=eig(A)
结果:
A =
[ a11, a12]
[ a21, a22]
B =
a11*a22-a12*a21
C =
[ a22/(a11*a22-a12*a21), -a12/(a11*a22-a12*a21)]
[ -a21/(a11*a22-a12*a21), a11/(a11*a22-a12*a21)]
D =
1/2*a11+1/2*a22+1/2*(a11^2-2*a11*a22+a22^2+4*a12*a21)^(1/2)
1/2*a11+1/2*a22-1/2*(a11^2-2*a11*a22+a22^2+4*a12*a21)^(1/2)
(4)简化()f x =
程序:
syms x ; f=(x^(-3)+6*x^(-2)+12*x^(-1)+8)^(1/3);
g1=simple(f)
g2=simple(g1)
结果:
g1 =
(2*x+1)/x
g2 =
2+1/x
(5)计算积分
2
()22ax dx ∞-⎰ 程序: syms x ;
syms a real ; % 将a 定义为实符号变量
f=2^(-(a*x)^2)
int(f,x,2,inf)
结果:
f =
2^(-a^2*x^2)
ans =
-1/2*pi^(1/2)*(-signum(a)+erf(2*a*log(2)^(1/2)))/a/log(2)^(1/2)
MATLAB工具软件实验(3)
(1)对函数()
f x=,用梯形法、辛普森法计算积分:
2 0()
S f x dx
=⎰,并讨论分割区间数n对误差的影响。
其中S的精确值取
为4.006994。
程序:
format long
n=10:10:100;
for ii=1:10
x=0:2/n(ii):2;
f=sqrt(1+exp(x));
s=trapz(x,f)
err(ii)=4.006994-s;
end
figure(1)
plot(n,err)
f1=inline('sqrt(1+exp(x))');
quad(f1,0,2)
结果:
s =
4.00702495691834
ans =
4.00699422414512
Figure 1
(2)对x=[0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1];
y=[-0.447 1.978 3.28 6.16 7.08 7.34 7.66 9.56 9.48 9.30 11.2];进行线性,三阶多项式插值。
程序:
x=[0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1];
y=[-0.447 1.978 3.28 6.16 7.08 7.34 7.66 9.56 9.48 9.30 11.2];
xi=0:0.01:1;
yi=interp1(x,y,xi);
y3=interp1(x,y,xi,'cubic');
plot(x,y,'go',xi,yi,'r',xi,y3,'b:')
legend('y','线性插值','三阶多项式插值',4)
结果:
(3)从图像处理工具箱中装载图像数据bacteria,完成对该图像的滤噪、边缘提取的等处理工作,达到对图像中细胞的增强和识别的目的。
程序:
I=imread('cell.tif'); %读入图像
J1=imnoise(I,'gaussian',0,0.02) % 对图像数据添加均值为0,方差为0.02的高斯噪声figure(1)
subplot(2,2,1),imshow(I)
subplot(2,2,2),imshow(J1)
h=[1,1,1;1,1,1;1,1,1]; %产生滤波模板
h=h/9;
J2=conv2(J1,h);
J3=medfilt2(J1);
subplot(2,2,3),imshow(J2,[]);
subplot(2,2,4),imshow(J3)
b=edge(I);
figure(2)
subplot(2,2,1);imshow(b)
b=edge(I,'roberts');
subplot(2,2,2);imshow(b)
c=edge(I,'sobel')
subplot(2,2,3);imshow(c)
结果:
Figure 1
Figure 2
(4)装载图像数据woman2,对该图像进行二维DCT变换,通过对变换后的一些系数值零的方法进行图像数据压缩,并比较压缩的效果。
程序:
load woman
figure(1)
imshow(X,map)
b=dct2(X);
figure(2)
imshow(log(abs(b)),[])
colormap(jet(64));
colorbar
b(abs(b)<10)=0; %将DCT变换值小于10的元素置为0
K=idct2(b)/255; %对逆DCT变换归一化
figure(3)
imshow(K)。