Example: stock market

An Introduction to Matlab David F. Gri ths

An Introduction to Matlab Version David F. Griffiths formerly of Department of Mathematics The University of Dundee Dundee DD1 4HN. Scotland, UK. With additional material by Ulf Carlsson Department of Vehicle Engineering KTH, Stockholm, Sweden Thanks to Dr Anil Bharath, Imperial College, Dr Chris Gordon, University of Christchurch, Prof. Dr. Markus Kottmann, HSR Hochschule fu r Technik, Switzerland for their contributions to this revised version. Copyright c 1996 by David F. Griffiths. Amended October, 1997, August 2001, September 2005, October 2012, March 2015. This Introduction may be distributed provided that it is not be altered in any way and that its source is properly and completely specified. Contents 15 Two Dimensional Arrays 17.

1 MATLAB Matlab is an interactive system for doing numerical computations. A numerical analyst called Cleve Moler wrote the rst version of Matlab in the

Tags:

  Introduction, Matlab, Introduction to matlab

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of An Introduction to Matlab David F. Gri ths

1 An Introduction to Matlab Version David F. Griffiths formerly of Department of Mathematics The University of Dundee Dundee DD1 4HN. Scotland, UK. With additional material by Ulf Carlsson Department of Vehicle Engineering KTH, Stockholm, Sweden Thanks to Dr Anil Bharath, Imperial College, Dr Chris Gordon, University of Christchurch, Prof. Dr. Markus Kottmann, HSR Hochschule fu r Technik, Switzerland for their contributions to this revised version. Copyright c 1996 by David F. Griffiths. Amended October, 1997, August 2001, September 2005, October 2012, March 2015. This Introduction may be distributed provided that it is not be altered in any way and that its source is properly and completely specified. Contents 15 Two Dimensional Arrays 17.

2 Size of a matrix .. 18. 1 Matlab 2 Transpose of a matrix .. 18. Special Matrices .. 18. 2 Starting Up 2 The Identity Matrix .. 19. Diagonal Matrices .. 19. 3 Matlab as a Calculator 3. Building Matrices .. 20. 4 Numbers & Formats 3 Tabulating Functions .. 20. Extracting Parts of Matrices .. 20. 5 Variables 3 Elementwise Products (.*) .. 21. Variable Names .. 3 vector products .. 21. Matrix Products .. 22. 6 Suppressing output 4 Matrices .. 23. 7 Built In Functions 4 16 Systems of Linear Equations 23. Trigonometric Functions .. 4 Overdetermined systems .. 24. Other Elementary Functions .. 4. 17 Characters, Strings and Text 26. 8 Vectors 5. The Colon Notation .. 5 18 Loops 27. Extracting Parts of Vectors .. 6. Column Vectors.

3 6 19 Timing 28. Transposing .. 6. 20 Logicals 28. 9 Keeping a record 7 While Loops .. 29.. 30. 10 Script Files 7. 21 Further Built in Functions 31. 11 Keyboard Accelerators 8 Rounding Numbers .. 31. The sum Function .. 31. 12 Arithmetic with Vectors 8 max & min .. 32. Inner Product (*) .. 8 Random Numbers .. 32. Elementwise Product (.*) .. 9 find for vectors .. 32. Elementwise Division (./) .. 10 find for matrices .. 32. Elementwise Powers (.^) .. 11. 22 Function m files 33. 13 Plotting Functions 12. Plotting Titles & Labels .. 12 23 Plotting Surfaces 37. Grids .. 12. Line Styles & Colours .. 12 24 Reading/Writing Data Files 38. Multi plots .. 13 Formatted Files .. 39. Hold .. 13 Unformatted Files .. 39. Hard Copy .. 13. 25 Graphic User Interfaces 40.

4 Subplot .. 14. Zooming .. 14 26 Command Summary 42. Figure Properties .. 14. text on Plots .. 15. Axes .. 16. 14 Elementwise Examples 17. 1. 1 Matlab Type quit at any time to exit from Matlab . Extensive documentation is available, either via Matlab is an interactive system for doing the command line by using the 'help topic'. numerical computations. command (see below) or via the internet. We recommend starting with the command A numerical analyst called Cleve Moler wrote the first version of Matlab in the demo 1970s. It has since evolved into a success- ful commercial software package. (a link may also be provided on the top line Matlab relieves you of a lot of the mun- of the command window). This brings up a dane tasks associated with solving prob- separate window which gives access to a short lems numerically.

