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 13 Final Exam Review Rutgers University, Fall 2008 Instructor Blase E. Ur 1 Written Question 1 Conditional Statements Relational operators: > greater than < less than >= greater than / equal to == (2 equals signs!) equal to ~= NOT equal to Logical Operators: & means AND only if both are true is the whole thing true  means OR if either is true, whole thing is true ~ means NOT switches True and False 1.1 IfElseif...Else Statements if(condition) statement1 elseif(condition2) %optional statement2 ... else %optional statement3 end if(age>=65) disp(’You are a senior citizen’) elseif(age>=25) disp(’You are a grownup’) elseif(age>=18) disp(’You are a college student’) elseif(age>=5) disp(’You are in school’) else disp(’You are a little kid’); end It’s important to note in this example that only one of these options is chosen. In essence, beginning at the if and going all the way to the end statement, the first true condition will have its statement(s) executed, and all of the others will be ignored. As a result, the order of the statements matters very much! What would happen if the first statement in the example above were if(age >= 5)? Well, it would display ’You are in school’ for everyone over age 5, overriding the messages about senior citizens, grownups, and college students. Switch case is an alternative to If Statements when you have a variable that only takes on a certain number of discrete values. You must replace ”VARIABLE” with the name of the variable you wish to test. Replace OPTION1, OPTION2, etc. with values (don’t put x==5, you JUST type 5). If you want to include more than one value, put all of the possibilities in a vector (or cell array, if you are using strings). Here’s an example of switch case in action. Note that ”exciting” will be displayed: destination= ’cambodia’; switch destination case ’florida’ disp(’boring!’) case ’bosnia’ disp(’a little less boring’) case {’cambodia’ , ’burma’ } disp(’exciting’) otherwise disp(’i don’’t know about that country’) end 1.2 BuiltIn Math Functions Matlab includes many builtin functions for math operations. Here are a number of the most important ones: sqrt(5), nthroot(27,3), log(5), log10(5), exp(5) rem(15,2), factor(15), factorial(15), primes(100), isprime(101) round(5.3), fix(5.3) % 0, floor(5.3) % inf, ceil(5.3) % +inf 1.3 Output, Input Remember that %s is a placeholder for a string, and %f is a placeholder for a number. You can say something like %.0f , which gives you 0 points after the decimal. name = ’Jones’; x = 14.7; fprintf(’Mr. %s has %.0f Matlab books.\n’, name, x) This would display ”Mr. Jones has 15 Matlab books.” money = input(’Please enter how much to withdraw: ’); 2 Written Question 1b Plotting In general, to plot, you need the x points and y points as separate vectors. Let’s say that we have x pointsIn general, to plot, you need the x points and y points as separate vectors....
View
Full Document
 Fall '08
 Finch
 matlab, Derivative, Exponentiation, Blase E. Ur

Click to edit the document details