School of Physics and Astronomy, University of Manchester PHYS20762 ComputaAonal Physics Hywel Owen Part 1: MATLAB and Data Analysis Lecture 1: IntroducAon to ComputaAonal Physics What is this course? • This is a course on ComputaAonal Physics: – The use of algorithms and numerical methods to perform data analysis and simulaAon in support of experimental and theoreAcal physics • There are 3 pillars of physics: – Theory – Experiment – SimulaAon PHYS20762 Lecture 1 Course Informa3on • The real course website: – hPp://theory.physics.manchester.ac.uk/~hywel/teaching/phys20762/ • But material is also posted on Blackboard – Blackboard is linked to the website above PHYS20762 Lecture 1 Should you be here? • If you are a TheoreAcal Physics student: – Go away. You should be on PHYS20872 Theory CompuAng Project. – Were you at the orientaAon session yesterday? No? Then talk to Niels Walet. • If you not a TheoreAcal Physics student: – CongratulaAons! – You should be here. • Can you do both PHYS20762 and PHYS20872? – No. • Can you do both AddiAonal Lab and PHYS20762? – No. • What is the difference between PHYS20762 and PHYS20872? – PHYS20762 is for Normal students – PHYS20872 is for Theory students PHYS20762 Lecture 1 Why should you do this course? • Technical compuAng underlies prePy much all of the jobs you are likely to do a]er graduaAng • You are about 90% likely to be doing scienAfic programming a]er you leave Manchester Graduate Des3na3ons ScienAc/CompuAng Finance/Management Postgrad/Research Other PHYS20762 Lecture 1 A couple of examples • My best friend ScoP • Graduate physics (Edinburgh) • PhD in crystallography, including wriAng of experimental control and data analysis • Then programming GUIs in C++ for mobile phone manufacturer • Now wriAng trading so]ware in C++ for CiAbank in London • ££££ • Me • Graduate physics (Manchester!) • PhD in liquid crystals, wriAng data acquisiAon and data analysis • WriAng control so]ware for diagnosAc staAon on parAcle accelerators (e.g. SRS); VB, LabVIEW, etc. • WriAng so]ware to simulate behaviour of new parAcle accelerators (e.g. DIAMOND) www.diamond.ac.uk • Carrying this on at Manchester PHYS20762 Lecture 1 What languages/plaAorms are actually used in the ‘real world’? Commercial • Compiled – C/C++ (esp. Finance) • Vis-‐C, C#, Obj-‐C, Java • Interpreted – Python (e.g. Google) – (Ruby, Javascript) • Environments – LabVIEW (esp. for DAQ) – Excel Research/Scien3fic • Compiled – C/C++ – Fortran • F90, F95 etc. • Interpreted – Python • Environments – MATLAB – MathemaAca PHYS20762 Lecture 1 Other languages (Pascal, Visual Basic, Perl, Tcl) Other packages (Maple, Mathcad, IDL, Origin) Structure of the Course • Part 1: MATLAB and Data Analysis – Lecture 1: The MATLAB environment – Lecture 2: Working with data in MATLAB – Lecture 3: Data analysis and fiong in MATLAB – Lecture 4: File input and output in MATLAB • Part 2: Numerical Methods – Lecture 5: IntroducAon to numerical methods – Lecture 6: Numerical soluAon of equaAons – Lecture 7: Numerical methods in MATLAB – Lecture 8: User-‐defined funcAons in MATLAB • Part 3: Monte-‐Carlo Techniques – Lecture 9: Random Numbers and Monte Carlo IntegraAon – Lecture 10: Monte Carlo ParAcle Transport • MATLAB PracAce (Week 1) • Projects: – Project 1 (Weeks 2-‐4): Analysis of experimental spectrum data – Project 2 (Weeks 5-‐8): Numerical integraAon of the damped oscillator equaAon – Project 3 (Weeks 9-‐12): PenetraAon of neutron through shielding • 12 Half days in the laboratory 3.58 (½ on Tue, ½ on Fri) PHYS20762 Lecture 1 Course Aims and Learning Outcomes • To familiarise the students with pracAcal techniques for using computers in physics. • On compleAon successful students will be able to: 1. write MATLAB programs involving computaAons and graphics; 2. use MATLAB to analyse and fit experimental data; 3. use numerical methods (Euler, Verlet) to find soluAons of an ordinary differenAal equaAon and thereby to analyse the behaviour of a physical system (e.g. damped oscillator); 4. understand the principles of the Monte Carlo method and its applicaAon to parAcle transport 5. use the Monte Carlo method to simulate parAcle transport through a given physical system (e.g. neutrons through shielding) PHYS20762 Lecture 1 Assessments • You will write 3 project reports • Assessment is based on the content of these reports • Read each project brief (on the module website) and follow it! • All the project briefs and assessment forms (with criteria) are already on the website. • Each project has a bonus secAon – you are encouraged to try them, they’re not that hard. (hint: don’t think of them as bonuses…) • Extra marks may be awarded for invenAveness – we want you to think! • Projects are to be submiPed in paper form: – Sorry, what? This is a compuAng course, and you want a report, in paper format??? – We tried electronic, and it’s too much hassle both for you and for us – We have to print them out to mark them up for your feedback anyway PHYS20762 Lecture 1 Some Recommended Texts • Chapman, S.J. MATLAB Programming for Engineers (Brooks and Cole 2000) • Higham, D.J. & Higham, N.J. MATLAB Guide (Philadelphia: Society for Industrial and Applied MathemaAcs) 2nd ediAon (2005) • Garcia, A.L. Numerical Methods for Physics (PrenAce Hall 1994) • Kerningham, B.W. & Ritchie, D.M. The C programming Language (PrenAce Hall) 2nd ediAon (1988) • Barlow, R.J. & BarneP, A.R. Compu>ng for Scien>sts, Principles of Programming with Fortran 90 and C++ (John Wiley and Sons Ltd , 1998) • There are not a lot of good texts for what we’re doing. PHYS20762 Lecture 1 Two Basic Messages About This Course • Not everything you need to know for the projects is in the lecture notes – APend the lectures, ask quesAons! – You will learn a lot by doing the projects: get feedback in the lab sessions • Not everything in the lectures is about the projects – We’re trying to: – a) Make the rest of your degree easier – b) Expose you to ideas that you can learn more about by yourselves later – c) Generally educate you in this topic PHYS20762 Lecture 1 Rubin Landau’s Rules of Educa3on 1. Most of educaAon is learning what the words mean; the concepts are usually quite simple once you understand what you are being told. 2. Confusion is the first step to understanding. 3. TraumaAc experiences tend to be the most educaAonal ones. Why MATLAB? • MATLAB is a terrible programming language • You could do this course in, for example, C++ • BUT…. – This is not a course in programming (you did that last semester) – This is a course on algorithms, i.e. implemenAng physics using compuAng – The language is secondary – Speed does not maPer for our course (hint: it does a bit) PHYS20762 Lecture 1 PHYS20762 Lecture 1 Interlude… Interlude: How do programmers spend their 3me? • Q: How much of a typical programmer’s day is spent actually programming? PHYS20762 Lecture 1 Interlude: How do programmers spend their 3me? • Q: How much of a typical programmer’s day is spent actually programming? PHYS20762 Lecture 1 Programming 25% Debugging 25% Documenta3on 10% Design 20% Mee3ngs 20% PHYS20762 Lecture 1 Onward… C++ is great, but… • There is an overhead of ‘structure’ needed in your code • There are no ‘scienAfic/math’ libraries built in to the language – Plenty of add-‐ons • It’s fairly plaxorm-‐dependent – You can’t send the source code to your friend easily • It’s fast, but you o]en don’t need that parAcularly PHYS20762 Lecture 1 MATLAB • MATLAB is the most widely-‐used interpreted scienAfic compuAng package – i.e. you will probably need to know it later – It’s also VERY handy to know how to use it for your degree • A]er this course I don’t want to hear people moaning about Easyplot! • MATLAB’s advantages – Interpreted – no messing around between wriAng, compiling and running – Lots of built-‐in libraries for scienAsts – Plaxorm independent. ‘Write once, run anywhere’ (nearly) • MATLAB’s disadvantages – Interpreted, so somewhat slower than compiled languages for big tasks – Like many library-‐heavy commercial packages, it’s expensive – But you won’t noAce either of these! PHYS20762 Lecture 1 Reminders: Workspace/Command Window PHYS20762 Lecture 1 Reminders: Edit Window PHYS20762 Lecture 1 Reminders: Variables in MATLAB • (Reminder: A variable is a unit of data with a name, which is available to the program) • Simple variables – comment = ‘This is a string’; % Can put comments after the definition! – a = 1; % Integers! – b = 2.883; % Reals! – c = 1.2*a + b; % Formulae! – x = 1; y = 2; % Multiple definitions per line! – v = [1 5 2 6]; % vector(row vector)–square brackets!! – m = [1 5;3 8]; % 2x2 matrix! – n = [0 1+7]; % Expression in a definition! – p = [y(2) 7 y(3)]; % Array indexing! PHYS20762 Lecture 1 Reminders: Defining and indexing arrays x = 1:2:10 % First:increment:last! ! x = 1 3 5 7 9! ! g = 1:4; % Row vector! h = g’; % Transpose makes column vector! i = [1;3;4;5]; % Or define column vector explicitly! ! ! This is all quite different from other languages! ! ! PHYS20762 Lecture 1 Reminders: Indexing x = [1.1 -2.2 3.3 -4.4 5.5];! ! x(3) is 3.3! x(1:2) is [1.1 -2.2]! x(1:2:5) is [1.1 3.3 5.5]! ! ! m = [1 2 3;-2 -3 -4;3 4 5];! m(6) is 4 – weird but true…! m(2,3) is -4! m(3,:) is [3 4 5]! m(:,2) is [2;-3;4]! m(1:2,3:4) is [2 3;-3 -4] PHYS20762 Lecture 1 1 2 3! -2 -3 -4! 3 4 5! 1 2 3! -2 -3 -4! 3 4 5! Reminders: Func3ons There are lots of funcAons and constants available to you: Math: + -‐ * / ^ () Comparison: == < > ~= (these return 1/0 for ‘true’/’false’) Logical: & | (also return 1/0 for ‘true’/’false’) Numerical: Inf j NaN pi FuncAons: abs exp log log2 log10 mod real imag round sign Trig: sin cos tan asin acos atan (etc.) Specialised: besselj factorial legendre (etc.) Use the Quick Reference Booklets BIG TIP: Someone will probably have wanted to do the same job as you. That funcAon already exists, you just have to FIND IT. PHYS20762 Lecture 1 Reminders: Let’s plot some data x = 0:pi/20:2*pi; % Make some x values! y = sin(x); % Make some y values as a fn. of x! ! plot(x,y); PHYS20762 Lecture 1 Hint: also try linspace Reminders: How about another one? x = 0:pi/20:2*pi; % Make some x values! z = (x-2).^2+2; % Make some z! % (x-2)^2+2 doesn’t work!! plot(x,z); % Over-writes first figure PHYS20762 Lecture 1 Matrix and Array Mul3ply/Power – ‘a Gotcha’ This is something *very* particular to MATLAB: • * is used for matrix multiplication • .* is used for element-by-element (array) multiplication • ^ is used for matrix powers • .^ is used for element-by-element (array) powers PHYS20762 Lecture 1 >> a = [1 2;3 4]! a =! 1 2! 3 4 >> a^2! ans =! 7 10! 15 22! >> a.^2! ans =! 1 4! 9 16! a^2 is equivalent to a*a (matrix multiplication) …and for mul3plica3on >> a = [1 2;3 4]! ! a =! ! 1 2! 3 4! ! >> b=[1 1;1 1]! ! b =! ! 1 1! 1 1! PHYS20762 Lecture 1 >> a*b! ! ans =! ! 3 3! 7 7! ! >> a.*b! ! ans =! ! 1 2! 3 4! Some other plot types • (Use the Quick Reference Booklets) PHYS20762 Lecture 1 polar(x,y) bar(x,y) loglog(x,y) semilogx(x,y) Code recommenda3ons • Microso] Excel – part of Office, okay for basic work and data collecAon • MATLAB (hPp://www.mathworks.co.uk/ ) – Students can use this anywhere you’re connected to the Uni network – Including VPN (hPp://www.itservices.manchester.ac.uk/vpn/) • MathemaAca (hPp://www.wolfram.com/) – Awesome so]ware, but less used than MATLAB – Also have a site license, but easier to use Cluster computers • Python (hPp://www.python.org/ ) – Use Python 2.7.x (not 3.x) as there’s more code made for it – Get the excellent Canopy package (any OS): hPps://www.enthought.com/products/canopy/ PHYS20762 Lecture 1 Lab Sessions • Today: MATLAB Refresher and PracAce • Lab Sessions are 10am to 1pm on Tuesdays or Fridays – (about) Half of you on Tuesdays, Half of you on Fridays. – Computer Lab 3.58 – Demonstrators are on hand only at these Ames; make use of them • At other Ames you may carry out work on any of the Schuster cluster computers • Refer to the Ametable and deadlines to work out when to hand in your project work: – hPp://theory.physics.manchester.ac.uk/~hywel/phys20762/phys20762-‐ Ametable/ PHYS20762 Lecture 1