Unformatted text preview:

COMPUTER APPLICATIONS IN THE GEOSCIENCES -- FALL 2008 -- Notes compiled by B. Konter (UTEP) and P. Shearer (UCSD) MATLAB INTRODUCTION Matlab is a commercial product that is used widely by students and faculty and researchers at UTEP. It provides a "high-level" programming environment for computing with a special emphasis on matrix operations. Many specialized functions are "hard wired" into Matlab so there is no need to use subroutine libraries for these tasks. It is also very easy to make plots of Matlab output--many plotting functions are also built into the program. Matlab is available for PCs, Macs, and UNIX workstations. The student version of Matlab currently sells for about $100 at the UTEP bookstore. We have a site license for Matlab on the department network, however this is limited to university computers only. Matlab is more user-friendly and easier to learn than standard languages like Fortran and C. It is good for many problems, but ultimately does not have the flexibility of C or Fortran. Complicated Matlab scripts will often run much slower than the Fortran or C equivalent. Matlab is well documented with online help (look to the menu bar when you are running the program). There are also many books written about Matlab that you may find useful if you become a serious user. The following is a brief introduction to help you get started and to demonstrate some of Matlab’s capabilities. Matlab can be used interactively as a super hand calculator, or, more powerfully, run using scripts (i.e., programs).  PCs: A Matlab icon can be found on your desktop.  Macs : Go to Macintosh HD  Appli cations  Matlab  and drag the Matlab icon to your doc (for future use). Start Matlab and several blank screens, including a Command Window, should appear………. LLABA B 66  MATLABM A T L A BCOMPUTER APPLICATIONS IN THE GEOSCIENCES -- FALL 2008 -- #1. Prompt and easy addition >> At the >> prompt, one can directly enter commands. As an example, the following is one way to compute 2+2: (Try it, you enter the bold, black infoand Matlab returns the gray info) >> a = 2 a = 2 >> b = 2 Command windowCOMPUTER APPLICATIONS IN THE GEOSCIENCES -- FALL 2008 -- b = 2 >> c = a + b c = 4 #2. The semi-colon (;) It is often annoying to have Matlab always print out the value of each variable. To avoid this, put a semicolon(;) after the commands: >> a = 2; >> b = 2; >> c = a + b; >> c c = 4 Only the final line produces output. Semicolons can also be used to string together more than one command on the same line: >> a = 2; b = 2; c = a + b; c c = 4 #3. More complicated math Now try an example for computing a quadratic equation, which includes multiplication (*), division (/), square-root (sqrt), and powers (^). Also note that parentheses are used to determine the order of operations. >> a = 2;COMPUTER APPLICATIONS IN THE GEOSCIENCES -- FALL 2008 -- >> b = -12; >> c = 16; >> quad1 = (-b + sqrt(b^2 - 4*a*c)) / (2*a) quad1 = 4 #4. More complicated math: Trig functions Trig functions are also supported by Matlab. Note that the arguments of trig functions must be in radians, not degrees: >> y = sin (pi/6) y = 0.5000 Note that "pi" is hardwired to the value of pi (3.14..); you don't have to set this. #5. Whos (what are my variables?) To see all the variables currently defined in Matlab’s current session (that you entered), use the "whos" command: >> whos Name Size Bytes Class a 1x1 8 double array b 1x1 8 double array c 1x1 8 double array quad1 1x1 8 double array y 1x1 8 double array To remove all variables from memory, use the "clear" command: >> clearCOMPUTER APPLICATIONS IN THE GEOSCIENCES -- FALL 2008 -- >> whos >> # 6. Using a Matlab script (*.m) file A script is a text file containing lines of Matlab commands (e.g. testscript.m). To run a script, simply type the name of your script, but without the “.m” suffix, at the Matlab prompt: >> testscript By doing this, Matlab simply executes each line one at a time, exactly as if you had typed each line into the command window. In your script, you can create lines beginning with a “%” sign, which are understood by Matlab to be comments about the code (and are ignored). Example: % At the first line of my code, I should always put my name % At the second line of my code, I should enter a % description of what the program is going to do. A script can be written with the built-in Matlab Editor, or you can open up an existing script that someone gave you. Here you will do both. First, make a new Lab6 folder in your geobase directory and copy to the Lab6 folder (yes, please copy) the following file from Bridget’s geobase folder: bkonter/Matlab_stuff/testscript.m To run a script, you need to tell Matlab where to look for the *.m files that you want to run. To do this, you’ll want to set the “Current Directory” to your working folder, so for this exercise it will be your Lab6 folder on geobase. In the top Matlab window, look for the icon “…” (see below) and click on it to navigate to your Lab6 folder. Next you will need to open up the Matlab Editor window: Window  Editor.COMPUTER APPLICATIONS IN THE GEOSCIENCES -- FALL 2008 -- A new, separate window should appear. Now open the testscript.m file: File  Ope n  select testscri pt.m Familiarize yourself with the content of the script (read through it), then run the program in the Matlab window by typing: >> testscript What did this program do? Draw a sketch of the resulting figure of testscript.m program below: # 7. Creating a new Matlab script (*.m) file Next you will create a new Matlab script to plot an example set of x-y data points. The file with these example points is called test_data.dat and is located in Bridget’s Matlab folder. Copy this file to your own Lab6 folder. Open up test_data.dat file in the Matlab Editor to inspect its contents: File  Open  test_data.dat The first column of the data will be the ‘x’ column (pretend it is earthquakeCOMPUTER APPLICATIONS IN THE GEOSCIENCES -- FALL 2008 -- depths), and the second column of


View Full Document

UTEP GEOL 5215 - Lab 6 MATLAB

Download Lab 6 MATLAB
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lab 6 MATLAB and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lab 6 MATLAB 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?