实验 1
1、ones语句:Y = ones(n)
%生成n×n 全1 阵
Y = ones(m,n) %生成m×n 全1 阵
Y = ones([m n]) %生成m×n 全1 阵
Y = ones(d1,d2,d3…) %生成d1×d2×d3×…全1
阵或数组
Y = ones([d1 d2 d3…]) %生成d1×d2×d3×…全1
阵或数组
Y = ones(size(A)) %生成与矩阵A 相同大小的全1 阵
2、find语句:k = find(x) %按行检索X
中非零元素的点,若没有非零元素,将返回空矩阵。
[i,j] = find(X) %检索X 中非零元素的行标i
和列标j。
[i,j,v] = find(X) %检索X 中非零元素的行标i 和列标j
以及对应的元素值v。
实验 3
1、编写一M函数,a和x作为函数参数输入,函数里面分别用if结构实现函数表示
function output=function1(x,a)
result=0;
if x<=-a
result=-1;
elseif
x>-a&x
result=x/a;
else x>=a
result=1;
end
output=[result];
2、编写一M函数,迭代计算
,给出可能的收敛值,其中x的初值作为函数的参数输入。
function output=function2(x)
y=0;
while 1
y=3/(x+2);
if abs(y-x)<0.000001
break;
else x=y;
end
end
output=[y];
end
3、编写一M函数,实现
近似计算指数,其中x为函数参数输入,当n+1步与n步的结果误差小于0.00001时停止,分别用for和while
结构实现。
用for结构实现:
function output=function3(x)
e=0;s=0;i=0;
while 1
e=s+x^i/factorial(i);
if abs(e-s)<0.000001
break;
else s=e; i=i+1;
end
end
output=[e];
用while 结构实现:
function output=function4(x)
s=1;e=0;
for i=1: 1000000
e=s+x^i/factorial(i);
if abs(e-s)<0.00001
break;
else s=e;
end
end
output=[e];
实验 4
1. 绘制函数
的曲线,其中曲线为绿虚线,并进行标注
x=-2:0.1:1;
y=x.^2;
plot(x,y,'--g');
hold on
x=1:0.1:2;
y=exp(-(x-1).^2);
plot(x,y,'--g')
text(-1,1,'曲线y1=x^2');
text(1.5,0.5,'曲线y1=e^(-(x-1)^2)');
2.
将用于绘制曲线 的数据分别保存成MAT、二进制的文本文件中
t=[0:pi/10:2*pi];
x=[sin(t) sin(t)];
y=[cos(t) cos(t)];
z=[sin(t).*cos(t)];
save mydatafile x y z
clear
load mydatafile
d=z
t=[0:pi/10:2*pi];
x=[sin(t)];
y=[cos(t)];
z=[ sin(t).* cos(t)];
fid=fopen('text.txt','w')
count=fwrite(fid,z,'float32')
closestatus=fclose(fid)
3.
重启Matlab,从上述保存的文件中依次读取变量z的前10个数据
fid=fopen('mydatafile.mat','r')
data1=fread(fid,10)
fid=fopen('text.txt','r');
data2=fread(fid,10)
实验 5
1.
将多项式A的系数向量形式[1 3 6 3
1]转换为完整形式,并求其根。同时在0-5内随机产生150组自变量,计算他们的对应取值;
A=[1 3 6 3 1];
[s,len]=poly2str(A,'x')
求根:
r=roots(A)
产生0-5内150组自变量:
q=5*rand(1,150)
计算对应取值:
w=polyval(A,q)
2.
对于上述150组数据,采用多项式进行拟合,并对
分别采用最邻近、双线性和三次样条插值方法进行插值;
x=1:4;
y=x.^4 + 3.* x.^3 + 6.* x.^2 + 3.* x +
1 ;
xi=1:0.1:10;
yi_nearest=interp1(x,y,
xi,'nearset');
yi_linear=interp1(x,y, xi);
yi_spline=interp1(x,y,
xi,'spline');
figure;
hold on;
subplot(1,3,1);
plot(x,y,'ro',xi, yi_nearest,'b-');
title('最邻近插值');
subplot(1,3,2);
plot(x,y,'ro',xi, yi_linear,'b-');
title('线性插值');
subplot(1,3,3);
plot(x,y,'ro',xi, yi_spline,'b-');
title('三次样条插值');
3、 计算
syms x y
t=int(y*exp(x),y,2*y)
int(t,1,2)
实验 6
1、连续信号的产生与可视化,直流信号、正弦交流信号、单位阶跃信号、单位冲击信号、符号信号、斜坡信号、单位衰减信号、复指数信号等实现以及可视化
直流信号:
x1=[-5:0.01:0];
y1=1;
plot(x1,y1);
hold on
x2=[0:0.01:5];
y2=1;
plot(x2,y2)
正弦交流信号:
x=[0:0.01:2*pi];
y=sin(x);
plot(x,y)
单位阶跃信号:
t=-4:0.01:4;
f=(t>0);
stairs(t,f);
axis([-4,4,-1.1,1.1]);
单位冲击信号:
t=-4:0.01:4;
n=length(t);
f=zeros(1,n);
f(1,(-t0+4)/0.01+1)=1;
plot(t,f);
axis([-4,4,-1.1,1.1]);
符号信号:
t=-4:0.01:4;
f=sign(t);
plot(t,f);
axis([-4,4,-1.1,1.1]);
斜坡信号:
t=0:0.01:4;
f=t;
plot(t,f);
axis([0,4,0,4]);
单位衰减信号:
t=0:0.01:10;
y=10*exp(-10*t).*sin(10*t);
plot(t,y);
复指数信号:
t=-10:0.1:10;
y=exp(10+j*t);
plot(t,y);
2、将信号 与信号
进行加、减、乘运算,并且将结果可视化。
加法运算:
t=-10:0.1:10;
y=exp(-3*t)+0.2*sin(4*pi*t);
plot(t,y);
减法运算:
t=-10:0.1:10;
y=exp(-3*t)-0.2*sin(4*pi*t);
plot(t,y);
乘法运算:
t=-10:0.1:10;
y=(exp(-3*t)).*(0.2*sin(4*pi*t));
plot(t,y);
3已知信号
,试通过反褶、移位、尺度变换由 的波形得到 的波形。
定义符号函数f(t)=sin(t)/t:
syms t;
f=sym('sin(t)/t');
对f进行移位:
f1=subs(f,t,t+3);
对f1进行尺度变换:
f2=subs(f1,t,2*t);
对f2进行反褶:
f3=subs(f2,t,-t);
绘制函数波形:
subplot(2,2,1);ezplot(f,[-8,8]);grid
on;
subplot(2,2,2);ezplot(f1,[-8,8]);grid
on;
subplot(2,2,3);ezplot(f2,[-8,8]);grid
on;
subplot(2,2,4);ezplot(f3,[-8,8]);grid
on;
4、分别产生两个方波信号,并且求这两个方波的卷积。
产生两个方波:
y1=[ones(1,10),zeros(1,20)];
y2=[ones(1,30),zeros(1,20)];
两个方波卷积:
y=conv(y1,y2);
n1=1:length(y1);
n2=1:length(y2);
L=length(y);
n=1:L;
%显示波形:
subplot(3,1,1);plot(n1,y1);axis([1,L,0,2]);
subplot(3,1,2);plot(n2,y2);axis([1,L,0,2]);
subplot(3,1,3);plot(n,y);axis([1,L,0,20]);