home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 3.ddi / DEMOS.DI$ / FOURIER.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  2.6 KB  |  96 lines

  1. %FOURIER Graphics demo of Fourier series expansion.
  2. %       Execute FOURIER to run it.
  3.  
  4. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  5.  
  6. clf 
  7. echo on
  8. clc
  9. %       The Fourier series expansion for a square-wave is made up of a sum
  10. %       of odd harmonics. We can show this graphically using MATLAB.
  11.  
  12. %    We start by forming a time vector running from 0 to 10 in steps 
  13. %    of 0.1, and taking the sine of all the points:
  14.  
  15. t = 0:.1:10;
  16. y = sin(t);
  17.  
  18. %    Let's plot this fundamental frequency.
  19.  
  20. plot(t,y), pause     % Press any key to continue.
  21. clc
  22. %    Now add the third harmonic to the fundamental, and plot it:
  23.  
  24. y = sin(t) + sin(3*t)/3;
  25.  
  26. plot(t,y), pause     % Press any key to continue.
  27. clc
  28.  
  29. %    Now use the first, third, fifth, seventh, and ninth harmonics:
  30.  
  31. y = sin(t) + sin(3*t)/3 + sin(5*t)/5 + sin(7*t)/7 + sin(9*t)/9;
  32.  
  33. plot(t,y), pause     % Press any key to continue.
  34. clc
  35. %    For a finale, we will go from the fundamental to the 19th harmonic,
  36. %    creating vectors of successively more harmonics, and saving all
  37. %    intermediate steps as rows in a matrix.
  38.  
  39. t = 0:.02:3.14;
  40. y = zeros(10,max(size(t)));
  41. x = zeros(size(t));
  42.  
  43. for k=1:2:19
  44.     x = x + sin(k*t)/k; 
  45.     y((k+1)/2,:) = x;
  46. end
  47.  
  48. %    Let's plot these successively on an overplot, showing the transition 
  49. %    to a square wave.  Note that Gibbs' effect says that it will never 
  50. %    really get there.
  51.  
  52. plot(y(1:2:9,:)'), title('The building of a square wave: Gibbs'' effect'), pause
  53. clc
  54.  
  55. %    Now plot this as a 3-d surface to show the transition to a square wave.
  56.  
  57. surf(flipud(y));
  58.  
  59. %       You can move the slider to change the number of terms in the series.
  60.  
  61. echo off
  62.  
  63. FSH = get(gca,'Children');
  64. global FSH
  65.  
  66. Nmax = 25;
  67. ha = axes('pos',[0 0 1 1],'Visible','off');
  68. tx(1) = text(.94,.7,'2','hor','right');
  69. tx(2) = text(.94,.95,int2str(Nmax),'hor','right');
  70. tx(3) = text(.94,.825,'Number of Terms:','horizontalalignment','right');
  71. set(tx,'visible','off')
  72.  
  73. P = pink(64);
  74. colormap(flipud(P(17:64,:)));
  75. title([int2str((k-1)/2) ' Terms'])
  76. drawnow
  77. hh = gca;
  78. fig = gcf;
  79. s = ['t=0:.02:3.14;x=zeros(size(t));' ...
  80.     'global HC FSH;' ...
  81.     'N = ceil(get(HC,''Value''));' ...
  82.     'y = zeros(N,length(t));' ...
  83.     'for k=1:2:(2*N-1);' ...
  84.     'x = x+sin(k*t)/k; y((k+1)/2,:) = x;end,' ...
  85.     'xd = 1:length(t); yd = (1:size(y,1))'';' ...
  86.     'set(FSH,''xd'',xd,''yd'',yd,''zd'',flipud(y),''cd'',flipud(y)),' ...
  87.     'title([int2str(N) '' Terms'']);'];
  88. global HC;
  89. HC = uicontrol(fig,'Style','slider','Position',[0.95 0.70 0.03 0.25],...
  90.     'Min',2,'Max',Nmax, 'Units', 'normalized', ...
  91.     'Value',9,'CallBack',s);
  92. set(tx,'visible','on');
  93. set(fig,'CurrentAxes',hh);
  94.  
  95. set(fig,'NextPlot','replace')
  96.