Introduction to Physics Computing MT 2013 Lecture 2 J Tseng Outline Control flow Some additional practical hints Further Matlab features MT 2013 (II) Introduction to Physics Computing (Tseng)
Download ReportTranscript Introduction to Physics Computing MT 2013 Lecture 2 J Tseng Outline Control flow Some additional practical hints Further Matlab features MT 2013 (II) Introduction to Physics Computing (Tseng)
Introduction to Physics Computing
MT 2013 Lecture 2 J Tseng
Outline
Control flow
Some additional practical hints
Further Matlab features
MT 2013 (II) Introduction to Physics Computing (Tseng) 2
Reminder: what is a program?
Basic idea: it’s a sequence of instructions
function shampoo() for j = 1:2 end lather(); rinse(); end function shampoo() lather(); rinse(); lather(); rinse(); end MT 2013 (II) Introduction to Physics Computing (Tseng) 3
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 MT 2013 (II) Introduction to Physics Computing (Tseng) 4
Conditional
BEGIN Do something Do more?
YES NO Do more something END END “intelligence” (or at least decision-making) is coded with conditionals (branches) In Matlab, basic conditional is if…elseif…else…end function shampoo lather(); rinse(); if isRequired() end lather(); rinse(); end MT 2013 (II) Introduction to Physics Computing (Tseng) 5
Loops
BEGIN
Computers excel at repeating the same operation over and over (100 million million times)
Do something
Matlab loops mimic human structures
Conditional: terminate on condition Indexed: run sequence over the elements of a set (vector) Do more?
NO END YES while expr
statement1
;
statement2
;
end
for index = vector
statement1
;
statement2
;
end
Loops over columns (if a matrix) MT 2013 (II) Introduction to Physics Computing (Tseng) 6
Loops (2)
Basic idea: it’s a sequence of instructions
function shampoo() end for j = 1:2 function shampoo lather(); rinse(); if isRequired() end end lather(); lather(); rinse(); end rinse(); lather(); rinse(); MT 2013 (II) Introduction to Physics Computing (Tseng) 7
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 Keep functions small less to debug at any time debug once MT 2013 (II) Introduction to Physics Computing (Tseng) 8
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
script.m
Function workspace function.m
>>
func()
MT 2013 (II) Introduction to Physics Computing (Tseng) 9
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 syms to define symbols This tells Matlab that x is a symbol rather than a pigeonhole/variable Then write functions in terms of symbols Use sym to 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 MT 2013 (II) Introduction to Physics Computing (Tseng) 10
Symbolic computation (2)
syms x y f(x,y) = x^3*y^4 f(3,5) ezsurf(f); MT 2013 (II) Introduction to Physics Computing (Tseng) 11
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):
Name
solve
Description
Solve algebraic equation(s) dsolve Solve ordinary differential equation(s) diff Differentiate symbolic expression int Integrate symbolic expression fourier Fourier transform collect, expand, pretty, factor, simplify Manipulate and simplify symbolic expressions Question: how does Matlab know with what variable to diff()?
MT 2013 (II) Introduction to Physics Computing (Tseng) 12
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 MT 2013 (II) Introduction to Physics Computing (Tseng) 13
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 MT 2013 (II) Introduction to Physics Computing (Tseng) 14