5 This allows you to spend video entitled Getting Started that describes more time thinking, and encourages you the purpose of the various panes in the main to experiment. Matlab window. Help is available from the command line prompt. Matlab makes use of highly respected al- Type help help for help (which gives a brief gorithms and hence you can be confident synopsis of the help system), help for a list of about your results. topics. The first few lines of this read HELP topics: - Powerful operations can be performed us- - ing just one or two commands. MatlabCode/ Matlab - (No table of contents file). You can build up your own set of func- Matlab /general - General purpose commands. Matlab /ops - Operators and special .. tions for a particular application.

6 Matlab /lang - Programming language .. Excellent graphics facilities are available, Matlab /elmat - Elementary matrices and .. and the pictures can be inserted into LATEX Matlab /randfun - Random matrices and .. and Word documents. Matlab /elfun - Elementary math functions. Matlab /specfun - Specialized math functions. These notes provide only a brief glimpse of the (truncated lines are shown with .. ). Then to power and flexibility of the Matlab system. For obtain help on Elementary math functions , a more comprehensive view we recommend the for instance, type book >> help elfun Matlab Guide 2nd ed. Higham & Higham Clicking on a key word, for example sin will SIAM Philadelphia, 2005, ISBN: provide further information together with a link 0-89871-578-4.

7 To doc sin which provides the most extensive documentation on a keyword along with exam- ples of its use. 2 Starting Up Another useful facility is to use the Matlab can be used in a number of different lookfor keyword ways or modes; as an advanced calculator in the calculator mode, in a high level programming command, which searches the help files for the language mode and as a subroutine called from keyword. See Exercise (page 22) for an a C-program. More information on the first two example of its use. of these modes is provided by these notes. When used in calculator mode all Matlab com- mands are entered to the command line from the keyboard at the command line prompt . indicated with '>>'. 2. 3 Matlab as a Calculator Command Example of Output >>format short (4 decimal places).

8 The basic arithmetic operators are + - * / ^ >>format short e +01. and these are used in conjunction with brackets: >>format long e +01. ( ). The symbol ^ is used to get exponents >>format short (4 decimal places). (powers): 2^4=16. >>format bank (2 decimal places). You should type in the commands shown at the prompt: >>. The command >> 2 + 3/4*5. ans = format compact is also useful in that it suppresses blank lines in >>. the output thus allowing more information to Is this calculation 2 + 3/(4*5) or 2 + (3/4)*5? be displayed. Matlab works according to the priorities: 1. quantities in brackets, 5 Variables 2. powers 2 + 3^2 2 + 9 = 11, >> 3-2^4. ans =. 3. * /, working left to right (3*4/5=12/5), -13. 4. + -, working left to right (3+4-5=7-5), >> ans*5.

9 Ans =. Thus, the earlier calculation was for 2 + (3/4)*5 -65. by priority 3. The result of the first calculation is labelled ans by Matlab and is used in the second cal- 4 Numbers & Formats culation, where its value is changed. We can use our own names to store numbers: Matlab recognizes several different kinds of num- bers >> x = 3-2^4. x =. Type Examples -13. Integer 1362, 217897 >> y = x*5. Real , y =. Complex (i = 1) -65. Inf Infinity (result of dividing by 0). NaN Not a Number, 0/0 so that x has the value 13 and y = 65. These can be used in subsequent calculations. These are examples of assignment statements: val- The e notation is used for very large or very ues are assigned to variables. Each variable small numbers: 3 must be assigned a value before it may be used +03 = 10 = 1 on the right of an assignment statement.

10 = 10 = All computations in Matlab are done in dou- ble precision, which means about 15 significant Variable Names figures. How Matlab prints numbers is con- Legal names consist of any combination of let- trolled by the format command. Type help ters and digits, starting with a letter. These format for full list. are allowable: Should you wish to switch back to the default format then format will suffice. NetCost, Left2 Pay, x3, X3, z25c5. 3. These are not allowable: sin, cos, tan and their arguments should be in radians. Net-Cost, 2pay, %x, @sign to work out the coordinates of a point on a circle of radius 5 centred at the origin and Use names that reflect the values they repre- having an elevation 30o = /6 radians: sent. Special names: you should avoid using >> x = 5*cos(pi/6), y = 5*sin(pi/6).


Related search queries