lecture 6 matlab functions basics of built-in functions, help feature, elementary functions (e.g.,...

24
Lecture 6 Lecture 6 MATLAB functions MATLAB functions Basics of Built-in Functions, Basics of Built-in Functions, Help Feature, Help Feature, Elementary Functions (e.g., Elementary Functions (e.g., Polynomials, Trigonometric Polynomials, Trigonometric Functions), Functions), Data Analysis, Random Numbers, Data Analysis, Random Numbers, Complex Numbers Complex Numbers © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Post on 19-Dec-2015

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

Lecture 6Lecture 6 MATLAB functions MATLAB functions

Basics of Built-in Functions, Basics of Built-in Functions,

Help Feature,Help Feature,

Elementary Functions (e.g., Polynomials, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Trigonometric Functions),

Data Analysis, Random Numbers, Data Analysis, Random Numbers,

Complex NumbersComplex Numbers

© 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Page 2: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

What is a built-in function?What is a built-in function? A computational expression that uses one or A computational expression that uses one or

more input values to produce an output value.more input values to produce an output value.

MATLAB Functions have 3 components:MATLAB Functions have 3 components:inputinput,, output output, and, and namename

bb = tan(= tan(xx))xx is the is the inputinput, , bb is the is the outputoutput, and , and tantan is is

the the name of a built-in functionname of a built-in function; in this case, it is ; in this case, it is the tangent function with argument in radians.the tangent function with argument in radians.

Page 3: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

a = sqrt(9)a = sqrt(9)

MATLAB functionsMATLAB functions

FunctionsFunctions take the form: take the form: variable = function(number or variable)variable = function(number or variable)

MATLAB has many functions stored in its file MATLAB has many functions stored in its file system.system.

To use one of the built-in functions, you need to To use one of the built-in functions, you need to know its name and what the input values are.know its name and what the input values are.

For example, the For example, the square root functionsquare root function:: sqrt()sqrt().. Find the square root of 9 using MATLABFind the square root of 9 using MATLAB

Page 4: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

HELP featureHELP feature

You may not always recall the name and You may not always recall the name and syntax of a function you want to use since syntax of a function you want to use since MATLAB has so many built-in functions.MATLAB has so many built-in functions.

MATLAB has an indexed library of its built-MATLAB has an indexed library of its built-in functions that you can access through in functions that you can access through the the helphelp feature. feature.

If you type If you type helphelp in the command window in the command window MATLAB provides a list of help topics.MATLAB provides a list of help topics.

Page 5: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

In MATLAB command window type In MATLAB command window type

If we are interested in the elementary If we are interested in the elementary mathematics functions, we find it on the mathematics functions, we find it on the list of help topics (5list of help topics (5thth down) and click it. down) and click it.

A list of commands appears with the A list of commands appears with the description of what each one does.description of what each one does.

Try a few!Try a few!

helphelp

HelpHelp

Page 6: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

MATLAB HelpMATLAB Help

Page 7: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

For more specific help:For more specific help: help topichelp topic

Check it out:Check it out:

MATLAB describes what the function MATLAB describes what the function tantan does does and provides helpful links to similar or related and provides helpful links to similar or related functions to assist you.functions to assist you.

help tanhelp tan

More HelpMore Help

Page 8: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

Use MATLAB help to find the exponential, Use MATLAB help to find the exponential, natural logarithm, and logarithm base 2 natural logarithm, and logarithm base 2 functions.functions.

Calculate eCalculate e77

Calculate ln(4)Calculate ln(4) Calculate logCalculate log22(12)(12)

exp(7)exp(7)

log2(12)log2(12)

log(4)log(4)

Hands-onHands-on

Page 9: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

Help NavigatorHelp Navigator

Click on Click on HelpHelp on the tool bar at the top of on the tool bar at the top of MATLAB, and select MATLAB, and select MATLAB HelpMATLAB Help..

A HELP window will pop up.A HELP window will pop up. Under Under Help NavigatorHelp Navigator on the left of screen on the left of screen

select the select the SearchSearch tab. tab. You can Search for specific help topics by You can Search for specific help topics by

typing your topic in the space after the typing your topic in the space after the ::.. You can also press You can also press F1F1 on your keyboard on your keyboard

to access the windowed help.to access the windowed help.

Page 10: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

Help Navigator windowHelp Navigator window

Page 11: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

MATLAB can perform all of the operations MATLAB can perform all of the operations that your calculator can and more.that your calculator can and more.

Search for the topic Search for the topic Elementary mathElementary math in in the Help Navigator just described.the Help Navigator just described.

Try the following in MATLAB to continue Try the following in MATLAB to continue your exploration of MATLAB capabilitiesyour exploration of MATLAB capabilities

ans=3ans=3

ans=1.9459ans=1.9459

Of course, try a few others.Of course, try a few others.

sqrt(9)sqrt(9)log(7)log(7)

Elementary Mathematical FunctionsElementary Mathematical Functions

Page 12: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

Sometimes it is necessary to round numbers. MATLAB Sometimes it is necessary to round numbers. MATLAB provides several utilities to do this.provides several utilities to do this.

What do they do?What do they do? Try the exercises above with Try the exercises above with yy in place of in place of xx..

round(xround(x))fix(xfix(x))floor(xfloor(x))ceil(xceil(x))

x=16.3x=16.3y=3.9y=3.9

Rounding functionsRounding functions

Page 13: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

What do these functions do?What do these functions do? If you are not sure, use the help feature.If you are not sure, use the help feature.

factor(10)factor(10)

rats(4.2)rats(4.2)

factorial(3)factorial(3)

gcd(20,10)gcd(20,10)

lcm(4,6)lcm(4,6)

Discrete-mathematics functionsDiscrete-mathematics functions

Page 14: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

Trigonometric functionsTrigonometric functions MATLAB can compute numerous trigonometric MATLAB can compute numerous trigonometric

functions, for angles in radians.functions, for angles in radians.

To convert degrees to radians, the conversion is based To convert degrees to radians, the conversion is based on the relationship: on the relationship: – 180 degrees = pi × radians180 degrees = pi × radians

Note: Note: pipi is a built-in constant in MATLAB. is a built-in constant in MATLAB. REMARK: Numerical computations with decimal-point REMARK: Numerical computations with decimal-point

numbers are only approximate (just like your calculator). numbers are only approximate (just like your calculator). If you try to find If you try to find sin(pi)sin(pi),, MATLAB will not return 0 as MATLAB will not return 0 as expected, but rather 1.2246e-016.expected, but rather 1.2246e-016.

Page 15: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

Hands-onHands-on

Use MATLAB to find the sine of 360 Use MATLAB to find the sine of 360 degrees.degrees.

Use MATLAB to find the arccosine of -1 in Use MATLAB to find the arccosine of -1 in degrees (the help function may be handy).degrees (the help function may be handy).

Use MATLAB to find the inverse tangent of Use MATLAB to find the inverse tangent of x as x ranges from -1 to 1 in steps of 0.1.x as x ranges from -1 to 1 in steps of 0.1.

Page 16: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

Data analysis functionsData analysis functions

It is often necessary to analyze data statistically. It is often necessary to analyze data statistically. MATLAB has many statistical functions:MATLAB has many statistical functions:

size() size() length()length()std()std()var()var()

max()max()min()min()mean()mean()median(median())

sum() sum() prod()prod()sort()sort()sortrows()sortrows()

Page 17: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

What is the largest number in vector x and What is the largest number in vector x and where is it located?where is it located?

What is the median of the above vector?What is the median of the above vector?

What is the sum of the above vector?What is the sum of the above vector?sum(xsum(x))

median(xmedian(x))

[[value,positionvalue,position] = ] = max(xmax(x))

x = [5, 3, 7, 10, 4]x = [5, 3, 7, 10, 4]

Data analysis practiceData analysis practice

ans = 29ans = 29

ans = 5ans = 5

value = 10value = 10 highest value is 10highest value is 10position = 4position = 4 located in the 4th columnlocated in the 4th column

Page 18: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

Sort v in descending order.Sort v in descending order.

Find the size of y.Find the size of y.

Find the standard deviation of v.Find the standard deviation of v.

Find the cumulative product of v.Find the cumulative product of v.

Sort the rows of y based on the 3Sort the rows of y based on the 3rdrd column. column.

v =[2, 24, 53, 7, 84, 9]v =[2, 24, 53, 7, 84, 9]

y = [2, 4, 56; 3, 6, 88]y = [2, 4, 56; 3, 6, 88]

Hands-onHands-on

Page 19: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

w=40.*rand(1)w=40.*rand(1)

Generation of random numbersGeneration of random numbers

rand(n)rand(n) produces an n×n matrix of produces an n×n matrix of random numbers from 0 to 1.random numbers from 0 to 1.

rand(n,m)rand(n,m) produces an n×m matrix of produces an n×m matrix of random numbers between 0 and 1.random numbers between 0 and 1.

To use MATLAB to produce a random number To use MATLAB to produce a random number between 0 and 40, do the following:between 0 and 40, do the following:

Page 20: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

Complex numbersComplex numbers

Complex numbersComplex numbers take the form of take the form of a+b*ia+b*i::– a is the real parta is the real part– b is the imaginary partb is the imaginary partwhere i = sqrt(-1).where i = sqrt(-1).

Complex numbers can be assigned in Complex numbers can be assigned in MATLAB on command lines as follows:MATLAB on command lines as follows:>>>> a = 2; b = 3; a = 2; b = 3; % Must assign a & b before% Must assign a & b before>>>> c = a+b*i c = a+b*i % assigning c as shown % assigning c as shown >>>> c = complex(a,b) c = complex(a,b) % or this other way.% or this other way.

Page 21: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

Complex numbers continuedComplex numbers continued To find the To find the real real andand imaginary components imaginary components

of a complex number:of a complex number:real(c)real(c)imag(c)imag(c)

To find the To find the absolute value absolute value oror modulus modulus of a of a complex number:complex number:abs(c)abs(c)

To find the To find the angle angle oror argument argument expressed expressed in radians of a complex number:in radians of a complex number:angle(c)angle(c)

Page 22: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

Other useful functionsOther useful functions

clockclock: produces an array that tells year, : produces an array that tells year, month, day, hour, min, sec.month, day, hour, min, sec.

datedate: tells date: tells date pipi: the number pi (3.141592653589…..): the number pi (3.141592653589…..) ii: imaginary number : imaginary number % i = sqrt(-1)% i = sqrt(-1) jj: imaginary number : imaginary number % j = sqrt(-1)% j = sqrt(-1) epseps: smallest difference between two : smallest difference between two

numerically-computed, adjacent real numerically-computed, adjacent real (‘floating point’) numbers = 2.2204e-(‘floating point’) numbers = 2.2204e-016. 016.

Page 23: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

ExercisesExercises

Find the modulus (magnitude, abs) and angle Find the modulus (magnitude, abs) and angle (argument) of the complex number 3+4i.(argument) of the complex number 3+4i.

Generate a 4x4 array of random numbers Generate a 4x4 array of random numbers between 0 and 10. Sort each column of the between 0 and 10. Sort each column of the array.array.

Use MATLAB’s help function to find built-in Use MATLAB’s help function to find built-in functions to determine:functions to determine:– The base 10 logarithm of 5The base 10 logarithm of 5– The secant of piThe secant of pi

Page 24: Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,

SummarySummary

Help FeatureHelp Feature Basic Math FunctionsBasic Math Functions Rounding FunctionsRounding Functions Discrete Mathematics FunctionsDiscrete Mathematics Functions Trigonometric FunctionsTrigonometric Functions Data AnalysisData Analysis Random NumbersRandom Numbers Complex NumbersComplex Numbers