对于已经接触过matlab编程软件的留学生们来说,它能够帮助我们实现在算法开发、数据分析以及交互环境等多方面的效果,在matlab当中我们能够将不同的语句调用到一起来执行,这也是我们所说的函数,那么在实际的操作当中,我们面对着较多不同的变量需要采用的不同的算法,我们要如何去自定义函数调用呢?本文致远及哦啊与教育就根据网络资料为各位带了一些比较常见的8个函数调用实例解析,不会的同学们赶紧学起来哦。
1.三角波产生器
t=-3:0.01:3;
f1=tripuls(t);
subplot(3,1,1);
plot(t,f1);
axis([-3,3,-0.2,1.2])
set(gcf,'color','w');
f2=tripuls(t,4);
subplot(3,1,2);
plot(t,f2);
axis([-3,3,-0.2,1.2])
%set(gcf,'color','w');
f3=tripuls(t,4,-1);
subplot(3,1,3);
plot(t,f3);
axis([-3,3,-0.2,1.2])
2.离散序列的相加与相乘
function[x,n]=jxl(x1,x2,n1,n2)
n=min(min(n1),min(n2)):max(max(n1),max(n2));
s1=zeros(1,length(n));s2=s1;
s1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
s2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
x=s1+s2;//x=s1.*s2:%序列乘
axis([(min(min(n1),min(n2))-1),(max(max(n1),max(n2))+1),(min(x)-0.5),(max(x)+0.5)])
3.序列的反摺
function[x,n]=xlfz(x1,n1)
x=fliplr(x1);n=fliplr(n1);
stem(n,x,'filled')
axis([min(n)-1,max(n)+1,min(x)-0.5,max(x)+0.5])
4.序列的卷积
function[x,n]=gghconv(x1,x2,n1,n2)
x=conv(x1,x2)
ns=n1(1)+n2(1);
leg=length(x1)+length(x2)-2;
n=ns:(ns+leg)
subplot(2,2,1)
stem(n1,x1,'filled')
title('x1(n)')
xlabel('n')
subplot(2,2,2)
stem(n2,x2,'filled')
title('x2(n)')
xlabel('n')
subplot(2,2,3)
stem(n,x,'filled')
title('x(n)=n1(n)+x2(n)')
xlabel('n')
p=get(gca,'position');
p(3)=2.4*p(3);
set(gca,'position',p)
5.l连续函数的卷积
function[f,t]=gggfconv(f1,f2,t1,t2)
d=input('??ê?è?2é?ù????d:');
f=conv(f1,f2)
f=f*d;
ts=t1(1)+t2(1);
l=length(f1)+length(f2)-2;
t=ts:d:(ts+l*d);
subplot(2,2,1)
plot(t1,f1)
axis([min(t1),max(t1),min(f1)-min(f1)*0.2,max(f1)+max(f1)*0.2])
title('f1(t)')
xlabel('t')
subplot(2,2,2)
plot(t2,f2)
axis([min(t2),max(t2),min(f2)-min(f2)*0.2,max(f2)+max(f2)*0.2])
title('f2(t)')
xlabel('t')
subplot(2,2,3)
plot(t,f)
axis([min(t),max(t),min(f)-min(f)*0.2,max(f)+max(f)*0.2])
p=get(gca,'position');
p(3)=2.4*p(3);
set(gca,'position',p)
title('f(t)=f1(t)*f2(t)')
xlabel('t')
matlab代写常见的函数调用实例:6.周期信号傅里叶级数
display('傅里叶展开的项数')
m=input('m=');
t=-2*pi:0.01:2*pi;
n=round(length(t)/4);
f=[ones(n,1);-1*ones(n,1);ones(n,1);-1*ones(n+1,1)];
ones(314,1);
y=zeros(m+1,max(size(t)));
figure(1);
y(m+1,:)=f';
figure(1);
plot(t/pi,y(m+1,:));
grid;
axis([-2 2 -1.5 1.5]);
title('周期方波');
xlabel(size(t));
xlabel('单位:pi','Fontsize',8);
x=zeros(size(t));
kk='1'
for k =1:2:2*m-1
pause;
x=x+sin(k*t)/k;
y((k+1)/2,:)=4/pi*x;
plot(t/pi,y(m+1,:));
hold on;
plot(t/pi,y((k+1)/2,:));
hold off;
grid;
axis([-2 2 -1.5 1.5]);
title(strcat('第',kk,'次谐波叠加'));
xlabel('单位:pi','Fontsize',8);
kk=strcat(kk,',',num2str(k+2));
end
pause;
plot(t/pi,y(1:m+1,:));
grid;
axis([-2 2 -1.5 1.5]);
title('各次谐波叠加');
xlabel('单位:pi','Fontsize',8);
7.cos(2/3pi*t)的采样
display('奈奎斯特周期1.5s,Ts<1.5,过采样,Ts>1.5,欠采样');
display('Please input the value of sample period');
Ts=input('Ts=');
t=0:0.01:40;
y=cos(2/3*pi*t);
subplot(221);
plot(t,y);
axis([0 6 -1.1 1.1]);
xlabel('t单位:s','Fontsize',8');
title('f(t)');
line([0 6],[0 0],'color',[0 0 0]);
N=300;
k=-N:N;
W=2*pi*5;
w=k*W/N;
Y=0.01*y*exp(-j*t'*w);
Y=abs(Y);
subplot(222);
plot(w/pi,Y);
axis([-2,2,0,pi*7+0.2])
xlabel(' omega单位:s');
title('F(jomega)');
subplot(223);
plot(t,y,'b:');
hold on
t2=0:Ts:40;
y2=cos(2/3*pi*t2)
stem(t2,y2);
axis([0 6 -1.1 1.1]);
xlabel('t单位:s','Fontsize',8');
title('fs(s)');
hold off
Y2=Ts*y2*exp(-j*t2'*w);
Y2=abs(Y2);
subplot(224);
plot(w/pi,Y,'b')
xlabel(' omega单位:s');
title('Fs(omega)');
hold on
plot(w/pi,Y2,'r');
axis([-2,2,0,pi*10]);
hold off
8.Sa的采样与重构
%奈奎斯特k=1,01,欠采样
display('Please input the value of k');
k=input('k=');
wm=1;%信号带宽
Ts=k*pi/wm; %采样间隔
ws=2*pi/Ts; %采样角频率
wc=ws/2;%滤波器截止频率
n=-10:10;%采样点的数量
m=fix(length(n)/2);%单边采样周期fix为取整
nTs=n*Ts;%计算每个采样点
dt=0.05;
t=-m*Ts:dt:m*Ts;
f=sinc(nTs/pi);
fa=f*Ts*wc/pi*sinc((wc/pi)*(ones(length(nTs),1)*t-nTs'*ones(1,length(t))));
f2=sinc(t/pi);
subplot(311);
stem(nTs,f);%绘制采样信号
hold on
plot(t,f2,'r:');%绘制包络
xlabel('t ');
title('对Sa(t)信号进行采样');
axis([-m*Ts m*Ts -0.5 1.2]);
hold off
subplot(312);
h1=plot(t,fa);%绘制重构信号
hold on
for i = -m*Ts:dt:m*Ts
ft=sinc(i/pi)*sinc(wc/pi*(t-i));%绘制重构信号的各个风量
h2=plot(t,ft,'m:');
hold on
yy=sinc(i/pi);
yy1=yy*yy;
plot(i,yy1,'o');
endxlabel('t ');
title('重构信号');
axis([-m*Ts m*Ts -0.5 1.2]);
legend([h1,h2],'重构信号','分信号')
hold off
subplot(313);
error=abs(fa-f2);
plot(t,error);
axis([-m*Ts m*Ts min(error) max(error)+0.1*max(error)]);
xlabel('t');
ylabel(' error(t)');
我们需要根据实际的情况来选择使用不同的函数调用,让matlab能够更好的实现我们所需要的效果,在matlab当中函数的执行是能够同时间处理和返回多个输出参数,这一点各位留学生们需要好好去理解。需要matlab代写的同学们赶紧联系我们网站在线客服吧~
最后
以上就是帅气刺猬最近收集整理的关于matlab函数编写实例,Matlab代写代做常见函数调用实例的全部内容,更多相关matlab函数编写实例内容请搜索靠谱客的其他文章。
发表评论 取消回复