Estadio Nacional

Páginas: 11 (2563 palabras) Publicado: 26 de diciembre de 2012
New Mexico Tech Hydrology Program

Hyd 510 Quantitative Methods in Hydrology Lab Notes. 3 Programming

Writing Programs in MATLAB
A MATLAB program is a file containing a list of MATLAB commands that are executed in a particular sequence. In MATLAB, a program file name must end with the extension “.m”, called an M-file. To run a program, type the file name (without the “.m” extension) at theMATLAB prompt. Each of the MATLAB exercises we’ve done so far could be written in an M-file. The M-file would contain the list of commands that you typed at the MATLAB prompt. The advantages of using an M-file instead of the command line are (1) the program can be run many times without re-entering the commands; (2) if you have errors, it is easier to debug the program; (3) the program is notremoved from memory when you exit MATLAB; and (4) control structures such as loops and “if” statements are easier to use inside of a program. Programs should be specific enough to accomplish the required tasks, but flexible enough so they can be used more than once. For example, if you want to find the mean of five head values, the program would be too specific if you coded the five values into thecode. A more flexible code would prompt the user to enter five head values, and would calculate the mean of those values. Then we could use the program to calculate the mean of any set of five head values. In an even more flexible code, we could prompt the user for the number of head values that will be entered, and then prompt the user to enter all of the head values. This program could be used tocalculate the mean of any set of an arbitrary number of head values.

Creating a MATLAB Program To create a new MATLAB program, select File, New, M-file. This will open the editor window. Type your commands in this window. When you save the file, you should save it on your home directory which is U:, and save it with a “.m” extension. To run the program, either go to the command window and typethe file name (without the extension) or select Tools, Run from the menu bar in the editor window. In either case, MATLAB must be running in the directory where your file is located. To find out what directory MATLAB is in from the command line, type “pwd” at the prompt. This is a unix command to print the name of the current directory. To change to the U drive, type “cd U:”; to change directoriestype “cd dirname”, where dirname is the directory name. You can also edit an existing program by selecting File, Open in the MATLAB menu bar. Top-down Design Top-down design is a procedure for logically dividing the program tasks into the necessary MATLAB command statements. At the top level, we define the main tasks of the program. Once these tasks are all defined, we subdivide the tasks intosmaller subtasks, and continue this refinement until we are at the lowest level, which is the MATLAB commands. This provides a logical procedure from getting from the problem statement to the actual list of commands. The problem statement might be very complex, so we break it down into smaller subtasks that can be solved easily. This procedure is very useful in large programs. Similarity to otherPrograming Languages Many of the MATLAB programing conventions discussed below are very similar to those in the C and C++ languages, and somewhat similar to those in FORTRAN. For MATLAB it can be particularly useful to refer to a C or C++ primers. You can find links to these and to Unix primers at: http://www.saintmarys.edu/~psmith/candunix.html A particularly good C primer is athttp://www.cm.cf.ac.uk/Dave/C/CE.html Keep in mind, however, that MATLAB works on vectors (and matrices; MATLAB is a vector language) while C and C++ work on scalars.

-3.1-

New Mexico Tech Hydrology Program

Hyd 510 Quantitative Methods in Hydrology Lab Notes. 3 Programming

(FORTRAN is also a scalar language. For vectorized supercomputers, eg. a old CRAY, there are vector versions of these standard...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Estadio nacional
  • Estadio nacional
  • Estadio nacional
  • Estadio Nacional de Pekín
  • La educación y el estadio nacional
  • Estadio Nacional Del Perú
  • Características Del Estadio Nacional Del Perú
  • Proyecto Remodelación Estadio Nacional

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS