WUSTL MATH 132 - Introduction to Matlab: Basic Commands

Unformatted text preview:

Math 1322, Fall 2002Introduction to Matlab: Basic CommandsYou should look at Sections 1.1-1.3 of the Matlab reference book ( byUsing Matlab in CalculusGary Jensen), preferably with Matlab running in front of you. Do not worry about rememberingeach and every command. Try to get an overview of the basics and what topics are referred to inthe book so you can return to them when needed. The material there will probably be easier toread if you go through these notes first.Availability Matlab is available on PC's in the Artsci Computing Center in Eads and in theCenter for Engineering Computing. It should also be available on the computers at Cornerstoneand in the Residential Computing Facilities.Starting Matlab on a PC If there is a Matlab icon on the desktop, double click on it If not, use the menus: Start, Programs, Matlab You will then be in the Matlab workspace where you can enter commandsQuitting Matlab You can use the menus: File, Exit Matlab OR, just type: quitHelp See p.1 of Using Matlab in CalculusNumbers, Arrays, VariablesMatlab “thinks” in terms of numbersarraysand . For example a = [1 3 5 7] An array containing 4 numbers (called its or elementsmembers or ). An array with a single row (or column)components is sometimes also called a . vector mostlyWe use arrays with just one row in Calculus I-II The same array in Matlab can also be entered with commas: a = [1,3,5,7] But when Matlab an array on screen, it always displaysomits commas and brackets b = An array with 2 rows and 3 columns.135214”•NamesIf we like, we can give arrays more imaginative than “a” or “b” . For examplenames evens = [8, 6, 4, 2, 0] or time_intervals = [1, 3, 5, 7, 8, 6, 4, 2, 0] (Blank spaces are not allowed in names: use an underscore character to connect the parts) “a” , “evens” and “time_intervals” are examples of in Matlab. A variablevariable names can refer either to a number or an array: b = 7 a = [1, 3, 5, 7] ( Actually, we can also think of 7 as being an array: the 1-element “array” [7] .)Names in Matlab are “Evens” is different from “evens”case sensitive  for example, Names can be up to 19 characters, must start with a letter, and can't contain punctuation symbols.A good name for a variable can remind you about what information it contains. Forexample, if we measure the room temperature (F° each hour for 6 hours, we might enterÑthe data in an array called “tempdata” : tempdata = [68,72,84,87,90,94]In Matlab, there are certain built-in for examplespecial variable names pi you can guess! i, j he complex number (which we won't use)both stand for tÈ" ans the variable that “holds” the number or array which resulted from the most recent Matlab calculationYou can preempt these names and use them for your own variables if you want. For example,you're allowed to define a variable pi = 17.634 but this isn't a wise practice!FormatMatlab normally displays 4 digits (although it carries about 16 digits in its internal calculations).The commands format long will display more digits format short returns the display to the shorter display help format displays some of the other format optionsCreating ArraysYou can in Matlab in several ways:create an array 1) by directly typing it in a = [1, 3, 5, 7] (or a = [1 3 5 7] without commas, if you like) a = [1,3,5 ; 7,8,9] to create The semicolon " ; " starts a new135789”• row as you enter the numbers 2) by specifying the “stepsize” from one element to the next a = [1:2:7] T : start with 1, increase in 2,his means steps of size and end with 7, giving a = [1, 3, 5, 7] again. If you like, you can leave off the brackets and simply enter a = 1:2:7 a = 1:4 If the stepsize is omitted, it is assumed to be 1, so creates the array a = [1, 2, 3, 4] For example, we could createb = [8, 6, 4, 2, 0] by typing it in directly, or by typing b = 8: 2:0 Negative stepsizes are allowed. This command means: start at 8, in steps of size 2, stop at 0decrease Notice that b = [8: 2 1] produces the array b = [8,6,4,2,0] since addingÀ same 2 to 0 would go past the stopping value 1Þ last number created before the stopping value 1 c = 0:50:100000 creates the array c = [0,50,100,150, ... ,100000] (The “ ... ” is not part of Matlab: in these notes it just means “and so on.”) It would be very time-consuming to enter the whole array by actually typing all the entries.This array “c” is very long (how many elements does it have?). If all these elements are displayedon the screen, it's a lot of clutter you don't need to see. How to avoid this? When there's asemicolon “ ; ” at the end of a command, Matlab creates the variable in memory but doesn'tproduce a display. A wiser command, therefore, would probably be: c = 0:50:100000; 3) By specifying the number of elements in the arrayIn 2) we specified the stepsize (and the number of elements in the array is whatever it turns outto be). An alternative is to specify the number of (equally-spaced) elements in the array (andthen the stepsize is whatever it turns out to be). The command a = linspace(1,5,7) creates an array of equally spaced ("linearly7 points spaced") from 1 to 5. Notice that these seven numbers divide the interval [ 1,5 ] into equal subintervals.six Rounded to 4 decimal places, a = [1.0000 1.6667 2.3333 3.0000 3.6667 4.3333 5.0000] (As usual, Matlab omits the brackets in its display.) 3) By “piecing together” two arrays you have already definedIf you already have a = [1, 3, 5] and b = [2, 4, 6], you can use the commands: c = [a b] to create c = [1, 3, 5, 2, 4, 6] or c = [b a] to create [2, 4, 6, 1, 3, 5] or c = [b ; a] to create (when a,b have the same length)246135”•During a long Matlab session you might have defined many variables. The following commandsare useful to “know what you've got” and to “clean up”:The command: who lists all the current user-defined variables clear a causes Matlab to “forget a” or “undefine” a clear a b causes Matlab to undefine a and b (note: no


View Full Document

WUSTL MATH 132 - Introduction to Matlab: Basic Commands

Documents in this Course
shapiro1

shapiro1

11 pages

shapiro

shapiro

11 pages

shapiro1

shapiro1

11 pages

shapiro

shapiro

11 pages

Load more
Download Introduction to Matlab: Basic Commands
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 Introduction to Matlab: Basic Commands 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 Introduction to Matlab: Basic Commands 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?