Unformatted text preview:

Math 1322 Fall 2002 Introduction to Matlab Basic Commands You should look at Sections 1 1 1 3 of the Matlab reference book Using Matlab in Calculus by Gary Jensen preferably with Matlab running in front of you Do not worry about remembering each and every command Try to get an overview of the basics and what topics are referred to in the book so you can return to them when needed The material there will probably be easier to read if you go through these notes first Availability Matlab is available on PC s in the Artsci Computing Center in Eads and in the Center for Engineering Computing It should also be available on the computers at Cornerstone and 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 commands Quitting Matlab You can use the menus OR just type File Exit Matlab quit Help See p 1 of Using Matlab in Calculus Numbers Arrays Variables Matlab thinks in terms of numbers and arrays For example a 1 3 5 7 b 1 2 3 1 An array containing 4 numbers called its elements or members or components An array with a single row or column is sometimes also called a vector We mostly 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 displays an array on screen it always omits commas and brackets 5 4 An array with 2 rows and 3 columns Names If we like we can give arrays more imaginative names than a or b For example 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 variable names in Matlab A variable 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 case sensitive for example Evens is different from evens 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 For example 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 special variable names for example you can guess both stand for the complex number which we won t use the variable that holds the number or array which resulted from the most recent Matlab calculation pi i j ans You 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 Format Matlab normally displays 4 digits although it carries about 16 digits in its internal calculations The commands format long format short help format will display more digits returns the display to the shorter display displays some of the other format options Creating Arrays You can create an array in Matlab in several ways 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 1 7 3 8 5 The semicolon starts a new 9 row as you enter the numbers 2 by specifying the stepsize from one element to the next a 1 2 7 a 1 2 7 a 1 4 For example we could create This means start with 1 increase in steps of size 2 and end with 7 giving a 1 3 5 7 again If you like you can leave off the brackets and simply enter If the stepsize is omitted it is assumed to be 1 so creates the array a 1 2 3 4 b 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 decrease in steps of size 2 stop at 0 Notice that produces the same array b 8 6 4 2 0 since adding 2 to 0 would go past the stopping value 1 last number created before the stopping value 1 b 8 2 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 displayed on the screen it s a lot of clutter you don t need to see How to avoid this When there s a semicolon at the end of a command Matlab creates the variable in memory but doesn t produce a display A wiser command therefore would probably be c 0 50 100000 3 By specifying the number of elements in the array In 2 we specified the stepsize and the number of elements in the array is whatever it turns out to be An alternative is to specify the number of equally spaced elements in the array and then the stepsize is whatever it turns out to be The command a linspace 1 5 7 a 1 0000 creates an array of 7 points equally spaced linearly spaced from 1 to 5 Notice that these seven numbers divide the interval 1 5 into six equal subintervals Rounded to 4 decimal places 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 defined If 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 2 1 4 3 6 when a b have the same length 5 During a long Matlab session you might have defined many variables The following commands are useful to know what you ve got and to clean up The command who clear a clear a b clear lists all the current user defined variables causes Matlab to forget a or undefine a causes Matlab to undefine a and b note no comma causes Matlab to undefine all user defined variables CAUTION Exercise You want to divide the interval 1 7 into 10 equal subintervals In three different ways create an array called endpoints which lists in order of increasing …


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
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 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?