Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,"— Presentation transcript:

1 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.

2 What is a built-in function? A computational expression that uses one or more input values to produce an output value. A computational expression that uses one or more input values to produce an output value. MATLAB Functions have 3 components: MATLAB Functions have 3 components: input, output, and name b = tan(x) 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.

3 MATLAB functions Functions take the form: Functions take the form: variable = function(number or variable) MATLAB has many functions stored in its file system. 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. 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(). For example, the square root function: sqrt(). Find the square root of 9 using MATLAB Find the square root of 9 using MATLAB

4 HELP feature You may not always recall the name and syntax of a function you want to use since MATLAB has so many built-in functions. You may not always recall the name and syntax of a function you want to use since MATLAB has so many built-in functions. MATLAB has an indexed library of its built- in functions that you can access through the help feature. MATLAB has an indexed library of its built- in functions that you can access through the help feature. If you type help in the command window MATLAB provides a list of help topics. If you type help in the command window MATLAB provides a list of help topics.

5 In MATLAB command window type In MATLAB command window type If we are interested in the elementary mathematics functions, we find it on the list of help topics (5 th down) and click it. If we are interested in the elementary mathematics functions, we find it on the list of help topics (5 th down) and click it. A list of commands appears with the description of what each one does. A list of commands appears with the description of what each one does. Try a few! Try a few! Help

6 MATLAB Help

7 For more specific help: help topic For more specific help: help topic Check it out: Check it out: MATLAB describes what the function tan does and provides helpful links to similar or related functions to assist you. MATLAB describes what the function tan does and provides helpful links to similar or related functions to assist you. More Help

8 Use MATLAB help to find the exponential, natural logarithm, and logarithm base 2 functions. Use MATLAB help to find the exponential, natural logarithm, and logarithm base 2 functions. Calculate e 7 Calculate e 7 Calculate ln(4) Calculate ln(4) Calculate log 2 (12) Calculate log 2 (12) Hands-on

9 Help Navigator Click on Help on the tool bar at the top of MATLAB, and select MATLAB Help. Click on Help on the tool bar at the top of MATLAB, and select MATLAB Help. A HELP window will pop up. A HELP window will pop up. Under Help Navigator on the left of screen select the Search tab. Under Help Navigator on the left of screen select the Search tab. You can Search for specific help topics by typing your topic in the space after the :. You can Search for specific help topics by typing your topic in the space after the :. You can also press F1 on your keyboard to access the windowed help. You can also press F1 on your keyboard to access the windowed help.

10 Help Navigator window

11 MATLAB can perform all of the operations that your calculator can and more. MATLAB can perform all of the operations that your calculator can and more. Search for the topic Elementary math in the Help Navigator just described. Search for the topic Elementary math in the Help Navigator just described. Try the following in MATLAB to continue your exploration of MATLAB capabilities Try the following in MATLAB to continue your exploration of MATLAB capabilities ans=3 ans=3 ans=1.9459 ans=1.9459 Of course, try a few others. Elementary Mathematical Functions

12 Sometimes it is necessary to round numbers. MATLAB provides several utilities to do this. Sometimes it is necessary to round numbers. MATLAB provides several utilities to do this. What do they do? What do they do? Try the exercises above with y in place of x. Try the exercises above with y in place of x. Rounding functions

13 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. Discrete-mathematics functions

14 Trigonometric functions MATLAB can compute numerous trigonometric functions, for angles in radians. MATLAB can compute numerous trigonometric functions, for angles in radians. To convert degrees to radians, the conversion is based on the relationship: 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. Note: pi is a built-in constant in MATLAB. 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.2246e-016. 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.2246e-016.

15 Hands-on Use MATLAB to find the sine of 360 degrees. Use MATLAB to find the sine of 360 degrees. Use MATLAB to find the arccosine of -1 in degrees (the help function may be handy). Use MATLAB to find the arccosine of -1 in degrees (the help function may be handy). Use MATLAB to find the inverse tangent of x as x ranges from -1 to 1 in steps of 0.1. Use MATLAB to find the inverse tangent of x as x ranges from -1 to 1 in steps of 0.1.

16 Data 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()length()std()var()max()min()mean()median()sum()prod()sort()sortrows()

17 What is the largest number in vector x and where is it located? What is the median of the above vector? What is the sum of the above vector? Data analysis practice ans = 29 ans = 5 value = 10 highest value is 10 position = 4 located in the 4th column

18 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. Hands-on

19 Generation of random numbers rand(n) produces an n×n matrix of random numbers from 0 to 1. 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. 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:

20 Complex numbers Complex numbers take the form of a+b*i: Complex numbers take the form of a+b*i: –a is the real part –b is the imaginary part where i = sqrt(-1). Complex numbers can be assigned in MATLAB on command lines as follows: 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.

21 Complex numbers continued To find the real and imaginary components of a complex number: To find the real and imaginary components of a complex number:real(c)imag(c) To find the absolute value or modulus of a complex number: To find the absolute value or modulus of a complex number:abs(c) To find the angle or argument expressed in radians of a complex number: To find the angle or argument expressed in radians of a complex number:angle(c)

22 Other useful functions clock: produces an array that tells year, month, day, hour, min, sec. clock: produces an array that tells year, month, day, hour, min, sec. date: tells date date: tells date pi: the number pi (3.141592653589…..) pi: the number pi (3.141592653589…..) i: imaginary number % i = sqrt(-1) i: imaginary number % i = sqrt(-1) j: imaginary number % j = sqrt(-1) j: imaginary number % j = sqrt(-1) eps: smallest difference between two numerically-computed, adjacent real (‘floating point’) numbers = 2.2204e-016. eps: smallest difference between two numerically-computed, adjacent real (‘floating point’) numbers = 2.2204e-016.

23 Exercises Find the modulus (magnitude, abs) and angle (argument) of the complex number 3+4i. Find the modulus (magnitude, abs) and angle (argument) of the complex number 3+4i. Generate a 4x4 array of random numbers between 0 and 10. Sort each column of the array. Generate a 4x4 array of random numbers between 0 and 10. Sort each column of the array. Use MATLAB’s help function to find built-in functions to determine: Use MATLAB’s help function to find built-in functions to determine: –The base 10 logarithm of 5 –The secant of pi

24 Summary Help Feature Help Feature Basic Math Functions Basic Math Functions Rounding Functions Rounding Functions Discrete Mathematics Functions Discrete Mathematics Functions Trigonometric Functions Trigonometric Functions Data Analysis Data Analysis Random Numbers Random Numbers Complex Numbers Complex Numbers


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

Similar presentations


Ads by Google