0% found this document useful (0 votes)
20 views

Lecture 06

The document discusses MATLAB built-in functions including basics of functions, help feature, elementary math functions like polynomials and trigonometric functions, data analysis, random numbers, and complex numbers.

Uploaded by

Alejoldm
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lecture 06

The document discusses MATLAB built-in functions including basics of functions, help feature, elementary math functions like polynomials and trigonometric functions, data analysis, random numbers, and complex numbers.

Uploaded by

Alejoldm
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Lecture 6

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

2016 Daniel Valentine. All rights reserved. Published by


Elsevier.
What is a built-in function?
A computational expression that uses one or
more input values to produce an output value.

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
Functions take the form:
variable = function(number or variable)
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
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.
If you type help in the command window
MATLAB provides a list of help topics.
Help
In MATLAB command window type

If we are interested in the elementary


mathematics functions, we find it on the
list of help topics (16th down) and click it.
A list of commands appears with the
description of what each one does.
Try a few!
MATLAB Help
More Help
For more specific help: help topic
Check it out:

MATLAB describes what the function tan does


and provides helpful links to similar or related
functions to assist you.
Exercise
Use MATLAB help to find the exponential,
natural logarithm, and logarithm base 2
functions.
Calculate e7
Calculate ln(4)
Calculate log2(12)
Help Navigator
Click on Help (question mark, ?) icon on
the toolbar at the top of MATLAB desktop.
A HELP window will pop up in the center
of the desktop.
You can Search for specific help topics by
typing your topic in the Search
Documentation space.
You can also click on any topic listed in
the Help window.
Help Navigator window
Elementary Mathematical Functions
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.
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
Sometimes it is necessary to round numbers. MATLAB
provides several utilities to do this.

What do they do?


Try the exercises above with y in place of x.
Discrete-mathematics functions

What do these functions do?


If you are not sure, use the help feature.
Trigonometric functions
MATLAB can compute numerous trigonometric
functions, for angles in radians.

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.


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.
Exercise
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 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.
MATLAB has many statistical functions:

max() sum() size()


min() prod() length()
mean() sort() std()
median( sortrows() var()
)
Data analysis practice

What is the largest number in vector x and


where is it located?
value = 10 highest value is 10
position = 4 located in the 4th column

What is the median of the above vector?


ans = 5
What is the sum of the above vector?
ans = 29
Exercise

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 3rd column.


Generation of random numbers
rand(n) produces an nn matrix of
random numbers from 0 to 1.
rand(n,m) produces an nm 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
Complex numbers take the form of a+b*i:
a is the real part
b is the imaginary part
where i = sqrt(-1).
Complexnumbers 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
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:
abs(c)
To find the angle or argument expressed in
radians of a complex number:
angle(c)
Other useful functions
clock: produces an array that tells year,
month, day, hour, min, sec.
date: tells date
pi: the number pi (3.141592653589..)
i: imaginary number % i = sqrt(-1)
j: imaginary number % j = sqrt(-1)
eps: smallest difference between two
numerically-computed, adjacent real
(floating point) numbers = 2.2204e-
016.
Exercises
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.
Use MATLABs help function to find built-in
functions to determine:
The base 10 logarithm of 5
The secant of pi
Summary
Help Feature
Basic Math Functions
Rounding Functions
Discrete Mathematics Functions
Trigonometric Functions
Data Analysis
Random Numbers
Complex Numbers

You might also like