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, Spring 2010 Instructor Blase E. Ur 1 BuiltIn Math Functions Matlab includes many builtin functions for math operations. Here are a number of the most important ones. For these functions, you are specifying input arguments (i.e. 5 is the input argument in the first example). If you have multiple arguments, separate them by commas, as in the second example: sqrt(5) % square root of 5 nthroot(27,3) %cube(3rd) 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 abs(5) % absolute value of 5 log(5) % natural logarithm (base e) of 5 log10(5) % logarithm (base 10) of 5 exp(5) % e^5 Note that you can use these basic functions for more complicated examples i.e. to calculate log 5 25, just do a change of base with the command log(25)/log(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 can give negative (congruent) answers sign(x) % 1 for x>0, 0 for x=0, 1 for x<0 factor(15) % returns a vector of the prime factors of 15 gcd(15,20) % the greatest common divisor lcm(3,7) % least common multiple factorial(15) % 15! primes(100) % lists all primes <= 100 isprime(101) % 1 (true) or 0 (false) 101 is prime 1.1 Function Examples sqrt(9) % ans=3 nthroot(81,4) %ans=3 rem(20,3) % ans = 2 mod(20,3) % ans = 2 rem(20,3) % ans = 2 mod(20,3) % ans = 1 primes(10) % ans = 2 3 5 7 isprime(23549) % ans = 1 1 round(5.3) % ans = 5 fix(5.3) % ans = 5 floor(5.3) % ans = 5 ceil(5.3) % ans = 6 round(5.6) % ans = 6 fix(5.6) % ans = 5 floor(5.6) % ans = 5 ceil(5.6) % ans = 6 round(5.6) % ans = 6 fix(5.6) % ans = 5 floor(5.6) % ans = 6 ceil(5.6) % ans = 5 1.2 Help If you type help followed by the name of a Matlab command, you’ll get that command’s help file. i.e. help round This is very useful if you want to see how a function works or what type of input it expects. 2 Comments Whenever you begin a line or part of a line with a percent sign (%), everything to the right of the percent sign on that line is ignored by Matlab. This is known as a comment. In order to document your Matlab code or remind yourself to do things, you can and should insert comments i.e. stuff = stuff * 10; % adding a zero % remember to take the absolute value Comments are also a very useful tool for debugging (fixing problems with) your programs. When you want to elim inate a few lines of code temporarily, you can ‘comment them out’ rather than cutting and pasting them into and out of your program. You can also make multiple lines part of a comment: {% both this line and this line are a comment %} 3 Inputs and Outputs 3.1 Input So far in this class, the main variability in the programs you’ve written has come from you, the programmers, by changing the value of variables you’ve set. In the real world, you’ll usually want to get input from the user, thechanging the value of variables you’ve set....
View
Full Document
 Spring '08
 Finch
 Linear Algebra, matlab, Matrices

Click to edit the document details