Question? Leave a message!




Introduction to Matlab & Data Analysis

Introduction to Matlab & Data Analysis 8
Introduction to Matlab Data Analysis Lecture 1: Introduction Lecture time: Sunday 11:1513:00 FGS C Eran Eden, Weizmann 2008 © 1 Team members  Lecturers:  Natalie KalevKronik kalev001umn.edu  Anat Tzimmer  Guest Lecturers:  Tutors:  Anat Tzimmer  Gil Farkash  Ayelet Sarel  Exercise checker:  Gil Farkash  Ayelet Sarel 2 Tips / formalities  Course website http://www.weizmann.ac.il/midrasha/courses/MatlabIntro  The website contains  Course material: Lectures + tutorials + other Matlab resources  HW and solutions  News  Where can I do the HW  On any pc computer at Weizmann (installation of Matlab will be discussed in the first tutorial)  In the tutorial class  Grade  HWs 60 + 40 Final Project  Course references  Matlab builtin tutorials and references 3 Tips / formalities  Signing up for one of the tutorials Feinberg B (1) Sunday 9:1511 (2) Wednesday 9:1511 There may be some changes during the semester  HW assistance at the computer room Once a week in Feinberg B With Ex. Checker 4 Course overview  Introduction to Matlab  Matlab building blocks: 1D 2D and 3D  Producing publication quality graphs arrays  Solving ODEs for a living:  Simple data analysis and graphics Math modeling of cancer treatment (Natalie)  Control and boolean logic  Loops  Functions and program design  Cells, structures and Files  Simple algorithms and complexity  Debugger  GUI toolbox 5 For whom is the course intended • For students with no or little experience of Matlab first two thirds of the course. • Please note that the workload is heavy and each assignment may take a few hours. • Submit HW with a study partner. • Some overlap or unsynchronized material may occur (lecture, tutorial, HW). 6 What is the course about (1) Programming in Matlab (2) Tackling data analysis problems with Matlab 7 What is the course about Example 1 of a data analysis problem CAGCATATTTGAAGCCGGGCCCACACACAATTGGGGAACGGATCCCCGCGGCCTCCCGGCA GACCCCGTCCGGCACGACGACGAAGAAGGGGAGGATGAAGTCGAATTTGAAGCGGATGAAG GATGAGGAGAGTGACGAAGAAGAGGACGAAGACGACGAGGTCCTTGACGAGGAAGTGAACT ATTGAATTTGAAGCTTATTCCATCTCAGATAATGATTATGACGGAATTAAGAAATTACTAG CAGCAGCTTTTCCTAAAGGCTCCTGTGAACACTGCAGAACTAACAGATCTCTTAATTCATA CAGAACCATATTGGAAGTGTGAATTTGAAGCTTAAGCAAACAAATGTTTCAGAAGACAGCG ATGATGATGATGCAGATGAAGATGAAATTTTTGGTTTCATAAGCCTTTTAAATTTAACTGA AAGAAAGGTACCCAGTGTGCTGAACAAATTAAAGAGTTGGTATTTGAAGCGGGTGAGAAGA ACTGTAAAGAATTTGAAGCGGCAGCTGGACAAGCTTTTAAATGACACCACCAAGCCTGTGG GCTTTCTCCTAAGTGAAAGATTCATTAATGTCCCTCCTCAGATTGCTCTGCCCATGCACCA GCAGCTTCAGAAAGAATTTGAAGCAATTTGAAGCCTAGTATTTGAAGCTTCTACCTTCTGA GACCCCGTCCGGCACGACGACGAAGAAGGGGAGGATGAAGTCGAGGATGAAGACGAAGATC GATGAGGAGAGTGACGAAGAAGAGGATTTGAAGCACGAAGACGACGAGGTCCTTGACGAGG AAGTGAATATTGAATTTGAAGCTTATTCCATCTCAGATAATGATTATGACGGAATTAAGAA ATTACTGCAGCAATTTGAAGCAAAGGCTCCTGTGAACACTGCAGATTTGAAGCAACTAACA ATTCAACAGAACCATATTGGAAGTGTGATTAAGCAAACAAATGTTTCAGAAGACAGCGATG ATGATGATGCATTTGAAGCAGATGAAGATGAAATTTTTGGTTTCATAAGCCTTTTAAATTT CTAATAAGCCATGTGGGAAGTGCTCTTTCTACCTTATTTGAAGCACACCATTTGTGGAAGA ATTACTGCAGCAATTTGAAGCAAAGGCTCCTGTGAACACTGCAGATTTGAAGCAACTAACA 8 What is the course about Example 1 of a data analysis problem Identifying repeating motifs CAGCATATTTGAAGCCGGGCCCACACACAATTGGGGAACGGATCCCCGCGGCCTCCCGGCA GACCCCGTCCGGCACGACGACGAAGAAGGGGAGGATGAAGTCGAATTTGAAGCGGATGAAG GATGAGGAGAGTGACGAAGAAGAGGACGAAGACGACGAGGTCCTTGACGAGGAAGTGAACT ATTGAATTTGAAGCTTATTCCATCTCAGATAATGATTATGACGGAATTAAGAAATTACTAG CAGCAGCTTTTCCTAAAGGCTCCTGTGAACACTGCAGAACTAACAGATCTCTTAATTCATA CAGAACCATATTGGAAGTGTGAATTTGAAGCTTAAGCAAACAAATGTTTCAGAAGACAGCG ATGATGATGATGCAGATGAAGATGAAATTTTTGGTTTCATAAGCCTTTTAAATTTAACTGA AAGAAAGGTACCCAGTGTGCTGAACAAATTAAAGAGTTGGTATTTGAAGCGGGTGAGAAGA ACTGTAAAGAATTTGAAGCGGCAGCTGGACAAGCTTTTAAATGACACCACCAAGCCTGTGG GCTTTCTCCTAAGTGAAAGATTCATTAATGTCCCTCCTCAGATTGCTCTGCCCATGCACCA GCAGCTTCAGAAAGAATTTGAAGCAATTTGAAGCCTAGTATTTGAAGCTTCTACCTTCTGA GACCCCGTCCGGCACGACGACGAAGAAGGGGAGGATGAAGTCGAGGATGAAGACGAAGATC GATGAGGAGAGTGACGAAGAAGAGGATTTGAAGCACGAAGACGACGAGGTCCTTGACGAGG AAGTGAATATTGAATTTGAAGCTTATTCCATCTCAGATAATGATTATGACGGAATTAAGAA ATTACTGCAGCAATTTGAAGCAAAGGCTCCTGTGAACACTGCAGATTTGAAGCAACTAACA ATTCAACAGAACCATATTGGAAGTGTGATTAAGCAAACAAATGTTTCAGAAGACAGCGATG ATGATGATGCATTTGAAGCAGATGAAGATGAAATTTTTGGTTTCATAAGCCTTTTAAATTT CTAATAAGCCATGTGGGAAGTGCTCTTTCTACCTTATTTGAAGCACACCATTTGTGGAAGA ATTACTGCAGCAATTTGAAGCAAAGGCTCCTGTGAACACTGCAGATTTGAAGCAACTAACA 9 What is the course about Example 2 of a data analysis problem 10 21 10 21 73 21 18 21 10 4 8 21 3 21 10 45 8 21 2 21 Image processing 10 What is the course about Example 3 of data analysis problems Signal processing 11 What is the course about (1) Programming in Matlab (2) Tackling data analysis problems with Matlab (3) Learn how to learn Matlab by yourself 12 Why Matlab  Easy to learn  Easy to debug  Great tool for scientific work  Exploring your data  Visualizing your data  Many useful “apps” 13 Matlab’s main disadvantage…  It’s slower than other programming languages.  (unless you use the compiler)… 14 Background computers Output Processing unit Input 15 Background hardware CPU Memory 16 Background hardware A central processing unit (CPU), is the hardware within a CPU computer that carries out the instructions of a computer program by performing the basic arithmetical, logical, and input/output operations of the system. (Wikipedia). In computing, memory refers to the physical devices used Memory to store programs (sequences of instructions) or data (e.g. program state information) on a temporary or permanent basis for use in a computer or other digital electronic device. (Wikipedia). Not to be confused with the data storage such as SSD and hard disk. 17 Background software High level languages C, C++, C, Java, Pascal, Perl, Lisp, Matlab Examples: Low level language Example: Assembly Machine language Example: 0111010101111101… Another important player: The operating system 18 The Matlab environment First we need to Open Matlab 19 The Matlab environment Opening/saving a file Changing current directory Prompt / Command line The command window Files and Directories inside the current directory workspace 20 Matlab can be used as a calculator 21 Our first command Writing a command in the command line 22 Our first script (Mfile) (2) Saving the script (1) Writing the script Comments start with a (3) Defining (4) Running the script script name 23 Making errors… This command does NOT exist in Matlab Pressing here will bring you to the line in the script where the error occurred 24 Another script… Making sophisticated graphics and animation in Matlab is easy. We will learn how to do this in two lectures Peaks Z = peaks; surf(Z); axis tight set(gca,'nextplot','replacechildren'); 5 Record the movie for j = 1:20 surf(sin(2pij/20)Z,Z) 0 F(j) = getframe; end 5 Play the movie twenty times movie(F,20) 2 3 2 0 1 0 1 2 2 3 y x 25 Help  help  doc  Example: doc disp  Google 26 Matlab apps 27 Introduction to Matlab Data Analysis Topic 2: The Matlab Building Blocks Variables, Arrays and Matrices Eran Eden, Weizmann 2008 © 28 identifiers  Identifiers are all the words that build up the program  An identifier is a sequence of letters, digits and underscores “”  Maximal length of identifiers is 63 characters  Can’t start with a digit  Can’t be a reserved word Examples of illegal Examples of Legal identifiers: identifiers:  007bond  time  time  dayoftheweek  babaluba  bond007  if  findWord  while 29 An overview of the main players in a program Identifiers Reserved Library Constants Variables User defined words functions functions 30 Reserved words (keywords)  Words that are part of the Matlab language  There are 17 reserved words:  for  if  function  elseif  otherwise  continue  try  global  break  while  end  case  return  else  switch  persistent  catch  Do NOT try to redefine their meaning  Don NOT try to redefine their library function names either 31 Constants  The value of a constant is fixed and does not change throughout the program Chars Numbers ‘c’ 100 0.3 Strings ‘I like to eat sushi’ Arrays ‘1 + 2’ 1 2 3 4 5 Matrices 5 3 4 2 32 Variables  Why do we need variables Computer memory salary 9000 constant newsalary 27000  Example: salary = 9000; newsalary = salary 3; If we update salary, variable disp(newsalary); newsalary will NOT be updated 27000 automatically Library functions 33 Variables The Matlab Console  Another example: pricebamba = 3 pricebamba = 3 What h appens if you omit the ‘;’ 34 Variables The Matlab Console  Another example: pricebamba = 3 pricebamba = 3 nbamba = 2; What h appens when we add the ‘;’ 35 Variables The Matlab Console  Another example: pricebamba = 3 pricebamba = 3 pricebisly = nbamba = 2; 5 pricebisly = 5 totalprice = nbisly = 3; 21 nbamba = 5 How can 36 totalprice = we fix it 21 Redefine totalprice totalprice = pricebamba nbamba + pricebisly nbisly nbamba = 5 totalprice Variables  Tip 1: Give your variables meaningful names. a = 9000 b = 100 are a bad choice for naming variables that store your working hours and salary A more meaningful choice of names would be salary = 9000; hours = 5; 37 Variables  Tip 2: Don’t make variable names too long salaryIgotformyworkatthegasolinestation = 9000; salaryIgotformyworkinthebakery = salaryIgotformyworkatthegasolinestation 3; disp(salaryIgotformyworkinthebakery); Very bad choice of variable name  When should I use capital letters  Tip 3: Whatever you do be consistent. 38 Variables Types  Each variable has a type  Why do we need variable types  Different types of variable store different types of data a = 10 Returns the type a = of a variable 10 class(a) The default variable type ans = double in Matlab is double 39 Variables Types  Double Doubleprecision floatingpoint format is a computer number format that occupies 8 bytes (64 bits) in computer memory and represents a wide dynamic range of values by using floating point. (Wikipedia).  Allows representation of very large numbers (size of a galaxy) to very small numbers (subatomic particles). 40 Variables Types  Each variable has a type  Why do we need variable types  Different types of variable store different types of data b = 10.56 d = true a = 10 c = 'Bush' b = d = a = c = 10.5600 1 10 Bush class(b) class(d) class(a) class(c) ans = ans = ans = ans = double logical double char 41 Variables Types  Different variable types require different memory allocations a = 10.4 double requires 8 bytes a = 10.4 1 2 3 8 1 0 0 0 1 1 0 0 1 0 1 1 1 0 0 0 0 0 0 0 1 0 0 0 … 1 0 0 0 1 0 0 0 b = 'B' char requires 2 bytes b = B Memory allocation and release is done 1 2 automatically in Matlab 1 0 0 0 1 1 0 0 1 0 1 1 1 0 0 0  How many bytes are required to store this variable: c = 'Bush' 42 Computer precision limitations  How much is: 0.42 + 0.08 0.5 ans = 0  How much is: 0.42 0.5 + 0.08 ans = 1.3878e017 43 Special variables 4 5 ans =  ans 20 ans + 1 ans = 21 44 Special variables 2 inf ans = Inf  ans 1 / 0  pi Warning: Divide by zero.  inf ans = Inf 45 Special variables 0 / 0 Warning: Divide by zero.  ans ans = NaN  pi NaN + 1  inf ans =  NaN NaN  In the tutorial you’ll see more… 46 Summary  Matlab is a high level language  Matlab working environment  Variables variable types + how to use them 47 Floating point  From Wikipedia, the free encyclopedia.  In computing, floating point describes a method of representing an approximation of a real number in a way that can support a wide range of values. The numbers are, in general, represented approximately to a fixed number of significant digits (the mantissa) and scaled using an exponent. The base for the scaling is normally 2, 10 or 16. The typical number that can be represented exactly is of the form: exponent  Significant digits × base The idea of floatingpoint representation over intrinsically integer fixedpoint numbers, which consist purely of significand, is that expanding it with the exponent component achieves greater range. For instance, to represent large values, e.g. distances between galaxies, there is no need to keep all 39 decimal places down to femtometreresolution (employed in particle physics). 48 Floating point (continued)  Assuming that the best resolution is in light years, only the 9 most significant decimal digits matter, whereas the remaining 30 digits carry pure noise, and thus can be safely dropped. This represents a savings of 100 bits of computer data storage. Instead of these 100 bits, much fewer are used to represent the scale (the exponent), e.g. 8 bits or 2 decimal digits. Given that one number can encode both astronomic and subatomic distances with the same nine digits of accuracy, but because a 9digit number is 100 times less accurate than the 11 digits reserved for scale, this is considered a tradeoff exchanging range for precision. The example of using scaling to extend the dynamic range reveals another contrast with fixedpoint numbers: Floatingpoint values are not uniformly spaced. Small values, close to zero, can be represented with much higher resolution (e.g. one femtometre) than large ones because a greater scale (e.g. light years) must be selected for 1 encoding significantly larger values. That is, floatingpoint numbers cannot represent point coordinates with atomic accuracy at galactic distances, only close to the origin. 49 Floating point  The term floating point refers to the fact that a number's radix point (decimal point, or, more commonly in computers, binary point) can "float"; that is, it can be placed anywhere relative to the significant digits of the number. This position is indicated as the exponent component in the internal representation, and floating point can thus be thought of as a computer realization of scientific notation. 50
Website URL
Comment