recap script m-file editor/debugger window cell mode chapter 3 “built in matlab function” using...

Post on 03-Jan-2016

218 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Lecture 21

Recap Script M-fileEditor/Debugger WindowCell ModeChapter 3 “Built in MATLAB Function”

Using Built-in Functions Using the HELP Feature Window HELP ScreenElementary Math FunctionsRounding FunctionsDiscrete MathematicsTrigonometric FunctionData Analysis Function

Maximum and Minimum Mean and Median

Sums and Products

Sorting Values

Matrix Size

Variance and Standard DeviationThe standard deviation and variance are measures of how much elements in a

data set vary with respect to each otherEvery student knows that the average score on a test is important, but you

also need to know the high and low scores to get an idea of how well you did. Test scores, like many kinds of data that are important in engineering, are often distributed in a “bell”-shaped curve

In a normal (Gaussian) distribution of a large amount of data, approximately 68% of the data falls within one standard deviation (sigma) of the mean (one sigma)

If the range is extended to a two-sigma variation ( two sigma), approximately 95% of the data should fall inside these bounds, and if you go out to three sigma, over 99% of the data should fall in this range

Usually, measures such as the standard deviation and variance are meaningful only with large data sets.

Normal Distribution

Random Numbers Random numbers are often used in engineering

calculations to simulate measured dataMeasured data rarely behave exactly as predicted by

mathematical models, so we can add small values of random numbers to our predictions to make a model behave more like a real system

Random numbers are also used to model games of chanceTwo different types of random numbers can be generated

in MATLAB:Uniform random numbers Gaussian random numbers

Uniform Random Numbers Uniform random numbers are generated with the rand function.

These numbers are evenly distributed between 0 and 1We can create a set of random numbers over other ranges by

modifying the numbers created by the rand functionFor example:

to create a set of 100 evenly distributed numbers between 0 and 5

first create a set over the default range with the command

r = rand(100,1);This results in a 100x1 matrix of valuesNow we just need to multiply by 5 to expand the range to 0 to

5:

r = r * 5;

Example Continued….If we want to change the range to 5 to 10, we can

add 5 to every value in the array:

r = r + 5;The result will be random numbers varying from 5

to 10. We can generalize these results with the equation

x=(max - min) . random_number_set + min

Gaussian Random Numbers Gaussian random numbers have the normal distribution There is no absolute upper or lower bound to a data set of

this type; we are just less and less likely to find data, the farther away from the mean we get

Gaussian random-number sets are described by specifying their average and the standard deviation of the data set

MATLAB generates Gaussian values with a mean of 0 and a variance of 1.0, using the randnfunction

For example:randn(3)returns a 3x3 matrix

ans =

Continued….If we need a data set with a different average or a different

standard deviation, we start with the default set of random numbers and then modify it

Since the default standard deviation is 1, we must multiply by the required standard deviation for the new data set

Since the default mean is 0, we’ll need to add the new mean:

x = standard_deviation . random_data_set + meanFor example: to create a sequence of 500 Gaussian random

variables with a standard deviation of 2.5 and a mean of 3, type

x = randn(1500)*2.5 + 3;

Complex Numbers MATLAB includes several functions used primarily with complex numbersComplex numbers consist of two parts

a real component an imaginary component

For example: 5+3i is a complex number. The real component is 5, and the imaginary component is 3.

Complex numbers can be entered into MATLAB in two ways: as an addition problem,

such as

A = 5 + 3i or A = 5+3*i or with the complex function, as in

A = complex(5,3)

which returns

A = 5.0000 + 3.0000i

Continued….As is standard in MATLAB, the input to the complex

function can be either two scalars or two arrays of valuesThus, if x and y are defined as

x = 1:3;

y = [-1,5,12];then the complex function can be used to define an array of

complex numbers as follows:

complex(x,y)ans =

1.0000 - 1.0000i 2.0000 + 5.0000i 3.0000 +12.0000i

real and imag FunctionThe real and imag functions can be used to separate the

real and imaginary components of complex numbersFor example: for A = 5 + 3*i , we have

real(A)

ans = 5imag(A)

ans = 3

isreal FunctionThe isreal function can be used to determine whether a

variable is storing a complex numberIt returns a 1 if the variable is real and a 0 if it is complexSince A is a complex number, we get

isreal(A)

ans =0Thus, the isreal function is false and returns a value of 0

Conjugate of Complex NumberThe complex conjugate of a complex number consists of the

same real component, but an imaginary component of the opposite sign

The conj function returns the complex conjugate:conj(A)

ans = 5.0000 - 3.0000iThe transpose operator also returns the complex conjugate of

an array, in addition to converting rows to columns and columns to rows

Thus, we haveA'

ans = 5.0000 - 3.0000i

top related