matlab程序函数.docx
- 文档编号:9624526
- 上传时间:2023-02-05
- 格式:DOCX
- 页数:38
- 大小:22.53KB
matlab程序函数.docx
《matlab程序函数.docx》由会员分享,可在线阅读,更多相关《matlab程序函数.docx(38页珍藏版)》请在冰豆网上搜索。
matlab程序函数
SomeoftheM-filesaregenericsothatthefilenamesindicatetheirfunction.Thesefilesare
1.dft.mDiscreteFourierTransform
程序
%
%DiscreteFourierTransform
%
functionXk=dft(x)
[N,M]=size(x);
ifM~=1,%makessurethatxisacolumnvector
x=x';
N=M;
end
Xk=zeros(N,1);
n=0:
N-1;
fork=0:
N-1
Xk(k+1)=exp(-j*2*pi*k*n/N)*x;
End
2.idft.mInverseDiscreteTimeFourierTransform
程序
%
%InverseDiscreteFourierTransform
%
functionx=idft(Xn)
[N,M]=size(Xn);
ifM~=1,%makessurethatXnisacolumnvector
Xn=Xn.';
N=M;
end
x=zeros(N,1);
n=0:
N-1;
fork=0:
N-1
x(k+1)=exp(j*2*pi*k*n/N)*Xn;
end
x=x/N;
3.contfft.mUseofFFTtocomputeContinuous-TimeFourierTransform
程序
function[X,w]=contfft(x,T);
%CONTFFT[X,w]=contfft(x,T)
%
%ComputestheFouriertransformofacontinuoustimesignal
%usingtheFFT.Theinputisthesampledcontinuous
%timesignalxandthesamplingtimeT.Theoutputis
%theFouriertransformX(w)andthefrequencyvectorw.
%
[n,m]=size(x);
ifn x=x'; end Xk=fft(x); N=length(x); k=0: N-1; k (1)=eps; X=(1-exp(-j*2*pi*k/N))./(j*2*pi*k/N/T).*Xk'; w=2*pi*k/N/T; 4.hybrid.mSimulationofcontinuous-timesystemanddigitalcontroller 程序 function[Y,UD]=hybrid(Np,Dp,Nd,Dd,T,t,U); %HYBRID[Y,UD]=hybrid(Np,Dp,Nd,Dd,T,t,U); % %HYBRIDreturnstheoutputvectorYandthecontrolvectorUC %ofahybridsystemwhichisdefinedbyNp,Dp,NdandDd. %Thetransferfunctionofthecontinuoustimeplantisstored %inNpandDp.NdandDdarethenumeratoranddenominators %ofthetransferfunctionofadiscretetimecontroller. %Tisthesamplingperiodofthedigitalcontroller. %tisarowvectorofthetimesatwhichtheinputsinUappear %andUisarowvectorofthesysteminput. %TheincrementintmustbechosensothatTisanintegermultiple %oftheincrement,i.e.,t=a: b: cthenb=T/mwherem>1isaninteger. %Notethatthesystemhasonlyoneinput. % [Ac,Bc,Cc,Dc]=tf2ss(Np,Dp); [Ad,Bd,Cd,Dd]=tf2ss(Nd,Dd); nsam=T/(t (2)-t (1));%numberofintegrationptspersample %initialize Y=0; UD=0; [ncr,ncc]=size(Ac); xc0=zeros(ncr,1); [ndr,ndc]=size(Ad); xdk=zeros(ndr,1); kmax=fix(t(length(t))/T);%#ofcompletesamplesint fork=0: kmax-1 %calculatecontrolandoutputofzoh ek=U(k*nsam+1)-Y(k*nsam+1); xd=Ad*xdk+Bd*ek; zoh=Cd*xdk+Dd*ek; xdk=xd; %integratecontinuous-timeplantwithinputofzohforTseconds udi=zoh*ones(nsam+1,1); ti=t(k*nsam+1: (k+1)*nsam+1); tint=t(1: nsam+1);%requiredforMatlab5.0,shiftstime,but %sincethisisatime-invariantsystem,theresultswon'tchange [yi,xi]=lsim(Ac,Bc,Cc,Dc,udi,tint,xc0); xc0=xi(nsam+1,: ); %augmentvectors Y=[Y;yi(2: nsam+1)]; UD=[UD;udi(2: nsam+1)]; end if(kmax*nsam+1 %computetailofsimulationfromt(kmax*nsam)tot_end k=kmax; %calculatecontrolandoutputofzoh ek=U(k*nsam+1)-Y(k*nsam+1); xd=Ad*xdk+Bd*ek; zoh=Cd*xdk+Dd*ek; %integratecontinuous-timeplantwithinputofzoh ti=t(k*nsam+1: length(t)); tint=ti-ti (1);%again,neededforMATLAB5.0 udi=zoh*ones(length(ti),1); [yi,xi]=lsim(Ac,Bc,Cc,Dc,udi,tint,xc0); %augmentvectors Y=[Y;yi(2: length(yi))]; UD=[UD;udi(2: length(udi))]; end 5.recur.mComputessolutionofdifferenceequations 程序 functiony=recur(a,b,n,x,x0,y0); % %y=recur(a,b,n,x,x0,y0) %solvesfory[n]from: %y[n]+a1*y[n-1]+a2*y[n-2]...+an*y[n-N] %=b0*x[n]+b1*x[n-1]+...+bm*x[n-M] % %a,b,n,x,x0andy0arevectors %a=[a1a2...aN] %b=[b0b1...bM] %ncontainsthetimevaluesforwhichthesolutionwillbecomputed %y0containstheinitialconditionsfory,inorder, %i.e.,y0=[y[n0-N],y[n0-N+1],...,y[n0-1]] %wheren0representsthefirstelementofn %x0containstheinitialconditionsonx,inorder %i.e.,x0=[x[n0-M],...,x[n0-1]] %theoutput,y,haslength(n) % N=length(a); M=length(b)-1; iflength(y0)~=N, error('Lengthsofaandy0mustmatch') end iflength(x0)~=M, error('Lengthofx0mustmatchlengthofb-1') end y=[y0zeros(1,length(n))]; x=[x0x] a1=a(length(a): -1: 1)%reversestheelementsina b1=b(length(b): -1: 1) fori=N+1: N+length(n), y(i)=-a1*y(i-N: i-1)'+b1*x(i-N: i-N+M)'; end y=y(N+1: N+length(n)) 以下是一些书上的程序(用空行隔开) %Example1.3 %readsdatafileandplotsclosingpriceofQQQQ c=csvread('QQQQdata1.csv',1,4,[14104]); n=1: 10; plot(n,c(n),n,c(n),'o') grid xlabel('Day(n)') ylabel('ClosingPrice') title('Figure1.22') %Example1.4 %closingpricec[n]plottedusingo's %11-dayMAfilteroutputy[n]plottedusing*'s c=csvread('QQQQdata2.csv',1,4,[14504]); fori=11: 50; y(i)=(1/11)*sum(c(i-10: i)); end; n=11: 50; plot(n,c(n),n,c(n),'o',n,y(n),n,y(n),'*') grid xlabel('Day(n)') ylabel('c[n]andy[n]') title('Figure1.27') pause % %plotofc[n]andy[n+5] n=6: 45; plot(n,c(n),n,c(n),'o',n,y(n+5),n,y(n+5),'*') axis([5453437.5]) grid xlabel('Day(n)') ylabel('c[n]andy[n+5]') title('Figure1.28') %Figure1.10 %givesexampleofacontinuous-timesignal t=0: 0.1: 30; x=exp(-.1*t).*sin(2/3*t); plot(t,x) grid axis([030-11]); ylabel('x(t)') xlabel('Time(sec)') title('Figure1.10') %Figure1.12 % n=-2: 6; x=[001210-100]; stem(n,x,'filled'); xlabel('n') ylabel('x[n]') title('Figure1.12') %Figure1.14 % n=0: 30; x=exp(-.1*n).*sin(2/3*n); stem(n,x,'filled') axis([030-11]); ylabel('x[n]') xlabel('n') title('Figure1.14') %ThisplotsFigure1.18a)andb) % n=-10: 30; OMEGA1=pi/3; OMEGA2=1; x1=cos(OMEGA1*n); x2=cos(OMEGA2*n); clf subplot(221),stem(n,x1,'filled') xlabel('n') ylabel('x[n]') title('Figure1.18a') axis([-10,30,-1.5,1.5]); subplot(222),stem(n,x2,'filled') xlabel('n') ylabel('x[n]') title('Figure1.18b') axis([-10,30,-1.5,1.5]); subplot(111) %Example2.4 % p=[0ones(1,10)zeros(1,5)];%correspondston=-1ton=14 x=p;v=p; y=conv(x,v); n=-2: 25; stem(n,y(1: length(n)))%seethetutorialforobtainingclosedcircles title('Example2.4') ylabel('x[n]*v[n]') xlabel('n') %Example2.5 % n=0: 40; x=sin(.2*n); h=sin(.5*n); y=conv(x,h); stem(n,h);%seetutorialforinstructionsonclosedcircles title('Example2.5') xlabel('n') ylabel('h[n]') pause stem(n,x) title('Example2.5') xlabel('n') ylabel('x[n]') pause stem(n,y(1: length(n))) title('Example2.5') xlabel('n') ylabel('y[n]') %Example2.11 %numericalsolutionofRCcircuit % R=1;C=1;T=.2; a=-(1-T/R/C);b=[0T/C]; y0=0;x0=1; n=1: 40; x=ones(1,length(n)); y1=recur(a,b,n,x,x0,y0);%approximatesolution %comparetoexactsolution t=0: .04: 8; y2=1-exp(-t);%exactsolution % y1=[y0y1];%augmentsI.C.ontovectorforplotting n=0: 40;%redefinesnaccordingly plot(n*T,y1,'o',t,y2,'-') % %thefollowinginsertsalegend holdon plot([22.32.6],[.6.6.6],'o',[22.32.6],[.5.5.5]) title('Example2.11'); xlabel('Time(sec)') ylabel('y(t)') text(3,.6,'Approximatesolution') text(3,.5,'Exactsolution') H=[];h=[]; pubplot print-dill\book\plotting\fig2_7 holdoff %Example2.12,useoftheODEsolver % tspan=[08];%vectorofinitialandfinaltimes y0=0;%initialvaluefory(t) [t,y]=ode45(@ex2_12_func,tspan,y0); %ComparetoEulerapproximationfromExample2.11 % R=1;C=1;T=.2; a=-(1-T/R/C);b=[0T/R/C]; y0=0;x0=1; n=1: 40; x=ones(1,length(n)); y1=recur(a,b,n,x,x0,y0);%approximatesolution %comparetoexactsolution t2=0: .04: 8; y2=1-exp(-t2);%exactsolution % y1=[y0y1];%augmentsI.C.ontovectorforplotting n=0: 40;%redefinesnaccordingly plot(n*T,y1,'o',t,y,'--',t2,y2,'-') % %thefollowinginsertsalegend legend('EulerApproximation','Runge-KuttaApproximation','ExactSolution'); %Example2.16 %Convolutionofexponentials % symstlambday y=int(exp(2*lambda-t),lambda,0,t);%for0 simplify(y) y=int(exp(2*lambda-t),lambda,0,1)+int(exp(2-t),lambda,1,t);%for1 simplify(y) y=int(exp(2*lambda-t),lambda,0,1)+int(exp(2-t),lambda,1,2);%for2 simplify(y) y=int(exp(2*lambda-t),lambda,t-4,1)+int(exp(2-t),lambda,1,2);%for4 simplify(y) y=int(exp(2-t),lambda,t-4,2)%for5 simplify(y) %Figure2.7 %LoanBalanceprogram %Programcomputesloanbalancey[k] y0=input('Amountofloan'); I=input('YearlyInterestrate'); c=input('Monthlyloanpayment');%x[n]=c y=[];%definesyasanemptyvector y (1)=(1+(I/12))*y0-c; forn=2: 360 y(n)=(1+(I/12))*y(n-1)-c; ify(n)<0,break,end end %Thefollowingcommandsarefordisplayingtheresults formatbank n=1: length(y);i=1; fprintf('\nny[n]indollars') whilei<=length(n) m=min([19length(n)-i]); out=[n(i: i+m)',y(i: i+m)'] i=i+20; fprintf('(Hitreturntocontinue)') pause end formatshorte functiony=recur(a,b,n,x,x0,y0); % %y=recur(a,b,n,x,x0,y0) %solvesfory[n]from: %y[n]+a1*y[n-1]+a2*y[n-2]...+an*y[n-N] %=b0*x[n]+b1*x[n-1]+...+bm*x[n-M] % %a,b,n,x,x0andy0arevectors %a=[a1a2...aN] %b=[b0b1...bM] %ncontainsthetimevaluesforwhichthesolutionwillbecomputed %y0containstheinitialconditionsfory,inorder, %i.e.,y0=[y[n0-N],y[n0-N+1],...,y[n0-1]] %wheren0representsthefirstelementofn %x0containstheinitialconditionsonx,inorder %i.e.,x0=[x[n0-M],...,x[n0-1]] %theoutput,y,haslength(n) % N=length(a); M=length(b)-1; iflength(y0)~=N, error('Lengthsofaandy0mustmatch') end iflength(x0)~=M, error('Lengthofx0mustmatchlengthofb-1') end y=[y0zeros(1,len
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- matlab 程序 函数