Download - Lecture 24: Rough and Ready Analysis
![Page 1: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/1.jpg)
Lecture 24: Rough and Ready Analysis
![Page 2: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/2.jpg)
MATLAB uses function names consistent with most major programming languages
For example sqrt sin cos log
![Page 3: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/3.jpg)
Function Input can be either scalars or matrices
![Page 4: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/4.jpg)
Function Input can be either scalars or matrices
![Page 5: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/5.jpg)
Some functions return multiple results
size function determines the number of rows and columns
![Page 6: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/6.jpg)
You can assign names to the output
![Page 7: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/7.jpg)
Nesting Functions
![Page 8: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/8.jpg)
There are functions for almost anything you want to do
Use the help feature to find out what they are and how to use them From the command window From the help selection on the menu
bar
![Page 9: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/9.jpg)
Elementary Math Functions
abs(x) absolute valuesign(x) plus or minusexp(x) ex
log(x) natural loglog10(x) log base 10
![Page 10: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/10.jpg)
Rounding Functions
round(x)fix(x)floor(x)ceil(x)
![Page 11: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/11.jpg)
Discrete Mathematics
factor(x)gcd(x,y) greatest common denominator
lcm(x) lowest common multiple
rats(x) represent x as a fraction
factorial(x)primes(x)isprime(x)
![Page 12: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/12.jpg)
Trigonometric Functions
sin(x) sinecos(x) cosinetan(x) tangentasin(x) inverse sinesinh(x) hyperbolic sineasinh(x) inverse hyperbolic sinesind(x) sine with degree inputasind(x) inverse sin with degree output
![Page 13: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/13.jpg)
Data Analysis
max(x)min(x)mean(x)median(x)sum(x)prod(x)sort(x)
![Page 14: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/14.jpg)
When x is a matrix, the max is found for each column
![Page 15: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/15.jpg)
max value
element number where the max value occurs
Returns both the smallest value in a vector x and its location in vector x.
![Page 16: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/16.jpg)
Vector of maximums
Vector of row numbers
Returns a row vector containing the minimum element from each column of a matrix x, and returns a row vector of the location of the minimum in each column of matrix x.
![Page 17: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/17.jpg)
Determining Matrix Sizesize(x) number of rows and columnslength(x) biggest dimension
![Page 18: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/18.jpg)
Variance and Standard Deviation
std(x)var(x)
( )
11
2
2
−
−=∑
=
N
xN
kk μ
σ
![Page 19: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/19.jpg)
Random Numbers
rand(n) Returns an n by n matrix of
random numbers between 0 and 1
rand(n,m) Returns an n by m matrix of
random numbers
These random numbers are evenly distributed
![Page 20: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/20.jpg)
Manipulating Matrices
• Defining matrices• A matrix can be defined by typing in a list of numbers
enclosed in square brackets.• The numbers can be separated by spaces or
commas.• New rows are indicated with a semicolon.
A = [ 3.5 ];B = [1.5, 3.1]; or B =[1.5 3.1];C = [-1, 0, 0; 1, 1, 0; 0, 0, 2];
![Page 21: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/21.jpg)
Manipulating Matrices
• Defining matrices• Define a matrix by using another matrix that has
already been defined.
• Reference an element in a matrix• Both row and column indices start from 1.
B = [1.5, 3.1]; S = [3.0, B]
S = [3.0, 1.5, 3.1]; T = [1 2 3; S]
S = 3.0 1.5 3.1 T = 1 2 3 3.0 1.5 3.1
S(2)
T(2, 3)
T(5)
The element value on row 2 and column 3 of matrix T
Count down column 1, then down column 2, and finally down column 3 to the correct element of matrix T.
![Page 22: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/22.jpg)
Manipulating Matrices
• Change values in a matrix• S(2) = -1.0;
changes the 2nd value in the matrix S
from 1.5 to -1.0
• Extend a matrix by defining new elements.
S = 3.0 1.5 3.1 T = 1 2 3 3.0 1.5 3.1
S(4) = 5.5;Extend the matrix S to four elements instead of three.
S(8) = 9.5;Matrix S will have eight values, and the values of S(5), S(6), S(7) will be set to 0.
T(3, 3) = 10;T(4, 5) = 20;
![Page 23: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/23.jpg)
Manipulating Matrices
• Using the colon operator• Define an evenly spaced matrix
H = 1:8 --- The default spacing is 1
time = 0.0:0.5:3.0 --- The middle value becomes the spacing.
• Extract data from matrices x = M( :, 1) --- extract column 1 from matrix M
y = M( :, 4) --- extract column 4 from matrix M
z = M(2, : ) --- extract row 2 from matrix M
a = M(2:3, : ) --- extract rows 2 and 3 from matrix M
b = M( :, 2:4) --- extract column 2 to column 4 from matrix M
c = M(2:3, 3:5) --- extract not whole rows or whole columns
from matrix M
• Converts a two dimensional matrix to a single column M( : )
M = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7
![Page 24: Lecture 24: Rough and Ready Analysis](https://reader036.vdocuments.net/reader036/viewer/2022081501/56813e20550346895da80031/html5/thumbnails/24.jpg)
Practice QuestionQ. What is the output of the following program? int main (void) { int magicNumber = 25; if ( magicNumber < 70 ) { printf("The number is a bit low ... "); } if ( magicNumber > 100 ) { printf("The number is way too high ... "); } if ( magicNumber = 77 ) { /* note the single equal sign! */ printf("77 is a very lucky number. "); } if (magicNumber > 70 ) { printf("At least you are not TOO low."); } }
a.) The number is a bit low ... b.) The number is a bit low ... 77 is a very lucky number. c.) The number is a bit low ... 77 is a very lucky number. At least you are not TOO low. d.) The number is a bit low ... At least you are not TOO low. e.) The number is way too high …
Solu
tion
: C