Example: stock market

MATLAB MANUAL AND INTRODUCTORY TUTORIALS

MATLAB MANUALAND INTRODUCTORY TUTORIALSIvan Graham,with some revisions by Nick Britton,Mathematical Sciences, University of BathFebruary 9, 2005 This MANUAL provides an introduction toMATLAB with exercises which are specifically oriented to theMATLAB service provided by Bath University Computing Service (BUCS). However much of the information providedhere is applicable to anyMATLAB installation, PC, Mac or MANUAL has a dual role: it serves first as a set of directed TUTORIALS to be carried out in the laboratoryand second as a general reference MANUAL chapter of the MANUAL represents one tutorial, and includes exercises to be done during privatestudy time. For each tutorial you should read through the relevant chapter, trying out the various featuresofMATLAB which are described, and then you should do the may extend the chapter by doing your own experiments with the experimenta-tion is essential when learning introduction : What isMATLAB?

0 Introduction: What is MATLAB? MATLAB is a package that has been purpose-designed to make computations easy, fast and reliable. It is installed on machines run by Bath University Computing Services (BUCS), which can be accessed in the BUCS PC Labs such as those in 1 East 3.9, 1 West 2.25 or 3 East 3.1, as well as from any of the PCs in the ...

Tags:

  Manual, Introduction, Tutorials, Introductory, Matlab, Matlab manual and introductory tutorials

Information

Domain:

Source:

Link to this page:

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

Other abuse

Transcription of MATLAB MANUAL AND INTRODUCTORY TUTORIALS

1 MATLAB MANUALAND INTRODUCTORY TUTORIALSIvan Graham,with some revisions by Nick Britton,Mathematical Sciences, University of BathFebruary 9, 2005 This MANUAL provides an introduction toMATLAB with exercises which are specifically oriented to theMATLAB service provided by Bath University Computing Service (BUCS). However much of the information providedhere is applicable to anyMATLAB installation, PC, Mac or MANUAL has a dual role: it serves first as a set of directed TUTORIALS to be carried out in the laboratoryand second as a general reference MANUAL chapter of the MANUAL represents one tutorial, and includes exercises to be done during privatestudy time. For each tutorial you should read through the relevant chapter, trying out the various featuresofMATLAB which are described, and then you should do the may extend the chapter by doing your own experiments with the experimenta-tion is essential when learning introduction : What isMATLAB?

2 11 First Tutorial: Simple Calculations and File StartingMATLAB.. Simple arithmetic inMATLAB.. TheMATLABD evelopment Environment .. TheMATLAB demo .. Recording aMATLAB session .. Printing out fromMATLAB.. Further information onMATLAB.. Exercises for Chapter 1 ..62 Second Tutorial: Variables and Arrays .. Simple Functions .. Graphics .. Exercises for Chapter 2 .. 103 Third Tutorial: Loops and Logical Theforloop .. Thewhileloop .. Theif-elseif-elsestatement .. Exercises for Chapter 3 .. 154 Fourth Tutorial: scripts and TheMATLAB workspace .. Script files .. Function files .. The general form of a function file .. Exercises for Chapter 4 .. 195 Fifth Tutorial: A Population Dynamics Cellular Automata .. A simple example.

3 A programming exercise .. Exercises for Chapter 5 .. 226 Sixth Tutorial: Further Experiments on Population Dynamics227 The MATLAB Symbolic Math Exercises on Chapter 7 .. 24A Appendix: Solutions to selected exercises24B Appendix: Glossary of UNIX commands25C Appendix: Some usefulMATLAB commands26D Appendix: Advanced Programming Style inMATLAB270 introduction : What isMATLAB?MATLABis a package that has been purpose-designed to make computations easy, fast and reliable. It isinstalled on machines run by Bath University Computing Services (BUCS), which can be accessed in theBUCS PC Labs such as those in 1 East , 1 West or 3 East , as well as from any of the PCs in theLibrary. The machines which you will use for runningMATLABare SUN computers that run on the UNIX operating system. (If you are a PC or Mac fan, note that this is a quite different environment from whatyou are used to.)

4 However you will need only a small number of UNIX commands when you are workingwithMATLAB. There is a glossary of common UNIX commands in Appendix B.) MATLAB started life in the 1970s as a user-friendly interface to certain clever but complicated programsfor solving large systems of equations. The idea behindMATLABwas to provide a simple way of using theseprograms that hid many of the complications. The idea was appealing to scientists who needed to use highperformance software but had neither the time nor the inclination (nor in some cases the ability) to writeit from scratch. Since its introduction ,MATLABhas expanded to cover a very wide range of applications andcan now be used as a very simple and transparent programming language where each line of code looks verymuch like the mathematical statement it is designed to good for the following. Computations, including linear algebra, data analysis, signal processing, polynomials and interpolation,numerical integration (quadrature), and numerical solution of differential equations.

