{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture 02

# Lecture 02 - 14:440:127 Introduction to Computers for...

This preview shows pages 1–3. Sign up to view the full content.

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 Here’s an example of how you need to carefully think through the steps you take when you’re programming. Let’s say we had values stored in the variables a and b. and wanted to switch them. What’s 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 doesn’t 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 won’t work on other types of variables). We’ll 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 don’t 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 you’re working in Matlab, you may also run into some issues where you’ll 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. We’ll talk about this in more depth later in the course. 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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) % 15!
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern