# CMPSC 200 Lecture Notes - Lecture 3: Matlab, Square Root, Prime Number

3 pages27 viewsFall 2017

Department

Computer ScienceCourse Code

CMPSC 200Professor

Bradley SottileLecture

3This

**preview**shows half of the first page. to view the full**3 pages of the document.**! 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

2) A few equivalent statements to write ! " # $

% &

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: , - #./0 calculated as 5*10^10, 5*(10^10) or as 5E10

ii. Exponential: exp(x) calculates 12

g. Logarithms

i. 3456*7: log(x) (tricky to remember – many languages do this, though)

ii. 389/06*7: log10(x)

iii. Change of base formula to get others: 389;6*7 " <=>?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 @A): floor(x)

d. Ceiling (rounds to nearest integer towards A): ceil(x)

###### You're Reading a Preview

Unlock to view full version

#### Loved by over 2.2 million students

Over 90% improved by at least one letter grade.