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

  1. %MATDEMO Introduction to MATLAB.
  2.  
  3. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  4.  
  5. echo off
  6. % Reset things before we get started.
  7. clear
  8. clc
  9. format 
  10. echo on
  11.  
  12. % Welcome to MATLAB. In the next few minutes, we hope to show you
  13. % some of the power and flexibility offered by the MATLAB environment.
  14. % This entire demonstration is written in MATLAB's fourth generation
  15. % language, which gives you an idea of how easy it is to perform
  16. % computations using MATLAB.
  17. %
  18. % Any line which is not preceded by a percent character, '%', is an
  19. % actual MATLAB command.
  20.  
  21. % Press any key to continue...
  22. pause
  23. clc
  24. % First, let's create a simple vector with 9 elements called 'a'.
  25.  
  26. a = [1 2 3 4 6 4 3 4 5 ]
  27. % Notice how the results from each operation in MATLAB echo its result
  28. % unless a semicolon is added at the end of each command.
  29.  
  30. % Press any key to continue...
  31. pause
  32. clc
  33. % Now let's add 2 to each element of our vector, 'a', and store the
  34. % result in a new vector. Notice how MATLAB requires no special handling
  35. % of vector or matrix math.
  36.  
  37. b = a + 2
  38. % Press any key to continue...
  39. pause
  40. clc
  41. % Creating graphs in MATLAB is as easy as one command. Let's plot the
  42. % result of our last vector addition.
  43.  
  44. plot(b)
  45. grid
  46. title('Press Any Key To Continue...')
  47.  
  48. % The "plot" statement graphs the elements of "b". The title was added
  49. % with the "title" statement and the grid was drawn with "grid".
  50.  
  51. % Press any key to continue...
  52. pause
  53. clc
  54. % MATLAB can make other graph types as well. 
  55.  
  56. bar(b)
  57. xlabel('Sample #')
  58. ylabel('Pounds')
  59.  
  60. % Notice the axis labels on the bar chart. Axis labels are drawn using
  61. % the "xlabel" and "ylabel" commands. The bar chart was drawn using the
  62. % "bar" function. 
  63.  
  64. % Press any key to continue...
  65. pause
  66. clc
  67. % MATLAB can use symbols in plots as well. Here is an example using *'s
  68. % to mark the points.  MATLAB offers a variety of other symbols and
  69. % line types for use in graphs as well. 
  70.  
  71. plot(b,'*')
  72. axis([0 10 0 10])
  73.  
  74. % Press Any Key To Continue...
  75. pause
  76. clf
  77. clc
  78. % One area in which MATLAB excels is matrix computation. Creating a
  79. % matrix is as easy as making a vector, using semicolons (;) to separate
  80. % the rows of a matrix. For example:
  81.  
  82. A = [1 2 0; 2 5 -1; 4 10 -1]
  83. % Press any key to continue...
  84. pause
  85. clc
  86. % We can easily find the transpose of the matrix 'A'. 
  87.  
  88. B = A'
  89. % Press any key to continue...
  90. pause
  91. clc
  92. % Now let's multiply these two matrices together. Note again that MATLAB
  93. % doesn't require you to deal with matrices as a collection of numbers.
  94. % MATLAB knows when you are dealing with matrices and adjusts your
  95. % calculations accordingly.
  96.  
  97. C = A*B
  98. % Press any key to continue...
  99. pause
  100. clc
  101. % Instead of doing a matrix multiply, we can multiply the corresponding
  102. % elements of two matrices or vector using the .* operator.
  103.  
  104. C = A.*B
  105. % Press any key to continue...
  106. pause
  107. clc
  108. % Let's find the inverse of a matrix...
  109.  
  110. X = inv(A)
  111. % Now, let's illustrate the fact that a matrix times its inverse
  112. % is the identity matrix.
  113.  
  114. I = inv(A)*A
  115. % Press any key to continue...
  116. pause
  117. clc
  118. % MATLAB has functions for nearly every type of common matrix calculation.
  119. % There are functions to obtain eigenvalues ...
  120.  
  121. eig(A)
  122. % as well as the singular value decomposition.
  123.  
  124. svd(A)
  125. % Press any key to continue...
  126. pause
  127. clc
  128. % The characteristic polynomial of a matrix A is
  129. %     det(lambda*I - A)
  130. % The "poly" function generates a vector containing the coefficients
  131. % of the characteristic polynomial.
  132.  
  133. p = round(poly(A))
  134. % Press any key to continue...
  135. pause
  136. clc
  137. % We can easily find the roots of a polynomial using the "roots" function.
  138.  
  139. roots(p)
  140. % These are actually the eigenvalues of the original matrix.
  141.  
  142. % Press any key to continue...
  143. pause
  144. clc
  145. % MATLAB has many applications beyond just matrix computation.
  146. % To convolve two vectors ...
  147.  
  148. q = conv(p,p)
  149. % Press any key to continue...
  150. pause
  151.  
  152. % Convolve again ...
  153.  
  154. r = conv(p,q)
  155. % Press any key to continue...
  156. pause
  157. clc
  158. % Two graphs of the result.
  159.  
  160. plot(r)
  161.  
  162. % Press any key to continue...
  163. pause
  164.  
  165. bar(abs(r))
  166.  
  167. % Press any key to continue...
  168. pause
  169.  
  170. clc
  171. % At any time, we can get a listing of the variables we have stored in
  172. % memory using the "who" or "whos" command.
  173.  
  174. whos
  175. % Press any key to continue...
  176. pause
  177. clc
  178. % You can get the value of a particular variable by typing its name.
  179.  
  180. A
  181. % Press any key to continue...
  182. pause
  183. format compact
  184. clc
  185. % You can have more than one statement on a single line by separating
  186. % each statement with commas or semicolons. Also note: if we don't
  187. % assign a variable to store the result of an operation, the result
  188. % is stored in a temporary variable called "ans".
  189.  
  190. sqrt(-1), log(-1), log(0)
  191.  
  192. % Since we separated the statements with commas, the result of each
  193. % operation was echoed to the screen. As you can see, MATLAB easily
  194. % deals with complex and infinite numbers in calculations.
  195.  
  196. % Press any key to continue...
  197. pause
  198. clc
  199. % MATLAB has functions which make it ideal as a signal processing tool.
  200. % Let's find the two dimensional FFT of a raised pedestal. First we'll
  201. % make a 32x32 matrix full of zeroes, then fill a 3x3 block at the center
  202. % of the matrix with ones.
  203.  
  204. A = zeros(32);
  205. A(14:16,14:16) = ones(3);
  206.  
  207. % You can see how easily you can change a part of a matrix or vector
  208. % without having to reenter the entire matrix. In case you can't visualize
  209. % what we just made, let's look at the values of our matrix. 
  210.  
  211. % Press any key to get a quick look at the entire matrix ...
  212. pause
  213. format loose
  214. colormap(cool)
  215. clc
  216. A
  217. % It's understandable if you couldn't visualize what was going on with
  218. % all of those numbers scrolling by. Let's make a three-dimensional
  219. % plot of the matrix using the "mesh" command.
  220.  
  221. % Press any key to continue...
  222. pause
  223.  
  224. mesh(A)
  225.  
  226. % Press any key to continue...
  227. pause
  228. clc
  229. % Now let's take the two-dimensional FFT of our matrix and plot its
  230. % magnitude.
  231.  
  232. y = fft2(A);
  233. mesh(abs(y))
  234.  
  235. % Press any key to continue...
  236. pause
  237.  
  238. clc
  239. % In signal processing, it is often desirable to shift the quadrants
  240. % of 2D FFTs so that the zeroth lag (DC) is at the center of the spectrum.
  241. % We can do this in MATLAB using the "fftshift" function.
  242.  
  243. y = fftshift(y);
  244. mesh(abs(y))
  245.  
  246. % Press any key to continue...
  247. pause
  248.  
  249. clc
  250. % Another interesting application is the Sobel edge-finding technique.
  251. % We can use this to find the edges of a pedestal. We will use the same
  252. % method to make the matrix as we did in the previous 2D FFT demo.
  253.  
  254. A = zeros(32);
  255. A(4:28,4:28)=ones(25);
  256. mesh(A)
  257.  
  258. % Press any key to continue...
  259. pause
  260.  
  261. clc
  262. % Find the horizontal edges by convolving our data with the Sobel
  263. % edge detector and plot the result.
  264.  
  265. s = [1 2 1; 0 0 0; -1 -2 -1];
  266. h = conv2(A,s);
  267. mesh(h)
  268.  
  269. % Press any key to continue...
  270. pause
  271. clc
  272. % Now let's find the vertical edges.
  273.  
  274. v = conv2(A,s');
  275. z = sqrt(h.^2+v.^2);
  276. mesh(z);
  277. title('Horizontal and Vertical Edges')
  278.  
  279. % Press any key to continue...
  280. pause
  281.  
  282. clc
  283. % MATLAB also allows you to create and analyze functions easily. For example,
  284. % here is a function we have saved as a MATLAB M-file. M-files are just ASCII
  285. % files which contain MATLAB commands. M-files can return values, as functions,
  286. % or behave just like simple scripts. Let's take a look at our "humps" function.
  287.  
  288. type humps
  289. % Press any key to continue...
  290. pause
  291.  
  292. clc
  293. % Now suppose we want to see what "humps" looks like in the range -1 <= x <=2.
  294. % First we'll create a vector called 'x', with values from -1 to 2 in steps
  295. % of .02. Then, we'll plot the values of x vs. humps(x).
  296.  
  297. x = -1:.02:2;
  298. plot(x,humps(x))
  299. title('Plot of humps(x)')
  300.  
  301. pause
  302.  
  303. clc
  304. % We can find the numerical integral of humps(x) using the "quad" function.
  305. % In this case we'll find the integral over the range of 0 <= x <= 1.
  306. % We will also use two optional features in "quad" which will allow us to
  307. % set the relative tolerance of our integration to .001, and show a plot
  308. % of the points chosen by the numerical integration.
  309.  
  310. q = quad('humps',0,1,.001,1);
  311. title('Evaluation points for humps(x) integration.')
  312.  
  313. pause
  314.  
  315. % The result of the integral is
  316.  
  317. q
  318.  
  319. % Press any key to continue...
  320. pause
  321. clc
  322. % Thank you for participating in this introduction to MATLAB.
  323. %
  324. % To see a list of other demos and examples that might interest you,
  325. % type
  326. %
  327. %    help demos
  328. % And, if you haven't already done so, simply type
  329. %
  330. %    demo
  331. %
  332. % to obtain the demo menu.
  333.  
  334.