Transcript Slide 1
Introduction to Matlab Physics II Week 5 C. Reed *Some material shamefully stolen from web resources Note The project portion of this practicum had been moved to our next meeting (following spring break). Today we will address some of the common problems I’ve seen in the homeworks and discuss some good programming techniques. There are a plethora of reasons why code that just works is not good enough: • Fixing bugs, adding features, and working with the code in all other aspects get a lot easier when the code isn’t messy. • Imagine someone else looking at your code, and trying to figure out what it does. • Experience tells that there is virtually no computational task that you come across only once in your programming life. •Imagine yourself looking at your own code, a week, a month, or a year from now: Would you still be able to understand why the code works as it does? Variable Naming Conventions • Variables should be short yet meaningful • camelCase – writeDescriptionsWithNoSpaces – firstLetterLowerCaseForVariablesAndFunctions • some_programmers_use_underscores – depends on both programmer and language • Use all caps and underscores with constants – HOURS_IN_DAY = 24; >>edit importdata Quiz • What’s the following code doing? function v = func1(a,b) v=1 if a > 24 || a < 0 v=0 end if b > 60 || b < 0 v=0 end Poorly chosen names make code difficult to understand, maintain, debug, etc Quiz: take 2 What’s the following code doing? function isValid = validTime(hour, min) isValid = 1 if hour > 24 || hour < 0 isValid = 0 end if min > 60 || min < 0 isValid = 0 end When to Comment—General Rules • Commenting is very important (uncommented code is everywhere) • Function/block comments explain what • Use inline comments to explain why polyfit(data,7) % degree 7 found to be optimal see http://www…. Code Indenting Code Repetition is Bad mmmkay? • Are you copying and pasting? – probably a better solution • [helper] functions • loops • google the task at hand (google: area of a triable matlab) Helper Functions Q: What would be a good helper function for triType? Creating Beautiful Matlab Output • Using cells can help create a beautiful matlab output – Can also save time when testing code • Best explanation is to use cells – Dowload example code from class website under week 4 section http://highenergy.physics.uiowa.edu/creed/HW/makeLCPlot.m Tips: Using the Profiler to find Bottlenecks • Desktop->Profiler – click start profiler – rerun lightcurve function – view profiler output Tips--Preallocation • Notice the use of the zeros function? – try without using zeros function – What’s happening? Task • Compare times for copying an array with preallocation vs without preallocation – make an m-file (script) and use the profiler Homework Part 1 (2 parts) • Read pages 6-23 in Guidelines for Writing Clean and Fast Code in Matlab – On class website – Write 1 paragraph describing the tip you find most useful Part 2: Small coding project (2-3 hours) – will announce tomorrow