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

MATLAB Function

Uploaded by

aceabel.aa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

MATLAB Function

Uploaded by

aceabel.aa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Built-In MATLAB®

Functions
Khairul anwar
Department of Chemical Engineering and Energy Sustainability, UNIMAS
Using built in function
• A big advantage of MATLAB® is that function arguments can generally be either scalars
or matrices.
• In our example, if x is a scalar, a scalar result is returned
x = 9;
b = sqrt(x)
b=
3
• the square-root function, sqrt, can also accept matrices as input. In
• this case, the square root of each element is calculated, so
• x = [4, 9, 16];
• b = sqrt(x)
• returns
• b=
• 234 2
• Some functions require multiple inputs. For example, the remainder function, rem,
requires two inputs: a dividend and a divisor. We represent this as rem(x,y), so
rem(10,3)
• calculates the remainder of 10 divided by 3:
ans =
1
• The size function is an example of a function that returns two outputs, which are
stored in a single array. It determines the number of rows and columns in a matrix.
Thus,
d = [1, 2, 3; 4, 5, 6];
f = size(d)
• returns the 1 * 2 result matrix
f=
2 3

3
• You can also assign variable names to each of the answers by representing the left-hand side of
the assignment statement as a matrix. For example,
[rows,cols] = size(d)
• gives
rows =
2
cols =
3
• finds the square root of the sine of whatever values are stored in the matrix named x. If x is
assigned a value of 2,
x = 2;
• You can create more complicated expressions by nesting functions. For instance,
g = sqrt(sin(x))
• the result is
g=
0.9536

4
Help Feature
• To use the command-line help function, type help in the command window:
help
• A list of help topics will appear:
HELP topics:
MATLAB\general – General-purpose commands
MATLAB\ops – Operators and special characters
MATLAB\lang – Programming language constructs
MATLAB\elmat – Elementary matrices and matrix manipulation
MATLAB\elfun – Elementary math functions
MATLAB\specfun – Specialized math functions
• and so on

5
• For example, to get help on the tangent function, type
help tan
• The following should be displayed:
tan Tangent of argument in radians.
tan(X) is the tangent of the elements of X.
See also atan, atan2, tand, atan2d.

6
• To use the windowed help screen, select Help  Documentation
from the toolstrip.
• To access this version of the help utility directly from the command
window, type doc <topic>. Thus, to access the windowed help for
tangent, type
doc tan

7
Exercise

8
Elementary Math Functions
The functions listed in Table 3.1 accept either a scalar or a matrix of x values.

9
Example

10
11
12
Exercise

Use script file to answer all the questions.

13
Exercise

Use the script file to answer the question.

14
Rounding Functions
• MATLAB® contains functions for a number of different rounding
techniques (see Table 3.2).
• For example, suppose you want to buy apples at the grocery store. The
apples cost $0.52 apiece. You have $5.00. How many apples can you
buy? Mathematically,

15
• But clearly, you can’t buy part of an apple, and the grocery store won’t
let you round to the nearest number of apples. Instead, you need to
round down.
• The MATLAB® function to accomplish this is fix. Thus,
fix(5/0.52)
• returns the maximum number of apples you can buy:
ans =
9

16
Rounding Functions

17
Exercise

18
Discrete mathematics
• Discrete mathematics is the mathematics of whole numbers. MATLAB® includes
functions to factor numbers, find common denominators and multiples, calculate
factorials, and explore prime numbers
• To calculate a factorial in MATLAB® use the factorial function. Thus,
factorial(5)
ans =
120
• gives the same result as
5*4*3*2*1
ans =
120
• The value of a factorial quickly becomes very large. 10 factorial is 3,628,800. MATLAB®
can handle up to 170! Anything larger gives Inf for an answer, because the maximum
value for a real number that can be expressed in MATLAB® is exceeded.
19
Trigonometric functions
• The MATLAB® code to perform these conversions is
degrees = radians * 180/pi;
radians = degrees * pi/180;
• MATLAB® also includes functions to convert from degrees to radians (deg2rad) or
radians to degrees (rad2deg). For example, to convert pi radians to degrees, use
rad2deg(pi)
ans =
180
• Similarly to convert 90 degrees to radians use
deg2rad(90)
ans =
1.5708

