CMPSC 200 Lecture 03 notes PDF

Title CMPSC 200 Lecture 03 notes
Course Programming For Engineers With Matlab
Institution The Pennsylvania State University
Pages 3
File Size 124.7 KB
File Type PDF
Total Downloads 7
Total Views 173

Summary

Lecture notes for Lesson 3. Supplement to video lesson...


Description

Sottile

!

CMPSC 200 Programming for Engineers with MATLAB Lecture 03 Outline Related Reading: Chapter 2, §2.3.2; Chapter 3, §3.1 through §3.4 Array Operations 1) MATLAB stores “arrays” of data as matrices 1 2 2) A few equivalent statements to write 𝐴 = 3 4 a. A = [1 2; 3 4] b. A = [1,2; 3,4] c. A = [1 2; 3 4] d. A = [1,2; 3,4] 3) The 𝑖th row and 𝑗th column of a matrix can be found as A(i,j). 4) Shortcut for creating a row of regularly spaced sets of numbers a. Start at a, increment in steps of b until you get to c b. a:b:c or [a:b:c](MATLAB accepts both notations; the first is preferred) c. If no b is indicated, default is a:1:b Built-in MATLAB Functions – Part I 1) Common Math Operations a. Absolute value: abs(x) b. Square root: sqrt(x) (compare it against x^0.5) c. 𝑛th root: nthroot(x,n) d. Sign or Signum Function : sign(x), which returns: i. 1 if the corresponding element of x is greater than zero ii. 0 if the corresponding element of x equals zero iii. -1 if the corresponding element of x is less than zero e. Remainder: rem(x,y) (remainder of 𝑥 divided by 𝑦) f. Powers i. Exponents: 5 ∗ 10/0 calculated as 5*10^10, 5*(10^10) or as 5E10 ii. Exponential: exp(x) calculates 𝑒 2 g. Logarithms i. ln5(𝑥): log(x) (tricky to remember – many languages do this, though) ii. log/0 (𝑥): log10(x) iii. Change of base formula to get others: log ; (𝑥) =

? 2 ? ;

2) Rounding functions a. Rounding: round(x) (default, to integers; see help documentation for options) b. Truncation: fix(x) c. Floor (rounds to nearest integer towards −∞): floor(x) d. Ceiling (rounds to nearest integer towards ∞): ceil(x)

!

Sottile 3) Discrete Math a. Prime factors of a number 𝑥: factor(x) b. Greatest common denominator of 𝑥 and 𝑦: gcd(x,y) c. Least common multiple of 𝑥 and 𝑦: lcm(x,y) d. Convert decimal number to fraction: rats(x) e. Factorial (𝑛! = 𝑛 ∗ (𝑛 − 1) ∗ 𝑛 − 2 ∗ … ∗ 2 ∗ 1): factorial(n) f. Prime Numbers i. Find all prime numbers less than or equal to some number: primes(x) ii. Check if a number is a prime number: isprime(x). isprime returns 1 if the number is prime, or returns 0 if the number is not prime (this choice of true/false representations is typical in computer science). 4) Trig Functions: default is radians vs. degrees (if it is available) a. Sine i. sin 𝑥 : sin(x) vs. sind(x) ii. sinF/ (𝑥):5asin(x) vs. asind(x) iii. sinh5(𝑥): sinh(x) iv. sinhF/ (𝑥): asinh(x) b. Cosine i. cos 𝑥 : cos(x) vs. cosd(x) ii. cos F/ (𝑥): acos(x) vs. acosd(x) iii. cosh 𝑥 :5cosh(x) iv. coshF/ (𝑥): acosh(x) c. Tangent i. tan 𝑥 : tan(x) vs. tand(x) ii. tanF/ (𝑥): atan(x) vs. atand(x) iii. tanh5(𝑥): tanh(x) iv. tanhF/ (𝑥): atanh(x) d. Secant i. sec5(𝑥): sec(x) vs. secd(x) ii. secF/ (𝑥): asec(x) vs. asecd(x) iii. sech5(𝑥): sech(x) iv. sechF/ (𝑥): asech(x) e. Cosecant i. csc5(𝑥): csc(x) vs. cscd(x) ii. cscF/ (𝑥): acsc(x) vs. acscd(x) iii. csch5(𝑥): csch(x) iv. cschF/ (𝑥): acsch(x) f. Cotangent i. cot5(𝑥): cot(x) vs. cotd(x) ii. cotF/ (𝑥): acot(x) vs. acotd(x) iii. coth5(𝑥): coth(x) iv. cothF/ (𝑥): acoth(x)

Sottile

!

5) Language Help: a. Built-in Help i. help, or help ____ (where ____ is item of interest) ii. doc, or doc ____ (where ____ is item of interest) b. MathWorks website: http://www.mathworks.com/help/matlab/...


Similar Free PDFs