1 / 14

Introduction to Physics Computing

Introduction to Physics Computing. MT 2013 Lecture 2 J Tseng. Outline. Control flow Some additional practical hints Further Matlab features. Reminder: what is a program?. Basic idea: it’s a sequence of instructions. function shampoo() for j = 1:2 lather(); rinse();

brownlisa
Download Presentation

Introduction to Physics Computing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction toPhysics Computing MT 2013 Lecture 2 J Tseng

  2. Outline • Control flow • Some additional practical hints • Further Matlab features Introduction to Physics Computing (Tseng)

  3. Reminder: what is a program? • Basic idea: it’s a sequence of instructions function shampoo() for j = 1:2 lather(); rinse(); end end function shampoo() lather(); rinse(); lather(); rinse(); end Introduction to Physics Computing (Tseng)

  4. Programs in Matlab • (Stored) programs in Matlab come in two forms: • Scripts: a stored sequence of commands • Functions: like a script, but can be called with arguments • In both cases, edit text file with “.m” suffix • Easiest to use the pull-down menu under “New” • A new script gives you a blank file to edit • A new function gives you a template • Change the name to something meaningful function [ output_args ] = untitled(input_args) %UNTITLED Summary of this function goes here % Detailed explanation goes here end Introduction to Physics Computing (Tseng)

  5. Conditional • “intelligence” (or at least decision-making) is coded with conditionals (branches) • In Matlab, basic conditional is if…elseif…else…end BEGIN Do something function shampoo lather(); rinse(); if isRequired() lather(); rinse(); end end Do more? YES NO Do more something END END Introduction to Physics Computing (Tseng)

  6. Loops BEGIN • Computers excel at repeating the same operation over and over (100 million million times) • Matlab loops mimic human structures • Conditional: terminate on condition • Indexed: run sequence over the elements of a set (vector) Do something YES Do more? NO END whileexpr statement1; statement2; end forindex = vector statement1; statement2; end Loops over columns(if a matrix) Introduction to Physics Computing (Tseng)

  7. Loops (2) • Basic idea: it’s a sequence of instructions function shampoo() for j = 1:2 lather(); rinse(); end end function shampoo lather(); rinse(); if isRequired() lather(); rinse(); end end function shampoo() while isRequired() lather(); rinse(); end end function shampoo() lather(); rinse(); lather(); rinse(); end Introduction to Physics Computing (Tseng)

  8. Additional tips • Comments:% highly recommended! • Like a log book: will you understand in 6 months? • Describe at least major sections of code • Indentation: • Indent if/end and loop blocks so you know what they include(the Matlab editor has automatic tools for this as well) • “Debugging”: • Very few programs work the first time • Test for unacceptable circumstances, print or return something descriptive • Print intermediate values and compare with your expectations:display(), disp(), fprintf() • Design for debugging • Use functions instead of repeating commands  debug once • Keep functions small  less to debug at any time Introduction to Physics Computing (Tseng)

  9. Workspaces (name scope) • Scripts operate on user’s workspace • If the script creates, destroys, or changes a variable, it will be reflected in your workspace • Functions have their own (local) workspace • Functions don’t affect your workspace, they just give you their result • This also means you can write functions without worrying about the caller • When debugging, you don’t have to look as far for most problems • There are ways of defining “global” variables, but they’re not recommended • They will ruin your life • They will ruin your friends’ lives CommandWindow Workspace >> script >> func() Function workspace script.m function.m Introduction to Physics Computing (Tseng)

  10. Symbolic computation • Not in the practical course handbook • Intention here simply is to flag up a useful capability • For details, see http://www.mathworks.co.uk/help/symbolic/index.html • Use symsto define symbols • This tells Matlab that x is a symbol rather than a pigeonhole/variable • Then write functions in terms of symbols • Use symto define a symbolic constant • Matlab symbolic output takes some getting used to • You still need to learn the math! • Experience is that symbolic computation is a nice shortcut, but to get it to do what you want it to do, you have to give the computer some pretty explicit hints Introduction to Physics Computing (Tseng)

  11. Symbolic computation (2) syms x y f(x,y) = x^3*y^4 f(3,5) ezsurf(f); Introduction to Physics Computing (Tseng)

  12. Symbolic computation (3) • You can perform arithmetic on expressions as with numbers • Matlab is fiddly about arguments and whether they match • Some interesting functions (look up online or in Matlab help): • Question: how does Matlab know with what variable to diff()? Introduction to Physics Computing (Tseng)

  13. Numerical methods • A number of algorithms underlie Matlab functionality • Some of these you will see in your practical exercises • Other resources on numerical methods: • R.L. Burden, J.D. Faires, Numerical Methods, 3rd ed., Boston: Prindle, Weber & Schmidt, 1985. • More mathematical: S.D. Conte, Carl de Boor, Elementary Numerical Analysis: An Algorithmic Approach, New York: McGraw-Hill, 1980. • Koonin and Meredith, Computational Physics • WH Press, SA Teukolsky, WT Vetterling, BP Flannery, Numerical Recipes, http://www.nr.com • Interactive Educational Modules in Scientific Computing, http://www.cse.uiuc.edu/iem/ • Kalos and Whitlock, Monte Carlo Methods, vol. 1. • Luc Devroye, Non-Uniform Random Variate Generation, http://www.nr.com/devroye Introduction to Physics Computing (Tseng)

  14. Conclusion • You should have started on CO01, or are just about to • In either case, downloading Matlab is a good start • You have (at least) heard of some of the basic practical issues of computer programming • Best way to learn remains doing it yourself • These lectures have generally avoided getting too philosophical – it is better to run into the problems first so you know what problems are being solved • Computer science, however, can be seen as part philosophy, mathematics, engineering, and art • Next week: Computing from a physicist’s perspective Introduction to Physics Computing (Tseng)

More Related