20
• MATLAB® also includes a set of trigonometric functions that accept
the angle in degrees so that you need not do the conversion to
radians. These include sind, cosd, and tand.

21
E x am p le

22
23
Solution

24
25
Data Analysis Functions
• Analyzing data
statistically in
MATLAB® is
particularly easy, partly
because whole data sets
can be represented by a
single matrix, and partly
because of the large
number of built-in data
analysis functions.

26
• All of the functions in this section work on the columns in two-dimensional
matrices. MATLAB® is column dominant—in other words, if there is a
choice to make, MATLAB® will choose columns first over rows.
• For example, if you want to find the maximum value in each row of the
matrix

• use the command


max(x')
• which returns
ans=
5 6

27
Exercise

28
Mean and median

29
Exercise

30
Sums and
products
 to add up (sum) all of the elements
in a matrix, or to multiply all of the
elements together.

31
• This is useful when dealing with the sequences of numbers in a series. Consider
the harmonic series

• We could use MATLAB® to create a sequence representing the first five values in
the sequence as follows
k = 1:5;
• equence = 1./k
• which gives us
sequence =
1.0000 0.5000 0.3333 0.2500 0.2000

32
• We could view the series as a sequence of fractions by changing the
format to rational with the following code
format rat
sequence =
1 1/2 1/3 1/4 1/5
• Now we could use the cumsum function to find the value of the
entire series for values of n from 1 to 5
format short
series = cumsum(sequence)
series =
1.0000 1.5000 1.8333 2.0833 2.2833

33
Sorting values

34
• These functions are particularly useful in analyzing data. Consider
the results of the Men’s 2014 Olympic 1000 meter speed skating
event shown in Table 3.9.

• The skaters were given a random number for this illustration, but
once the race is over we’d like to sort the table in ascending order,
based on the times in the second column.

35
• To sort in descending order, place a minus sign in front of the column
number used for sorting. Thus, sortrows(skating_results, −2) sorts the
array in descending order, based on the second column. The result of
this command is
ans =
1 68.89
3 68.86
2 68.74
5 68.43
4 68.39

36
Determining matrix size

• MATLAB® offers three functions (see Table 3.10) that allow us to


determine how big a matrix is: size, length, and numel. The size
function returns the number of rows and columns in a matrix. The
length function returns the larger of the matrix dimensions. The
numel function returns the total number of elements in a matrix

37
Exercise

38
Variance and Standard Deviation
• The standard deviation and variance are measures of how much
elements in a data set vary with respect to each other.

39
Random numbers
• Random numbers are often used in engineering calculations to simulate measured data.
Measured 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.
• Uniform random numbers are generated with the rand function. These numbers are evenly
distributed between 0 and 1.
• For 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 100 * 1 matrix of values. Now we just need to multiply by 5 to expand
the range to 0 to 5:
r = r * 5;
• If we want to change the range to 5 to 10, we can add 5 to every value in the array:
r = r + 5;

40
• Gaussian random-number sets are described by specifying their
average (mean) 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 randn function. For example,

41
Complex numbers
• MATLAB® includes several functions used primarily with complex numbers. Complex numbers consist of two
parts: a real component and an imaginary component.
• 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 A = 5 + 3*j
• or with the complex function, as in
A = complex (5,3)
• which returns
A=
5.0000 + 3.0000i.
• As is standard in MATLAB®, the input to the complex function can be either two scalars or two arrays of
values. Thus, 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 42
• When the absolute-value function is used with a complex number, it
calculates the radius, using the Pythagorean theorem:
abs(A)
ans =
5.8310

43
• Similarly, the angle is found with the angle function:
angle(A)
ans =
0.5404
• The result is expressed in radians. Both functions, abs and angle, will accept scalars or arrays as
input. Recall that B is a 1 * 3 array of complex numbers:
B=
5.0000 + 3.0000i 6.0000 + 3.0000i 15.0000 + 9.0000i
• The abs function returns the radius if the number is represented in polar coordinates:
abs(B)
ans =
5.8310 6.7082 17.4929
• The angle from the horizontal can be found with the angle function:
angle(B)
ans =
0.5404 0.4636 0.5404

44
Special values and miscellaneous functions

45
Exercise

46

You might also like