110 likes | 247 Views
Introduction to Programming in MATLAB. Intro. MATLAB Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD is licensed under a Creative Commons Attribution- NonCommercial - ShareAlike 3.0 Unported License . Based on a work at www.peerinstruction4cs.org.
E N D
Introduction to Programming in MATLAB Intro. MATLAB Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Based on a work at www.peerinstruction4cs.org.
A number by any other name wouldn’t smell as sweet? Variable Names
Which of these is NOT a legal variable name in MATLAB? • _factors_of_48_ • my_factors • factor • f • None or more than one of the above
An important programming language concept called “scope” refers to the process for managing names, and how to resolve conflicts where different things have the same name. We’ll talk more about this later, but for now just something you should be aware of.
Computer scientists call words/text/sentences/punctuation “strings” Strings in MATLAB
Strings in MATLAB • Remember: to a computer, EVERYTHING is numbers • Numbers, letters, words, punctuation, images, video, sound • Even computer programs themselves! • In your lab, you got MATLAB to “reveal” to you its code numbers that it associates with each character in a string • ‘a’ = 97, ‘b’ = 98, … • This character code is a widely-used code called ASCII • Next slide gives part of the ASCII code (the whole thing goes from 0 to 255, next slide shows only 32 to 126)
ASCII code (excerpt from 32 to 126)
Which of the following is the result of running this code: >> foo = 'I can't wait to learn more about MATLAB!' + 1 • The value of the variable foo will be set to equal the string, and it will print foo = 'I can't wait to learn more about MATLAB!' • Several numbers will be printed (one for each character in the string) • MATLAB will give an error
Text messages from inside the belly of the beast—just like real text messages with their LOLZ, BRB, IKWYM, IDK, TTYL, program errors require some clues to interpret. Interpreting MATLAB Errors
Which of these shows the outcome when “factor(16+) + 3” is typed? >> factor(16+) + 3 ??? factor(16+) + 3 Error: Unbalanced or unexpected parenthesis or bracket. >> factor(16+) + 3 ??? factor(16+) + 3 | Error: Unbalanced or unexpected parenthesis or bracket. >> factor(16+) + 3 ??? factor(16+) + 3 | Error: Unbalanced or unexpected parenthesis or bracket. (a) (b) (c)
What does the 24 refer to in this error message? >> factor(48.001) ??? Error using ==> factor at 24 N must be a nonnegative integer. • It doesn’t refer to anything—just jibberish • 48.001 is the argument, and 2 * 24 is 48, so it’s a number you encounter when factoring 48 • It is a location in the code for the factor function • None or more than one of the above