This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 14:440:127 Introduction to Computers for Engineers Notes for Lecture 02 Rutgers University, Fall 2009 Instructor- Blase E. Ur 1 Variables Continued from Lecture 1 1.1 Switching Variables Heres an example of how you need to carefully think through the steps you take when youre programming. Lets say we had values stored in the variables a and b. and wanted to switch them. Whats the problem if you type the following?: a = b; b = a; Well, now they both have the value originally stored in b. So how do you perform this switch? Use an intermediate temporary variable: temp = a; % store the value of a into temp a = b; % overwrite a with the value of b b = temp; % overwrite b with the original a 2 Data Types Unlike a number of popular computer languages, Matlab does not require you to declare variables or their types. I.e. it doesnt matter if you are going to store an integer, an array of characters, or a decimal number, you can just start using a variable. Matlab is dynamically typed, which means that you can change the type of a variable in the middle of a program without any problems. However, it is strongly typed, which means that it performs checks (and if one type of variable is required for an operation, it wont work on other types of variables). Well see more about data types later in the class. For now, just know that you can type in integers and doubles (essentially, decimals) as numbers. If you want to use alphanumeric text in Matlab, you need to enclose that text in single quotes: i.e. my text . If you dont enclose the text in single quotes, Matlab will think you are referring to variables. 2.1 NaN, .99999 In Matlab, if you run into NaN, that means not a number. This usually results from trying to perform undefined operations, i.e. dividing 0 by 0. As youre working in Matlab, you may also run into some issues where youll perform a calculation that should evaluate to 6, but instead it evaluates to 5.99999999999999. The reason for this is that Matlab only performs calculations with a certain degree of precision, and also uses numerical methods to perform calculations. Well talk about this in more depth later in the course. 1 3 Built-In Math Functions Matlab includes many built-in functions for math operations. Here are a number of the most important ones: sqrt(5) % square root of 5 nthroot(27,3) %cube(3) root of 27 sin(pi) % sine of pi radians cos(pi/2) % cosine of pi/2 asin(1) % arcsine of 1 sind(75) % sine of 75 degrees log(5) % natural logarithm (base e) of 5 log10(5) % logarithm (base 10) of 5 exp(5) % e^5 round(5.3) % round 5.3 (.5 or greater rounds up) fix(5.3) % round towards 0 floor(5.3) % round towards -inf ceil(5.3) % round towards +inf rem(15,2) % remainder of 15/2 mod(15,2) % similar to rem % but different for different signs sign(x) % 1 for x>0, 0 for x=0, -1 for x<0 factor(15) % returns the prime factors of 15 gcd(15,20) % the greatest common divisor lcm(3,7) % least common multiple factorial(15)...
View Full Document
- Fall '09