Lecture 6 MATLAB functions Basics of Builtin Functions
Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e. g. , Polynomials, Trigonometric Functions), Data Analysis, Random Numbers, Complex Numbers © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
What is a built-in function? n A computational expression that uses one or more input values to produce an output value. n MATLAB Functions have 3 components: input, output, and name b = tan(x) x is the input, b is the output, and tan is the name of a built-in function; in this case, it is the tangent function with argument in radians.
MATLAB functions n Functions take the form: variable = function(number or variable) n n MATLAB has many functions stored in its file system. To use one of the built-in functions, you need to know its name and what the input values are. For example, the square root function: sqrt(). Find the square root of 9 using MATLAB
HELP feature n You may not always recall the name and syntax of a function you want to use since MATLAB has so many built-in functions. n MATLAB has an indexed library of its builtin functions that you can access through the help feature. n If you type help in the command window MATLAB provides a list of help topics.
Help n In MATLAB command window type n If we are interested in the elementary mathematics functions, we find it on the list of help topics (5 th down) and click it. n A list of commands appears with the description of what each one does. n Try a few!
MATLAB Help
More Help n For more specific help: help topic n Check it out: n MATLAB describes what the function tan does and provides helpful links to similar or related functions to assist you.
Hands-on n Use MATLAB help to find the exponential, natural logarithm, and logarithm base 2 functions. n Calculate e 7 n Calculate ln(4) n Calculate log 2(12)
Help Navigator n Click on Help on the tool bar at the top of MATLAB, and select MATLAB Help. n A HELP window will pop up. n Under Help Navigator on the left of screen select the Search tab. n You can Search for specific help topics by typing your topic in the space after the : . n You can also press F 1 on your keyboard to access the windowed help.
Help Navigator window
Elementary Mathematical Functions n MATLAB can perform all of the operations that your calculator can and more. n Search for the topic Elementary math in the Help Navigator just described. n Try the following in MATLAB to continue your exploration of MATLAB capabilities ans=3 ans=1. 9459 Of course, try a few others.
Rounding functions n Sometimes it is necessary to round numbers. MATLAB provides several utilities to do this. n n What do they do? Try the exercises above with y in place of x.
Discrete-mathematics functions What do these functions do? n If you are not sure, use the help feature. n
Trigonometric functions n MATLAB can compute numerous trigonometric functions, for angles in radians. n To convert degrees to radians, the conversion is based on the relationship: – 180 degrees = pi × radians Note: pi is a built-in constant in MATLAB. n REMARK: Numerical computations with decimal-point numbers are only approximate (just like your calculator). If you try to find sin(pi), MATLAB will not return 0 as expected, but rather 1. 2246 e-016. n
Hands-on n Use MATLAB to find the sine of 360 degrees. n Use MATLAB to find the arccosine of -1 in degrees (the help function may be handy). n Use MATLAB to find the inverse tangent of x as x ranges from -1 to 1 in steps of 0. 1.
Data analysis functions It is often necessary to analyze data statistically. n MATLAB has many statistical functions: n max() min() mean() median() sum() prod() sortrows() size() length() std() var()
Data analysis practice What is the largest number in vector x and where is it located? value = 10 position = 4 highest value is 10 located in the 4 th column What is the median of the above vector? ans = 5 What is the sum of the above vector? ans = 29
Hands-on Sort v in descending order. Find the size of y. Find the standard deviation of v. Find the cumulative product of v. Sort the rows of y based on the 3 rd column.
Generation of random numbers n n rand(n) produces an n×n matrix of random numbers from 0 to 1. rand(n, m) produces an n×m matrix of random numbers between 0 and 1. To use MATLAB to produce a random number between 0 and 40, do the following:
Complex numbers n Complex numbers take the form of a+b*i: – a is the real part – b is the imaginary part where i = sqrt(-1). n Complex numbers can be assigned in MATLAB on command lines as follows: >> a = 2; b = 3; % Must assign a & b before >> c = a+b*i % assigning c as shown >> c = complex(a, b) % or this other way.
Complex numbers continued n To find the real and imaginary components of a complex number: real(c) imag(c) n To find the absolute value or modulus of a complex number: abs(c) n To find the angle or argument expressed in radians of a complex number: angle(c)
Other useful functions n clock: produces an array that tells year, month, day, hour, min, sec. n date: tells date n pi: the number pi (3. 141592653589…. . ) n i: imaginary number % i = sqrt(-1) n j: imaginary number % j = sqrt(-1) n eps: smallest difference between two numerically-computed, adjacent real (‘floating point’) numbers = 2. 2204 e-016.
Exercises Find the modulus (magnitude, abs) and angle (argument) of the complex number 3+4 i. n Generate a 4 x 4 array of random numbers between 0 and 10. Sort each column of the array. n Use MATLAB’s help function to find built-in functions to determine: n – The base 10 logarithm of 5 – The secant of pi
Summary n n n n Help Feature Basic Math Functions Rounding Functions Discrete Mathematics Functions Trigonometric Functions Data Analysis Random Numbers Complex Numbers
- Slides: 24