5 Graphics, in 2-D and 3-D, including colour, lighting, and also has collections of specialised functions, calledtoolboxes, that extend its functionality. In particular,it can do symbolic algebra, it can tell you that(x+y)^2is equal tox^2+2*x*y+y^ is important not to confuse the type of programming that we shall do in this course with fundamentalprogramming in an established high-level language like C, JAVA or FORTRAN. In this course we willtake advantage of many of the built-in features ofMATLABto do quite complicated tasks but, in contrastto programming in a conventional high-level language, we shall have relatively little control over exactlyhow the instructions which we write are carried out on the machine. As a result, MATLAB programs forcomplicated tasks may be somewhat slower to run than programs written in languages such as C. HowevertheMATLAB programs are very easy to write, a fact which we shall emphasise here.

6 We shall useMATLABasa vehicle for learning elementary programming skills and applications. These are skills which will be usefulindependently of the language you choose to work in. Some students in First Year will already know someof these skills, but we shall not assume any prior will meet a course in JAVA programming in the second semester of the First First Tutorial: Simple Calculations and File ManagementIn this tutorial, you will start to become familiar with theMATLAB development environment and some ofits facilities. You will learn how to start and quitMATLAB, how to do simple arithmetic calculations, how to assign values to variables, how to use some ofMATLAB s built-in functions, how to copy files from my filespace to yours, and what ascriptis and how to write, save and run Starting aMATLAB session (in WindowsXP). Here is one way to do it. Log on to Windows.

7 From the Desktop, clickStart, thenAmos, mary, midgefrom the pop-up In the BUCS XClient pop-up window, change host frombucstoamosormidgeand log on. In the blue UNIX window, typexterm &(where represents a space) to the$prompt for a second UNIX window, which you may need later. (See the last paragraph of section ) In either one of these UNIX windows, typematlab.(It is highly advisable to run only oneMATLAB session at any one time. The total number ofMATLAB sessionsthat may be run on campus is limited by the licence. You need only one session to do everything you needto do. However, there is no limit to how many UNIX windows you can have.) RunningMATLAB. After you have typedmatlab, aMATLAB logo will come up and then aMATLAB command window with a prompt>>. Now you are ready to useMATLAB. WhenMATLABis starting you shouldget a flash of theMATLAB graphics window on your screen.

8 If this did not happen, something is wrong withyour set-up and you will not get graphics when you want them. ASK YOUR TUTOR FOR HELP (and/orgo to the BUCS help desk in the library after your first tutorial) if this Terminating your session. To finish aMATLAB session, typeexitto the>>prompt. Then typeexitto the$prompts. Then log out from your Windows is essential that you log out completelyafter you have finished with any now on, when you are instructed to type various commands, unless you are explicitly told to typethem elsewhere, they should be typed to the prompt>>in theMATLAB command Simple arithmetic inMATLABThe basic operations are+, -, *, /, and^, which stand for add, subtract, multiply, divide and exponen-tiate, or raise to the power of . Work through the following example, which shows the results of typingsome simple arithmetic commands to theMATLAB prompt.

9 The commands typed by the user are thoseimmediately following the>>prompt. A%symbol means a comment: the rest of this line is ignored byMATLAB. When a computation produces a response, this is displayed immediately below the command thatproduced script Lines beginning % are comments>> clear % removes any old variables from the workspace% (There are none if you ve just started a session,% but this could be important later.)>> format short % outputs results in decimal form% with 5 digit accuracy>> 2+2 % trivial computation, result displayed on screenans =4>> x = 2+2 % same computation, result loaded into new% variable x and displayedx =4>> y = 2^2 + log(pi)*sin(x); % more complicated computation% illustrates built-in functions2% log and sin and built-in constant pi% The ; at the end suppresses output>> y % prints the contents of yy = >> format short e % changes format of output>> yy = +00>> format long % changes format of output again>> yy = on % This allows text to be presented page by page% on the screen.

10 Without it the text just scrolls% around very quickly and is impossible to read>> help format % prints the contents of the MANUAL % for the command format FORMAT Set output computations in MATLAB are done in double may be used to switch between different outputdisplay formats as follows:FORMAT Default. Same as SHORT Scaled fixed point format with 5 LONG Scaled fixed point format with 15 SHORT E Floating point format with 5 LONG E Floating point format with 15 SHORT G Best of fixed or floating point format with 5 LONG G Best of fixed or floating point format with 15 HEX Hexadecimal + The symbols +, - and blank are printedfor positive, negative and zero parts are BANK Fixed format for dollars and RAT Approximation by ratio of small :FORMAT COMPACT Suppress extra LOOSE Puts the extra line-feeds back in.


Related search queries