### Create a StudySoup account

#### Be part of our community, it's free to join!

Already have a StudySoup account? Login here

# MATH MODELING MATH 442

Texas A&M

GPA 3.6

### View Full Document

## 18

## 0

## Popular in Course

## Popular in Mathematics (M)

This 51 page Class Notes was uploaded by Vivien Bradtke V on Wednesday October 21, 2015. The Class Notes belongs to MATH 442 at Texas A&M University taught by Gregory Klein in Fall. Since its upload, it has received 18 views. For similar materials see /class/226030/math-442-texas-a-m-university in Mathematics (M) at Texas A&M University.

## Reviews for MATH MODELING

### What is Karma?

#### Karma is the currency of StudySoup.

#### You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 10/21/15

MATLAB 74 Basics P Howard Fall 2007 Contents 1 Introduction 3 11 The Origin of MATLAB 3 12 Starting MATLAB at Texas AampM University 3 13 The MATLAB Interface 3 14 Basic Computations 4 15 Variable Types 4 16 Diary Files 5 17 Clearing and Saving the Command Window 5 18 The Command History 6 19 File Management from MATLAB 6 110 Getting Help 6 2 Symbolic Calculations in MATLAB 6 21 De ning Symbolic Objects 7 211 Complex Numbers 7 212 The Clear Command 8 22 Manipulating Symbolic Expressions 8 221 The Collect Command 8 222 The Erpartd Command 9 223 The Factor Command 9 224 The Horner Command 10 225 The Simple Command 10 226 The Pretty Command 11 23 Solving Algebraic Equations 11 24 Numerical Calculations with Symbolic Expressions 13 241 The Double and Eval commands 13 242 The Subs Command 14 3 Plots and Graphs in MATLAB 15 31 Plotting Functions with the plot command 17 32 Parametric Curves 18 33 Juxtaposing One Plot On Top of Another 19 34 Multiple Plots 20 35 Ezplot 20 36 SaVing Plots as Encapsulated Postscript Files 22 4 Semilog and Doublelog Plots 23 41 Semilog Plots 23 411 Deriving Functional Relations from a Semilog Plot 24 42 Double log Plots 26 5 Inline Functions and M les 28 51 lnline Functions 28 52 Script M Files 32 53 Function M les 32 54 Functions that Return Values 33 55 Subfunctions 34 56 Debugging M les 35 6 Basic Calculus 35 61 Differentiation 35 62 Integration 36 63 Limits 37 64 Sums and Products 38 65 Taylor series 38 66 Maximization and Minimization 39 7 Matrices 39 8 Programming in MATLAB 41 81 Overview 41 82 Loops 42 821 The For Loop 42 822 The While Loop 42 83 Branching 43 831 lf Else Statements 43 832 Switch Statements 44 84 Input and Output 44 841 Parsing Input and Output 44 842 Screen Output 45 843 Screen lnput 45 844 Screen Input on a Figure 46 9 Miscellaneous Useful Commands 46 10 Graphical User Interface 47 11 SIMULINK 47 12 Mbook 47 13 Useful Unix Commands 47 131 Creating Unix Commands 48 132 More Help on Unix 48 1 Introduction 11 The Origin of MATLAB MATLAB which stands for MATrix LABoratory is a software package developed by Math Works Inc to facillitate numerical computations as well as some symbolic manipulation The collection of programs primarily in Fortran that eventually became MATLAB were developed in the late 1970s by Cleve Moler who used them in a numerical analysis course he was teaching at the University of New Mexico Jack Little and Steve Bangert later reprogrammed these routines in C and added M les toolboxes and more powerful graph ics original versions created plots by printing asterisks on the screen Moler Little and Bangert founded MathWorks in California in 1984 12 Starting MATLAB at Texas AampM University New this semester your NetlD and password should access your calclab account Log in and click on the six pointed geometric gure in the bottom left corner of your screen Go to Mathematics and choose Matlab Congratulations Alternatively click on the surface plot icon at the foot of your screen 13 The MATLAB Interface The default MATLAB screen is divided into three windows with a large Command Window on the right and two smaller windows stacked one atop the other on the left The Command Window is where calculations are carried out in MATLAB while the smaller windows display information about your current MATLAB session your previous MATLAB sessions and your computer account Your options for these smaller windows are Command History which displays the commands you7ve typed in from both the current and previous sessions Current Directory which shows which directory you7re currently in and what les are in that directory and Workspace which displays information about each variable de ned in your current session You can choose which of these options you would like to have displayed by selecting Desktop from the main MATLAB window and left clicking on the option MATLAB will place a black check to the left of this option Occasionally it will be important that you are working in a certain directory Notice that you can change MATLAB7s working directory by double clicking on a directory in the Current Directory window In order to go backwards a directory click on the folder with a black arrow on it in the top left corner of the Current Directory window 14 Basic Computations At the prompt designated by two arrows gtgt type 2 2 and press Enter You should nd that the answer has been assigned to the default variable ans Next type 22 and hit Enter Notice that the semicolon suppresses screen output in MATLAB We will refer to a series of commands as a MATLAB script For example we might type gtgtt4 gtgtssint MATLAB will report that s 7568 Notice that MATLAB assumes that t is in radians not degrees Next type the up arrow key on your keyboard and notice that the command ssint comes back up on your screen Hit the up arrow key again and t439 will appear at the prompt Using the down arrow you can scroll back the other way giving you a convenient way to bring up old commands without retyping them The left and right arrow keys will move the cursor left and right along the current line Occasionally you will nd that an expression you are typing is getting inconveniently long and needs to be continued to the next line You can accomplish this by putting in three dots and typing Enter Try the following1 gtgt234 56 ans 20 Notice that 234m was typed at the Command Window prompt followed by Enter When you do this MATLAB will proceed to the next line but it will not offer a new prompt This means that it is waiting for you to nish the line you7re working on 15 Variable Types MATLAB uses double precision oating point arithmetic accurate to approximately 15 dig its By default only a certain number of these digits are shown typically ve To display more digits type format long at the beginning of a session All subsequent numerical output will show the greater precision Type format short to return to shorter display MATLAB7s four basic data types are oating point which we7ve just been discussing symbolic see Section 2 character string and inlinc function A list of all active variablesialong with size and typeiis given in the Workspace Ob serve the differences for example in the descriptions given for each ofthe following variables gtgtt5 gtgtv125 gtgts7howdy7 gtgtysolve7ayb7 1In the MATLAB examples of these notes you can separate the commands llve typed in from MATLAB S responses by picking out those lines that begin with the command line prompt gtgti 4 16 Diary Files For many of the assignments this semester7 and also for the projects7 you will need to turn in a log of MATLAB commands typed and of MATLAB7s responses This is straightforward in MATLAB with the diary command Example 11 Write a MATLAB script that sets z 1 and computes tan l z or arctan Save the script to a le called SCTiptZtEt and print it In order to accomplish this7 we use the following MATLAB commands gtgtdiary script1txt gtgtX1 X 1 gtgtatan1 ans 07854 gtgtdiary off In this script7 the command diary SCTipt1tEt creates the le scriptlicct and MATLAB begins recording the commands that follow7 along with MATLAB7s responses When the command diary o is typed7 MATLAB writes the commands and responses to the le scriptlicct Commands typed after the diary o command will no longer be recorded7 but the le SCTipt1tEt can be reopened either with the command diary on or with diary scriptlicct Finally7 the diary le scriptlidt can be deleted with the command delete scriptlimt In order to print seripilicmt7 follow the reprint instructions posted in the Blocker lab More precisely7 open a terminal window by selecting the terminal icon from the bottom of your screen and use the reprint command reprint d blocker SCTipt1tEt You will be prompted to give your NetlD neo account ID and password The le will be printed in Blocker 133 A 17 Clearing and Saving the Command Window The Command Window can be cleared with the command ele7 which leaves your variable de nitions in place You can delete your variable de nitions with the command clear All variables in a MATLAB session can be saved with the menu option File7 Save Workspace As7 which will allow you to save your workspace as a mat le Later7 you can open this le simply by choosing File7 Open7 and selecting it A word of warning7 though This does not save every command you have typed into your workspace it only saves your variable assignments For bringing all commands from a session back7 see the discussion under Command History 18 The Command History The Command History window will open with each MATLAB session displaying a list of recent commands issued at the prompt Often you will want to incorporate some of these old commands into a new session An easy way to accomplish this is as follows right click on the command in the Command History and while holding the right mouse button down choose Evaluate Selection This is exactly equivalent to typing your selection into the Command Window 19 File Management from MATLAB There are certain commands in MATLAB that will manipulate les on its primary directory For example if you happen to have the le junkin in your working MATLAB directory you can delete it simply by typing delete junkin at the MATLAB command prompt Much more generally if you precede a command with an exclamation point MATLAB will read it as a unix shell command see Section 13 of these notes for more on Unix shell commands So for example the three commands ls lop junkin morejunkm and ls serve to list the contents of the directory you happen to be in copy the le junkin to the le morejunkm and list the les again to make sure its there 110 Getting Help As with any other software package the most important MATLAB command is help You can type this at the prompt just as you did the commands above For help on a particular topic such as the integration command int type help int If the screens input ies by too quickly you can stop it with the command more on Finally MATLAB has a nice help browser that can be invoked by typing helpdesk Lets get some practice with MATLAB help by computing the inverse sine of 7568 First we need to look up MATLAB7s expression for inverse sine At the prompt type helpdesk Next in the left hand window of the pop up menu click on the index tab second from left and in the data box type inverse In the box below your input you should now see a list of inverse subtopics Using your mouse scroll down to sine and click on it An example should appear in the right window showing you that MATLAB uses the function asin as its inverse for sine Close help by clicking on the upper right X as usual and at the prompt type asin 7568 The answer should be 8584 Pop quiz lf asin is the inverse of sin why isnt the answer 4 2 Symbolic Calculations in MATLAB Though MATLAB has not been designed with symbolic calculations in mind it can carry them out with the Symbolic Math Toolbox which is standard with student versions In order to check ifthis or any other toolbox is on a particular version of MATLAB type U67quot at the MATLAB prompt ln carrying out these calculations MATLAB uses Maple software but the user interface is signi cantly different 21 De ning Symbolic Objects Symbolic manipulations in MATLAB are carried out on symbolic variables7 which can be either particular numbers or unspeci ed variables The easiest way in which to de ne a variable as symbolic is with the syms command Example 21 Suppose we would like to symbolically de ne the logistic model N 1M aNlt1e g where N denotes the number of individuals in a population and R denotes the growth rate of the population First7 we de ne both the variables and the parameters as symbolic objects7 and then we write the equation with standard MATLAB operations gtgtsyms N R a K gtgtRaN1 NK R Bimini MK Here7 the expressions preceded by gtgt have been typed at the command prompt and the others have been returned by MATLAB A Symbolic objects can also be de ned to take on particular numeric values Example 22 Suppose that we want a general form for the logistic model7 but we know that the carrying capacity K is 107 and we want to specify this We can use the following commands gtgtKsym10 10 gtgtRaN1 NK aN1 110N 211 Complex Numbers You can also de ne and manipulate symbolic complex numbers in MATLAB Example 23 Suppose we would like to de ne the complex number 2 z W and compute 22 and 22 We use gtgtsyms x y real gtgtZxiy Z xiy gtgtsquareexpandz 2 square x222ixy y2 2 gtgtZzbarexpandzconj ZZbar x22y22 Here we have particularly speci ed that z and y be real as is consistent with complex notation The built in MATLAB command eonj computes the complex conjugate of its input and the err pand command is required in order to force MATLAB to multiply out the expressions The err pand command is discussed more below in Subsubsection 222 212 The Clear Command You can clear variable de nitions with the clear command For example if z is de ned as a symbolic variable you can type clear 5 at the MATLAB prompt and this de nition will be removed Clear will also clear other MATLAB data types If you have set a symbolic variable to be real you will additionally need to use syms cc unreal or the Maple kernel that MATLAB calls will still consider the variable real 22 Manipulating Symbolic Expressions Once an expression has been de ned symbolically MATLAB can manipulate it in various ways 221 The Collect Command The collect command gathers all terms together that have a variable to the same power Example 24 Suppose that we would like organize the expression fx xsin x3ew x2 by powers of x We use gtgtsyms x gtgtfxsinxx23expxx22 f xsinxx23expxx22 gtgtcollectf ans x26expxx24sinxxA3sinxexpxx 222 The Expand Command The expand command carries out products by distributing through parentheses7 and it also expands logarithmic and trigonometric expressions Example 25 Suppose we would like to expand the expression f em We use gtgtsyms x gtgtfexpxx22 expxxA 2 gtgtexpandf ans expx gtkexpx 2 223 The Factor Command The factor command can be used to factor polynomials Example 26 Suppose we would like to factor the polynomial f 4 7 2x2 1 We use gtsyms x gtfxA4 2x221 f xA4 2x221 gtfactorf ans x 1A2x1 2 224 The Homer Command The homer command is useful in preparing an expression for repeated numerical evaluation In particular7 it puts the expression in a form that requires the least number of arithmetic operations to evaluate Example 27 Re write the polynomial from Example 6 in Horner form gtgtsyms x gtgtfx 4 2x 21 f xA4 2x221 gtgthornerf ans 1 2x 2x 2 225 The Simple Command The simple command takes a symbolic expression and re writes it with the least possible number of characters It runs through MATLAB7s various manipulation programs such as collect7 cal pend7 and factor and returns the result of these that has the least possible number of characters Example 28 Suppose we would like a reduced expression for the function fx 7 1li1xz 7 z 2 39 We use gtgtsyms x f gtgtf11x1xo21xxo2 f 11x1x 2x1x 2 gtgtsimplef simplify x1x2222x22 radsimp x1x2222x22 combinetrig 3xA22x2x 31x24x22 factor x1x2222x22 expand 2x3x222x1x22 combine 11x1x 2x1x 2 co nvertexp 11x1x 2x1x 2 co nvertsincos 11x1x 2x1x 2 co nvert t an 11x1x 2x1x 2 collectx 2x3x 22x1x 2 mwcos2sin 11x1x 2x1x 2 ans x1x 2 A2x 2 In this example7 three lines have been typed7 and the rest is MATLAB output as it tries various possibilities In returns the expression in ms7 in this case from the factor command 226 The Pretty Command MATLAB7s pretty command simply re writes a symbolic expression in a form that appears more like typeset mathematics than does MATLAB syntax Example 29 Suppose we would like to re write the expression from Example 38 in a more readable format Assuming7 we have already de ned f as in Example 387 we use prettyf at the MATLAB prompt The output of this command doesn7t translate well into a printed document7 so I wont give it here 23 Solving Algebraic Equations MATLAB7s built in function for solving equations symbolically is solve Example 210 Suppose we would like to solve the quadratic equation azz bx c 0 We use gtgtsyms a b c x gtgteqnax 2bxc eqn ax 2bxc gtgtrootssolveeqn roots 12a bbA2 4acA12 12a b bA2 4acA12 Observe that we only de ned the expression on the left hand side of our equality By default7 MATLAB7s solve command sets this expression to 0 Also7 notice that MATLAB knew which variable to solve for It takes x as a default variable Suppose that in lieu of solving for L we know z and would like to solve for a We can specify this with the following commands gtgtasolve eqn7a a bxcx 2 In this case7 we have particularly speci ed in the solve command that we are solving for a Alternatively7 we can type an entire equation directly into the solve command For example gtgtsyms a gtgtrootssolveaxA2bxc roots 12a bbA2 4ac A12 12a b bA2 4acA12 Here7 the syms command has been used again because a has been rede ned in the code above Finally7 we need not rst make our variables symbolic if we put the expression in solve in single quotes We could simply use solve a A2bc A MATLAB7s solve command can also solve systems of equations Example 211 For a population of prey z with growth rate R1 and a population of predators y with growth rate Ry7 the the Lotka7Volterra predatoriprey model is Rm ax lmy Ry icydzy In this example7 we would like to determine whether or not there is a pair of population values Ly for which neither population is either growing or decaying the rates are both 0 We call such a point an equilibrium point The equations we need to solve are 0 wibzy 0 7cydxy ln MATLAB gtgtsymsabcdxy gtgtRxax bxy Rx ax bxy gtgtRy cydxy Ry Cgtltydgtkxgtky gtgt prey predsolveRx7Ry prey 0 1dc pred 0 1ba Again7 MATLAB knows to set each of the expression Pm and By to 0 In this case7 MAT LAB has returned two solutions7 one with 00 and one with 3 In this example7 the appearance of prey pred particularly requests that MATLAB return its solution as a vector with two components Alternatively7 we have the following gtgtpopssolveRX7Ry pops X 2X1 sym y 2X1 sym gtgtpopsX ans 0 1dc gtgtpopsy ans 0 1ba In this case7 MATLAB has returned its solution as a MATLAB structure7 which is a data array that can store a combination of different data types symbolic variables7 numeric values7 strings etc In order to access the value in a structure7 the format is structurenamevariableidenti cation A 24 Numerical Calculations with Symbolic Expressions In many cases7 we would like to combine symbolic manipulation with numerical calculation 241 The Double and Eval commands The double and eval commands change a symbolic variable into an appropriate double vari able ie7 a numeric value Example 212 Suppose we would like to symbolically solve the equation 3 2x 7 1 07 and then evaluate the result numerically We use gtgtsyms X gtgtFSOlVeX632X 1 gtgtevalr ans 04534 02267 146771 02267 146771 gtgtdoubler ans 04534 02267 146771 02267 146771 MATLAB7s symbolic expression for r is long so I havent included it here but you should take a look at it by leaving the semicolon off the solve line A 242 The Subs Command In any symbolic expression values can be substituted for symbolic variables with the subs command Example 213 Suppose that in our logistic model N RN aN17 X we would like to substitute the values a 1 and K 10 We use gtgtsyms a K N gtgtRaN1 NK R aN1 NK gtgtRsubsRa1 R 110N1 NK gtgtRsubsRK10 R 110N1 110N Alternatively numeric values can be substitued in We can accomplish the same result as above with the commands gtgtsyms a K N gtgtRaN1 NK aN1 NK gtgta1 a 01000 gtgtK10 K 10 gtgtRsubsR R 110N1 110N In this case the speci cations 1 1 and K 10 have de ned a and K as numeric values The subs command however places them into the symbolic expression 3 Plots and Graphs in MATLAB The primary tool we will use for plotting in MATLAB is pl0t Example 31 Plot the line that passes through the points 14 3 We rst de ne the x values 1 for the rst point and 3 for the second as a single variable x 1 3 typically referred to as a vector and the y values as the vector y 4 6 and then we plot these points connecting them with a line The following commands accompanied by MATLAB7s output suf ce gtgtx1 3 X 1 3 gtgty4 6 4 6 gtgtplotXy The output we obtain is the plot given as Figure 1 n u m X 2 22 2 2n n Figure 1 A very simple linear plot ln MATLAB it7s particularly easy to decorate a plot For example7 minimize your plot by clicking on the left button on the upper right corner of your window7 then add the following lines in the Command Window gtgtxlabel7Here is a label for the x axis7 gtgtylabel7Here is a label for the y axis7 gtgttitle7Useless Plot7 gtgtaxis0 4 210 The only command here that needs explanation is the last It simply tells MATLAB to plot the x axis from 0 to 47 and the y axis from 2 to 10 If you now click on the plots button at the bottom of the screen7 you will get the labeled gure7 Figure 2 mm in 35 a as as i 5 2 25 M a mm 5 Figure 2 A still pretty much ridiculously simple linear plot I added the legend after the graph was printed7 using the menu options Notice that all this labeling can be carried out and edited from these menu options After experimenting a little7 your plots will be looking great or at least better than the default setting gures displayed here Not only can you label and detail your plots7 you can write and draw on them directly from the MATLAB window One warning If you retype pl0ty after labeling7 MATLAB will think you want to start over and will give you a clear gure with nothing except the line To get your labeling back7 use the up arrow key to scroll back through your commands and re issue them at the command prompt Unless you labeled your plots using menu options7 in which case you7re out of luck7 though this might be a good time to consult Section 36 on saving plots De ning vectors as in the example above can be tedious if the vector has many compo nents7 so MATLAB has a number of ways to shorten your work For example7 you might try gtgtX19 X 31 Plotting Functions with the plot command In order to plot a function with the plot command we proceed by evaluating the function at a number of a values zlx2n and drawing a curve that passes through the points xkykz1 where yk Example 32 Use the plot command to plot the function f 2 for z E 01 First we will partition the interval 01 into twenty evenly spaced points with the com mand lmspacem Z 20 The command lmspaceabrz de nes a vector with n evenly spaced points beginning with left endpoint a and terminating with right endpoint b Then at each point we will de ne f to be 2 We have gtgtXlinspace0l20 X Columns 1 through 8 0 00526 01053 01579 02105 02632 03158 03684 Columns 9 through 16 04211 04737 05263 05789 06316 06842 07368 07895 Columns 17 through 20 08421 08947 09474 10000 gtgtfx02 f Columns 1 through 8 0 00028 00111 00249 00443 00693 00997 01357 Columns 9 through 16 01773 02244 02770 03352 03989 04681 05429 06233 Columns 17 through 20 07091 08006 08975 10000 gtgtplotxf Only three commands have been typed MATLAB has done the rest One thing you should pay close attention to is the line fXA2 where we have used the array operation 0 This operation 0 signi es that the vector x is not to be squared a dot product yielding a scalar but rather that each component of a is to be squared and the result is to be de ned as a component of f another vector Similar commands are and These are referred to as array operations and you will need to become comfortable with their use Example 33 In our section on symbolic algebra we encountered the logistic population model which relates the number of individuals in a population N with the rate of growth of the population B through the relationship N a 2 RN 7 aN1 g 7 XN aN Taking a l and K 10 we have RN 71N2 N In order to plot this for populations between 0 and 20 we use the following MATLAB code which creates Figure 3 gtgtNlinspace0201000 gtgtR 1NA2N gtgtplotNR Observe that the rate of growth is positive until the population achieves its carrying capac ity77 of K 10 and is negative for all populations beyond this In this way if the population is initially below its carrying capacity then it will increase toward its carrying capacity but will never exceed it If the population is initially above the carrying capacity it will decrease toward the carrying capacity The carrying capacity is interpreted as the maximum number of individuals the environment can sustain A Figure 3 Growth rate for the logistic model 32 Parametric Curves In certain cases the relationship between z and y can be described in terms of a third variable say t In such cases It is a parameter and we refer refer to a plot of the points Ly as a parametric curve Example 34 Plot a curve in the x y plane corresponding with t t2 1 and yt t for t E 711 One way to accomplish this is through solving for t in terms of z and substituing your result into yt to get y as a function of x Here rather we will simply get values of z and y at the same values of t Using semicolons to suppress MATLAB7s output we use the following script which creates Figure 4 gtgttlinspace 11100 gtgtXtA2 1 gtgtyexpt gtgtplotxy Figure 4 Plot of t t2 1 and yt t for t E 7171 33 Juxtaposing One Plot On Top of Another Example 35 For the functions t t2 1 and yt 6 7 plot t and yt on the same gure7 both versus t The easiest way to accomplish this is with the single command gtgtplot t7x7t7y The color and style of the graphs can be speci ed in single quotes directly after the pair of values For example7 if we would like the plot of t to be red7 and the plot of yt to be green and dashed7 we would use gtgtplott7x77r77t7y77g 7 For more information on the various options7 type help plot Another way to accomplish this same thing is through the hold on command After typing hold oh7 further plots will be typed one over the other until the command hold o is typed For example7 gtgtplott7x2 gtgthold on gtgtplot t7y gtgttitle7One plot over the other7 gtgtu 1 0 1 gtgtv1 0 1 gtgtplotu7V 21f a plot window pops up here7 minimize it and bring it back up at the end 19 34 Multiple Plots Often7 we will want MATLAB to draw two or more plots at the same time so that we can compare the behavior of various functions Example 36 Plot the three functions f x g 2 and h 3 The following sequence of commands produces the plot given in Figure 5 gtgtX linspace0l20 gtgtf X gtgtg xf2 gtgth Xf3 gtgtsubplot 311 gtgtplotxf gtgtsubplot gtgtplotXg gtgtsubplot 713 gtgtplotxh AVA 7172 V Figure 5 Algebraic functions on parade The only new command here is subpl0tmnp This command creates m rows and n columns of graphs and places the current gure in position p counted left to right7 top to bottom 35 Ezplot In most of our plotting for M1517 we will use the plot command7 but another option is the built in function 2171th7 which can be used along with symbolic variables Example 37 Plot the function f 4 2mg 7 7 We can use gtgtsyms f X gtgtfX 42XA3 7X f XA42XA3 7X gtgteZplotf In this case7 MATLAB chooses appropriate axes7 and we obtain the plot in Figure 6 MM7x 1600 1400 1200 Figure 6 Default plot from ezplot We can also specify the domain on which to plot with ezpl0t mmcvmacv For example7 ezplot ZZ creates Figure 7 Alternatively7 the variables need not be de ned symbolically if they are placed in single quotes We could also plot this example using respectively gtgteZplOtlt7XA42XA3 7X7 or gtgteZplOtlt7XA42XA3 7X77 171 A The ezplot command can also be a good way for plotting implicitly de ned relations7 by which we mean relations between z and y than cannot be solved for one variable in terms of the other Example 38 Plot y versus z given the relation MM7x Figure 7 Domain speci ed plot with ezplot This is7 of course7 the equation of an ellipse7 and it can be plotted by separately graphing each of the two solution curves 2 i2 1 i 7 y V 9 Alternatively7 we can use the following single command to create Figure 8 gtgteZplot7XA29yA24177 3737 272 Here7 observe that the rst interval speci es the values of z and the second speci es the values for y Finally7 we can use ezplot to plot parametrically de ned relations Example 39 Use ezplot to plot y versus 7 given zt t21 and yt at for t E 711 We can accomplish this with the single command gtgteZplot7t 21777expt77 1 71 36 Saving Plots as Encapsulated Postscript Files In order to print a plot7 rst save it as an encapsulated postscript le From the options in your graphics box7 choose File7 Save As7 and change Save as type to EPS le Finally7 click on the Save button The plot can now be printed using the sprint command Once saved as an encapsulated postscript le7 the plot cannot be edited7 so it should also be saved as a MATLAB gure This is accomplished by choosing File7 Save As7 and saving the plot as a g le which is MATLAB7s default 22 xZ9yZ4l Figure 8 The ellipse described by 32 1 4 Semilog and Doublelog Plots In many applications7 the values of data points can range signi cantly7 and it can become convenient to work with logl0 values of the original data In such cases7 we often work with semilog or double log or log log plots 41 Semilog Plots Consider the following data real and estimated for world populations in certain years We can plot these values in MATLAB with the following commands7 which produce Figure 9 gtgtyears 4000 2000 1 2000 gtgtpops7e6 27e7 17e8 61e9 gtgtplotyearspops7o7 Looking at Figure 97 we immediately see a problem the nal data point is so large that the remaining points are effectively zero on the scale of our graph In order to overcome this 7 m 7 Q 7 4 00 3000 2000 l000 0 1000 2000 Figure 9 Standard plot for populations versus year problem7 we can take a base 10 logarithm of each of the population values That is7 logo 7 gtlt 106 logo 7 6 logo 27 gtlt107 loglO 27 7 logo 17 gtlt108 loglO 17 8 loglo 61 gtlt109 loglo 61 9 We can plot these new values with the following commands gtgtlogpopslog10pops gtgtplotyearslogpops7o7 In this case7 we obtain Figure 10 We can improve this slightly with MATLAB7s built in function semilogy This function carries out the same calculation we just did7 but MATLAB adds appropriate marks on the vertical axis to make the scale easier to read We use gtgtsemilogyyearspops707 The result is shown in Figure 11 Observe that there are precisely eight marks in Figure 11 between 107 and 108 The rst of these marks 2 gtlt 1077 the second 3 gtlt 107 etc up to the eighth7 which is 9 gtlt 107 At that point7 we have reached the mark for 108 411 Deriving Functional Relations from a Semilog Plot Having plotted our population data7 suppose we would like to nd a relationship of the form 24 54000 3000 2000 1000 0 1000 2000 Figure 10 Plot of the log of populations versus years 10 109 O 108 O 1 10 105 4000 3000 2000 1000 0 1000 2000 Figure 11 Semilog plot of world population data where N denotes the number of individuals in the population during year x We proceed by observing that the four points in Figure 11 all lie fairly close to the same straight line In Section 7 we will discuss how calculus can be used to nd the exact form for such a line7 but for now we simply allow MATLAB to carry out the computation From the graphics window for Figure 10 the gure created prior to the use of semz39logy7 choose Tools7 Basic Fitting From the Basic Fitting rnenu7 choose a Linear t and check the box next to Show Equations This produces Figure 12 y000040 x06 quot9 95 54000 3000 2000 l000 0 1000 2000 Figure 12 Best line t for the population data This line suggests that the relationship between N and z is logl0 N 00048 86 Recall that we obtained this gure by taking logl0 of our data Taking each side of this last expression as an exponent for the base 107 we nd 10bng 1000048m86 1000048m108639 We conclude with the functional relation NW 1000048110867 which is the form we were looking for Finally7 we note that MATLAB7s built in function semilagm plots the s axis on a loga rithrnic scaling while leaving the y axis in its original form 42 Doublelog Plots In the case that we take the base 10 logarithm of both variables in the problern7 we say that the plot is a double log or log log plot Example 41 In certain cases7 the number of plants in an area will decrease as the average size of the individual plants increases Since each plant is using more resources7 fewer plants can be sustained In order to nd a quantitative relationship between the number of plants N and the average plant size S7 consider the data given in Table 1 Table 1 Number of plants N and average plant size S In this case7 we will nd a relationship between N and S of the form 5 f N We proceed by taking the base 10 logarithm of all the data and creating a double log plot of the resulting values The following MATLAB code produces Figure 13 gtgtNl 10 50100 gtgtS10000 31623 2828 10 gtgtloglogNS Figure 13 Double log plot of average plant size S versus number of plants N Since the graph of the data is a straight line in this case3 we can compute the slope and intercept from standard formulas In standard slope intercept forrn7 we can write the 3Cooked up7 admittedly7 though the relationship we7ll get in the end is fairly general 27 equation for our line as log10 S m log10 N b The slope is 12 11 m 7 2 7 1 where zhyl and x27y2 denote two points on the line7 and b is the value of logl0 S when N 1 because logl0 1 0 In reading the plot7 notice that values 10k should be interpreted simply as k That is7 471 7717777 072 2 and b4 We conclude 3 loglo S ii loglo N 4 In order to get a functional relationship of the type we are interested in7 we take each side of this last expression as an exponent for the base 10 That is7 1010ng 10 310g10N4 1010gN gio4 S 104N 3 ln practice7 the multiplication factor 104 varies from situation to situation7 but the power law N is fairly common We often write SaN E 5 Inline Functions and M les Functions can be de ned in MATLAB either in line that is7 at the command prompt or as M les separate text les 51 Inline Functions Example 51 De ne the function f em in MATLAB and compute f1 We can accomplish this7 as follows7 with MATLAB7s built in mlme function gtgtfinline7expx 7 gtgtf1 ans 27183 Observe7 in particular7 the difference between f1 when f is a function and f1 when f is a vector if f is a vector7 then f1 is the rst component of f7 not the function f evaluated A at 1 In a similar manner7 we can de ne a function of several variables Example 52 De ne the function ay x2 y2 in MATLAB and compute f17 2 In this case7 we use gtgtfinline7x 2 y 2777X7y7 f lnline function fxy xA2 y 2 gtgtf12 ans 5 Notice that in the case of multiple variables we specify the order in which the variables will appear as arguments of f Compare the previous code with the following7 in which MATLAB expects y as the rst input of f and x as the second4 finline7xA2y 2777y777x7 lnline function fltyxgt x 2y 2 A In many cases we would like to de ne functions that use MATLAB7s array operations A and This can be accomplished either by typing the array operations in by hand or by using the vectom39ze command Example 53 De ne the function f x2 in MATLAB in such a way that MATLAB can take vector input and return vector output Compute f if z is the vector x 17 2 We use gtgtfinlinevectorize7x 27 f lnline function f1 x xf2 gtgtx1 2 X l 2 gtgtfx ans l 4 4Granted7 in this example order doesnlt matteri A Finally in some cases it is convenient to de ne an inline function when the variables are symbolic Since the mlme function expects a string or character as input we rst convert the symbolic expression into a string expression Example 54 Compute the inverse of the function f 96 7 gt 71 1 7 7 and de ne the result as a MATLAB inline function Compute f 15 We use gtgt nvsolve71x1y7 nv lty1gty gtgt nvinlinechar nv nv lnline function nVY Y1y gtgt nv5 ans 08000 Observe that the variable m is originally de ned symbolically even though the expression MATLAB solves is given as a string The char command converts nv into a string which is appropriate as input for inline lnline functions can be plotted with either the ezplot command or the fplot function plot command Example 55 De ne the function fx z sinx as an inline function and plot if for z E 0 27f using rst the ezplot command and second the fplot command The following commands create respectively Figure 14 and Figure 15 gtgtfinline7xsinx7 f lnline function fx xsinx gtgtezplotf0 2pil gtgtfplotf0 2pil xsmx Figure 14 Plot of f z sinx using ezplot Figure 15 Plot of f z sinx using fplot 52 Script MFiles The heart of MATLAB lies in its use of M les We will begin with a scn39pt M le which is simply a text le that contains a list of valid MATLAB commands To create an M le click on File at the upper left corner of your MATLAB window then select New followed by M le A window will appear in the upper left corner of your screen with MATLAB7s default editor You are free to use an editor of your own choice but for the brief demonstration here let7s stick with MATLABs In this window type the following lines x linspace02pi50 f sinx plotxf Save this le by choosing File Save As from the main menu In this case save the le as sineplotm and then close or minimize your editor window Back at the command line type sineplot at the prompt and MATLAB will plot the sine function on the domain 0 27f It has simply gone through your le line by line and executed each command as it came to it 53 Function M les The second type of M le is called a function M le and typically though not inevitably these will involve some variable or variables sent to the M le and processed As our rst example we will write a function M le that takes as input the number of points for our sine plot from the previous section and then plots the sine curve We can begin by typing gtgtedit sineplot ln MATLAB7s editor revise your le sineplotm so that it has the following form function sineplotn x linspace02pin f sinx plotxf Every function M le begins with the command function and the input is always placed in parentheses after the name of the function M le Save this le as before and then run it with 5 points by typing gtgtsineplot5 In this case the plot should be fairly poor so try it with 50 points ie use sinepl0t50 We can also take several inputs into our function at once As an example suppose that we want to take the left and right endpoints of our plotting interval as input as well as the number of points We use function sineplotabn X linspaceabn f sinX plOtltX7f Here7 observe that order is important7 so when you call the function you will need to put your inputs in the same order as they are read by the M le For eXample7 to again plot sine on 07 27f7 we use gtgtsineplot02pi750 MATLAB can also take multiple inputs as a vector Suppose the three values 07 27f7 and 50 are stored in the vector 1 That is7 in MATLAB you have typed gtgtv072pi750 In this case7 we write a function M le that takes 1 as input and appropriately places its components function sineplotv X linspacev17v27v3 f sinX plOtltX7f 54 Functions that Return Values In the function M les we have considered so far7 the les have taken data as input7 but they have not returned values In order to see how MATLAB returns values7 suppose we want to compute the maximum value of sinz on the interval over which we are plotting it Change smeplotm as follows function maXvalue sineplotv X linspacev17v27v3 f sinX maXvalue maXf plOtltX7f In this new version7 we have made two important changes First7 we have added mad value to our rst line7 specifying that the value we want MATLAB to return is the one we compute as maXvalue Second7 we have added a line to the code that computes the maXimum of f and assigns its value to the variable mamalue The MATLAB function mam takes vector input and returns the largest component When running an M le that returns data from the command window7 you will typically want to assign the returned value a designation Here7 you might use gtgtmsineplotv The maximum of sinx on this interval will be recorded as the value of m MATLAB can also return multiple values Suppose we would like to return both the maximum and the minimum of f in this example We use function minvaluemaxvalue sineplotv x linspacev1v2v3 f sinx minvalue minf maxvalue maxf plotxf In this case at the command prompt type gtgtmnsineplotv The value of m will now be the minimum of sinx on this interval while 71 will be the maximum As our last example we will write a function M le that takes vector input and returns vector output In this case the input will be as before and we will record the minimum and maximum of f in a vector We have function w sineplotv x linspacev1v2v3 f sinx w minfmaXfli plotxf This function can be called with gtgtbsineplotv where it is now understood that b is a vector with two components 55 Subfunctions Function M les can have subfunctions script M les cannot have subfunctions In the following example the subfunction sub m simply squares the input x function value subfunexx SUBFUNEX Function M le that contains a subfunction value xsubfunx function value subfunx SUBFUN Subfunction that computed xA2 value xA2 For more information about script and function M les see Section 6 of these notes on Programming in MATLAB 56 Debugging M les Since MATLAB views M les as computer programs7 it offers a handful of tools for debug ging First7 from the M le edit window7 an M le can be saved and run by clicking on the icon with the white sheet and downward directed blue arrow alternatively7 choose Debug Run or simply type F5 By setting your cursor on a line and clicking on the icon with the white sheet and the red dot7 you can set a marker at which MATLAB7s execution will stop A green arrow will appear7 marking the point where MATLAB7s execution has paused At this point7 you can step through the rest of your M le one line at a time by choosing the Step icon alternatively Debug Step or F6 Unless you7re a phenomenal programmer7 you will occasionally write a MATLAB program M le that has no intention of stopping any time in the near future You can always abort your program by typing Controlc7 though you must be in the MATLAB Command Window for MATLAB to pay any attention to this If all else fails7 ControlAltBackspace will end your session on a calclab account 6 Basic Calculus Of course7 MATLAB comes equipped with a number of tools for evaluating basic calculus expressions 61 Differentiation Symbolic derivatives can be computed with dz Y To compute the derivative of 3 type gtgtsyms X gtgtdiffx 3 ans 362 Alternatively7 you can rst de ne x3 as a function of f gtgtfinline7X637X7 gtgtdifffx ans 362 Higher order derivatives can be computed simply by putting the order of differentiation after the function7 separated by a comma gtgtdifffx2 ans 6X Finally7 MATLAB can compute partial derivatives See if you can make sense ofthe following input and output gtgtsyms Y gtgtginline7xA2y 2777x777y7 lnline function aw XA2yA2 gtgtdiffgx7y7Y ans 2XA2y 62 Integration Symbolic integration is similar to symbolic differentiation To integrate 27 use gtgtsyms X gtgtintXA2 ans 13XA3 or gtgtfinline7x 2777x7 f lnline function fx XA2 gtgtintfx ans 13XA3 The integration with limits fol zzdm can easily be computed if f is de ned inline as above gtgtintfx7071 ans 13 For double integrals7 such as fol fgimz2 y2dydz7 simply put one mt inside another gtgtsyms y gtgtintintXA2 y 27y707sinx707pi ans piA2 329 Numerical integration is accomplished through the commands quad7 quadv7 and quadl For example7 quadlVectorize7exp XA477071 ans 08448 If x has been de ned as a symbolic variable7 you dont need the single quotes You might also experiment with the numerical double integration function dblquad Notice that the function to be numerically integrated must be a vector hence7 the vectom39ze command ln particular7 the vectorize command changes all operations in an expression into array oper ations For more information on vectom39ze7 type help vectom39ze at the MATLAB Command Window 63 Limits MATLAB can also compute limits7 such as sin z lim 1 410 z We have7 gtgtsyms X gtgtl1m1tltS1HXX7X70 ans 1 For left and right limits l1m m i 1 li m 1 maO s 410 s we have gtgtlimitabsxXX7077left7 ans 1 gtgtlimitabsxXX7077right7 ans 1 Finally7 for in nite limits of the form x4z27371 l 3x4 7 logx 37 we can type gtgtlimitXA4 XA2 33XA4 logXXlnf ans 13 64 Sums and Products We often want to take the sum or product of a sequence of numbers For example7 we might want to compute 7 Zn 28 n1 We use MATLAB7s sum command gtgtX17 X Similarly7 for the product 7 Hn123456750407 n1 we have gt gt pro d X ans 5040 MATLAB is also equipped for evaluating sums symbolically Suppose we want to evaluate 1 1 1 8 m 1 1 k1 n We type gtgtsyms k n gtgtsymsum1k 1k1717n ans 1n11 65 Taylor series Certainly one of the most useful tools in mathematics is the Taylor expansion7 whereby for certain functions local information at a single point can be used to obtain global information in a neighborhood of the point and sometimes on an in nite domain The Tayor expansion for sinx up to tenth order can be obtained through the commands gtgtsyms x gtgttaylor sinx x10 ans x 16xo31120xA5 15040x 71362880XA9 We can also employ MATLAB for computing the Taylor series of a function about points other than 05 For example the rst four terms in the Taylor series of em about the point z 2 can be obtained through gtgttaylorexpx42 exp exp2x 212exp2x 2 A216exp2x 2 A3 66 Maximization and Minimization MATLAB has several built in tools for maximization and minimization One of the most direct ways to nd the maximum or minimum value of a function is directly from a MATLAB plot In order to see how this works create a simple plot of the function fx sinx 7 z for z E 0 gtgtxlinspace0pi225 gtgtfsinx 2pi x gtgtplotxf Now in the graphics menu choose Tools Zoom In Use the mouse to draw a box around the peak of the curve and MATLAB will automatically redraw a re ned plot By re ning carefully enough and choosing a suf cient number of points in our lmspace command we can determine a fairly accurate approximation of the functions maximum value and of the point at which it is achieved In general we will want a method more automated than manually zooming in on our solution MATLAB has a number of built in minimizers fmmlme fmmuncO and fmm search For straightfoward examples of each of these use MATLAB7s built in help For a more complicated example of fmmsearchO see Example 27 of our course notes Modeling Basics In either case we rst need to study MATLAB M les so we will consider that topic next 7 Matrices We cant have a tutorial about a MATrix LABoratory without making at least a few com ments about matrices We have already seen how to de ne two matrices the scalar or 1 gtlt 1 matrix and the row or 1 gtlt 71 matrix a row vector as in Section 41 A column vector or matrix can be de ned similarly by 5You may recall that the Taylor series of a function about the point 0 is also referred to as a Maclaurin series gtgtX1 2 3 This use of semicolons to end lines in matrices is standard7 as we see from the following MATLAB input and output gtgtA12 3 45 6 789 1 2 3 4 5 6 7 8 9 gtgtA22 ans 5 gtgtdetA ans 0 gtgtB12 2112033 B 1 2 2 1 1 2 0 3 3 gtgtdetl3 ans 3 gtgtB 1 ans 10000 00000 06667 10000 10000 0 10000 10000 03333 gtgtAB ans 3 13 15 9 31 36 15 49 57 gtgtAB ans 1 4 6 4 5 12 0 24 27 Note in particular the difference between A gtk B and A gtk B A convention that we will nd useful while solving ordinary differential equations numer ically is the manner in which MATLAB refers to the column or row of a matrix With A still de ned as above7 Amn represents the element of A in the mth row and nth column If we want to refer to the rst row of A as a row vector7 we use A17 7 where the colon represents 40 that all columns are used Similarly7 we would refer to the second column of A as A7 2 Some examples follow gtgtA12 ans 2 gtgtA21 ans 4 gtgtA1 ans 1 2 3 gtgtA2 ans 2 5 8 Finally7 adding a prime 7 to any vector or matrix de nition transposes it switches its rows and columns gtgtA7 ans 1 4 7 2 5 8 3 6 9 gtgtX1 2 3 X 8 Programming in MATLAB 81 Overview Perhaps the most useful thing about MATLAB is that it provides an extraordinarily con venient platform for writing your own programs Every time you create an M le you are writing a computer program using the MATLAB programming language If you are familiar with C or C7 you will nd programming in MATLAB very similar6 And if you are famil iar with any programming languageiFortran7 Pascal7 Basic7 even antiques like Coboliyou 6ln fact7 its possible to incorporate C or C programs into your MATLAB document 41 shouldnt have much trouble catching on In this section I will run through the basic com mands you will need to get started in programming Some of these you have already seen in one form or another on previous pages 82 Loops 821 The For Loop One of the simplest and most fundamental structures is the for loop exempli ed by the MATLAB code f1 for n25 ffn end The output for this loop is given below f 2 f 6 f 24 f 120 Notice that l7ve dropped off the command prompt arrows because typically this kind of structure is typed into an M le not in the Command Window 1 should point out however that you can type a for loop directly into the command line What happens is that after you type for 7125 MATLAB drops the prompt and lets you close the loop with end before responding By the way if you try typing this series of commands in MATLAB7s default editor it will space things for you to separate them and make the code easier to read One nal thing you should know about for is that if you want to increment your index by something other than 1 you need only type for example for 16412150 which counts from 4 the rst number to 50 the last number by increments of 2 822 The While Loop One problem with for loops is that they generally run a predetermined set of times While loops on the other hand run until some criterion is no longer met We might have x1 while xlt3 xx1 if x gt 100 break end end The output for this loop is given below x 2 x 3 Since while loops don7t necessarily stop after a certain number of iterations7 they are notori ous for getting caught in in nite loops In the example above l7ve stuck a break command in the loop7 so that if l7ve done something wrong and 5 gets too large7 the loop will be broken 83 Branching Typically7 we want a program to run down different paths for different cases We refer to this as branching 831 If Else Statements The most standard branching statement is the if else A typical example7 without output7 is given below if X gt 0 y Xi elseif X 0 y 1 else y 0 end The spacing here is MATLAB7s default Notice that when comparing 5 with 07 we use instead of simply This is simply an indication that we are comparing 5 with 07 not setting 5 equal to 0 The only other operator that probably needs special mention is quot for not equal You probably wouldnt be surprised to nd out what things like lt7 lt7 gt7 gt mean Finally7 observe that elsez39f should be typed as a single word MATLAB will run les for which it is written as two words7 but it will read the if in that case as beginning an entirely new loop inside your current loop 832 Switch Statements A second branching statement in MATLAB is the switch statement Switch takes a variablei 5 in the case of the example belowiand carries out a series of calculations depending on what that variable is In this example if 5 is 7 the variable y is set to 1 while if 5 is 10 or 17 then y is set to 2 or 3 respectively switch X case 7 84 Input and Output 841 Parsing Input and Output Often you will nd it useful to make statements contingent upon the number of arguments owing in to or out of a certain function For this purpose MATLAB has nargin and nargout which provide the number of input arguments and the number of output arguments respectively The following function addthree accepts up to three inputs and adds them together If only one input is given it says it cannot add only one number On the other hand if two or three inputs are given it adds what it has We have function saddthreex y Z ADDTHREE Example for nargin and nargout if nargin lt 2 error7Need at least two inputs for adding end if nargin 3XY else s xyZ end Working at the command prompt now we nd gtgtaddthree1 77 Error using gt addthree Need at least two inputs for adding gtgtaddthree12 ans 3 gtgtaddthree123 ans 6 Notice that the error statement is the one we supplied 1 should probably mention that a function need not have a xed number of inputs The command uarargm allows for as many inputs as the user will supply For example the following simple function adds as many numbers as the user supplies function saddallVarargin ADDALL Example for nargin and nargout ssumVarargin Working at the command prompt we nd gtgtaddall1 ans l gtgtaddall12 ans 3 gtgtaddall12345 ans 15 842 Screen Output Part of programming is making things user friendly in the end and this means controlling screen output MATLAB7s simplest command for writing to the screen is disp gtgtx 22 gtgtdisp7The answer is 7 num2strx 17 The answer is 4 In this case uqustrO converts the number x into a string appropriate for printing 843 Screen Input Often you will want the user to enter some type of data into your program Some useful commands for this are pause keyboard and input Pause suspends the program until the user hits a key while keyboard allows the user to enter MATLAB commands until he or she types return As an example consider the M le EXAMPLE A script le with examples of pause keyboard and input disp7Hit any key to continue7 pause disp7Enter a command Type 77return77 to return to the script le7 keyboard answerinput7Are you tired of this yet yesno777 7s7 if isequal answer77yes7 return end Working at the command prompt7 we have7 gtgteXample Hit any key to continue Enter a command Type 77return77 to return to the script le gtgt34 ans 7 gtgtreturn Are you tired of this yet yesno7yes 844 Screen Input on a Figure The command gmput can be used to put input onto a plot or graph The following function M le plots a simple graph and lets the user put an X on it with a mouse click function example EXAMPLE Marks a spot on a simple graph pl1 2 3l ql1 2 3l p10tp7q hold on disp7Click on the point where you want to plot an X X yginput1 Gives X and y coordinates to point mateyX16 hold off 9 Miscellaneous Useful Commands In this section I will give a list of some of the more obscure MATLAB commands that I nd particularly useful As always7 you can get more information on each of these commands by using MATLAB7s help command 0 strcmpstr1str2 string compare Compares the strings str and StTQ and returns logical true 1 if the two are identical and logical false 0 otherwise 0 cha mput Converts just about any type of variable input into a string character array o numgstrmum Converts the numeric variable num into a string 0 str2numstr Converts the string str into a numeric variable See also steroubleO o strcatstr1str2 Horizontally concatenates the strings str 31762 etc 10 Graphical User Interface Ever since 1984 when Apple7s Macintosh computer popularized Douglas Engelbart7s mouse driven graphical computer interface users have wanted something fancier than a simple command line Unfortunately actually coding this kind of thing yourself is a full time job This is where MATLAB7s add on GUIDE comes in Much like Visual C GUIDE is a package for helping you develop things like pop up windows and menu controlled les To get started with GUIDE simply choose File New GUI from MATLAB7s main menu 1 1 SIMULINK SIMULINK is a MATLAB add on taylored for visually modeling dynamical systems To get started with SIMULINK choose File New Model 12 Mbook M book is a MATLAB interface that passes through Microsoft Word apparently allowing for nice presentations Unfortunately my boycott of anything Microsoft precludes the possibility of my knowing anything about it 13 Useful Unix Commands In Linux you can manipulate les create directories etc using menu driven software such as Konqueror off the Internet sub menu Often the fastest way to accomplish simple tasks is still from the Unix shell To open the Unix shell on your machine simply click on the terminalseashell icon along the bottom of your screen or from the System sub menu choose Terminal A window should pop up with a prompt that looks something like usemam6 Here you can issue a number of useful commands of which I7ll discuss the most useful for our purposes Commands are listed in bold lenames and directory names in italics 0 cat lename Prints the contents of a le lename to the screen 0 cd dimame Changes directory to the directory dimame o mkdir dimame Creates a directory called dimame 0 cp lename lenameg Copies a le named lename into a le name lenameQ creating lenameQ o ls Lists all les in the current directory 0 rm lename Removes deletes the le lename o quota Displays the number of blocks your currently using and your quota Often7 when your account crashes7 its because your quota has been exceeded Typically7 the system will allow you to long into a terminal screen to delete les 0 du s Summarizes disk usage of all les and subdirectories 0 nd name tag Finds all les ending tag7 in all directories man ls Opens the unix on line help manual information on the command ls Think of it as typing help ls Of course7 man works with any other command as well Use 1 to exit 0 man k jitterbug Searches the unix manual for commands involving the keyword jitterbug Oddly7 there are no matches7 but try7 for example7 man copy 131 Creating Unix Commands Sometimes you will want to write your own Unix commands7 which similar to MATLAB7s M les simply run through a script of commands in order For example7 use the editor of your choice even MATLAB7s will do to create the following le7 named myhelp Unix script le with a list of useful commands echo Useful commands echo echo cat Prints the contents of a le to the screen echo cd Changes the current directory echo echo You can also issue commands with a Unix script ls Any line in a Unix script le that begins with is simply a comment and will be ignored The command echo is Unix7s version of print Finally7 any command typed in will be carried out Here7 l7ve used the list command To run this command7 type either simply myhelp if the Unix command path is set on your current directory or Nmyhelp if the Unix command path is not set on your current directory 132 More Help on Unix Unix help manuals are among the fattest books on the face of the planet7 and they7re easy to nd Typically7 however7 you will be able to nd all the information you need either in the on line manual or on the Internet One good site to get you started is httpwwwmCST0lemlsseduunlcchelp References P R Pratap7 Getting Started with MATLAB 5 A Quick Introduction for Scientists and Engineers Oxford University Press7 1999 HL D Hanselrnan and B Little eld7 Mastering MATLAB 5 A Comprehensive Tutorial and Reference Prentice Hall7 1998 UNH httpspiceracksrunhedurnathadrntutorialsoftwarernatlab HLR B R Hunt7 R L Lipsrnan7 and J M Rosenberg with K R Coornbes7 J E Os born7 and G J Stuck7 A Guide to MATLAB for beginners and edperienced users7 Cambridge University Press 2001 MAT httpwwwrnathworkscorn asin7 6 axis7 16 branching7 43 break7 43 char7 46 character string7 4 clear7 8 collect7 8 Command History7 6 command window7 5 complex numbers7 7 continuing a line7 4 dblquad7 37 det7 40 difl7 35 differentiation7 35 disp7 45 double7 13 eval7 13 expand7 9 exporting graphs as eps les7 22 ezplot7 20 factor7 9 oating point7 4 for7 42 formatting output7 4 fplot7 30 ginput7 46 graphical user interface7 47 graphs saVing7 22 help7 6 helpdesk7 6 hold on7 19 horner7 10 if else7 43 inline function7 47 28 input7 45 integration7 36 keyboard7 45 Limits7 37 linspace7 17 loglog7 27 loops7 42 M book7 47 M les debugging7 35 function7 32 script7 32 Matrices7 39 matrix transpose7 41 multiple plots7 20 nargin7 44 nargout7 44 num2str7 47 output7 45 parsing7 44 partial derivatives7 35 pause7 45 plot7 15 plots multiple7 19 pretty7 11 products7 38 quad7 36 quadl7 36 real7 8 saVing plots as eps7 22 semilogyo7 24 simple7 10 SIMULINK7 47 sin7 4 solve7 11 str2dlouble7 47 str2num7 47 strcat7 47 strcmp7 46 structure7 13 subs7 14 sums7 38 switch7 44 symbolic7 4 differentiation7 35 Integration7 36 sums7 38 symbolic objects7 7 symsum7 38 Taylor series7 38 unreal7 8 varargin7 45 vectorize7 37 vectorize7 29 while7 42 Workspace save as7 5 Zoom7 39

### BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.

### You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

## Why people love StudySoup

#### "I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

#### "When you're taking detailed notes and trying to help everyone else out in the class, it really helps you learn and understand the material...plus I made $280 on my first study guide!"

#### "I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

#### "It's a great way for students to improve their educational experience and it seemed like a product that everybody wants, so all the people participating are winning."

### Refund Policy

#### STUDYSOUP CANCELLATION POLICY

All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email support@studysoup.com

#### STUDYSOUP REFUND POLICY

StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here: support@studysoup.com

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to support@studysoup.com

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.