Mathematical Functions: Polytechnic Institute of Tabaco
Mathematical Functions: Polytechnic Institute of Tabaco
Mathematical Functions: Polytechnic Institute of Tabaco
The Pioneer in Computer Education and Engineering Education in the First District of Albay
Mathematical functions
MATLAB offers many predefined mathematical functions for technical computing which contains a
large set of mathematical functions.
Typing help elfun and help specfun calls up full lists of elementary and special
functions respectively.
There is a long list of mathematical functions that are built into MATLAB. These functions are
called built-ins. Many standard mathematical functions, such as sin(x), cos(x), tan(x), ex, ln(x), are
evaluated by the functions sin, cos, tan, exp, and log respectively in MATLAB.
Table 2.1 lists some commonly used functions, where variables x and y can be numbers, vectors,
or matrices.
In addition to the elementary functions, MATLAB includes a number of predefined constant values. A list
of the most common values is given in Table 2.2.
Not a number
Examples
We illustrate here some typical examples which related to the elementary functions previously defined.
√
As a first example, the value of the expression y = e−a sin(x) + 10 y, for a = 5, x = 2, and
y = 8 is computed by
>> a = 5; x = 2; y = 8;
>> y = exp(-a)*sin(x)+10*sqrt(y) y
=
28.2904
>> log(142)
ans =
4.9558
>> log10(142)
ans = 2.1523
Note the difference between the natural logarithm log(x) and the decimal logarithm (base
10) log10(x).
To calculate sin(π/4) and e10, we enter the following commands in MATLAB,
>> sin(pi/4)
ans =
0.7071
>> exp(10)
ans =
2.2026e+004
NOTES:
• Only use built-in functions on the right hand side of an expression. Reassigning the value to a
built-in function can create problems.
√
• are some exceptions. Forexample, i and j are pre-assigned to 1. However,
There − one or
both of i or j are often used as loop indices.
• Toavoid any possible confusion, it is suggested to use instead ii or jj as loop indices.
Basic plotting
Overview
MATLAB has an excellent set of graphic tools. Plotting a given data set or the results of computation
is possible with very few commands. You are highly encouraged to plot mathematical functions and
results of analysis as often as possible. Trying to understand mathematical equations with graphics is
an enjoyable and very efficient way of learning mathematics. Being able to plot mathematical
functions and data freely is the most important step, and this section is written to assist you to do
just that.
Creating simple plots
The basic MATLAB graphing procedure, for example in 2D, is to take a vector of x- coordinates, x =
(x1, . . . , xN ), and a vector of y-coordinates, y = (y1, . . . , yN ), locate the points (xi, yi), with i = 1, 2, .
. . , n and then join them by straight lines. You need to prepare x and y in an identical array form;
namely, x and y are both row arrays or column arrays of the same length.
TheMATLAB command toplotagraphisplot(x,y). Thevectorsx= (1, 2, 3, 4, 5, 6) and y = (3,
−1, 2, 4, 5, 1) produce the picture shown in Figure 2.1.
>> x = [1 2 3 4 5 6];
>> y = [3 -1 2 4 5 1];
>> plot(x,y)
NOTE: The plot functions has different forms depending on the input arguments. If y is a vector
plot(y)produces a piecewise linear graph of the elements of y versus the index of the elements of y.
If we specify two vectors, as mentioned above, plot(x,y) produces a graph of y versus x.
For example, to plot the function sin (x) on the interval [0, 2π], we first create a vector of x values
ranging from 0 to 2π, then compute the sine of these values, and finally plot the result:
>> x = 0:pi/100:2*pi;
>> y = sin(x);
>> plot(x,y)
NOTES:
The color of a single curve is, by default, blue, but other colors are possible. The desired color is
indicated by a third argument. For example, red is selected by plot(x,y,’r’). Note the single quotes,
’ ’, around r.
>> x = 0:pi/100:2*pi;
>> y1 = 2*cos(x);
>> y2 = cos(x);
>> y3 = 0.5*cos(x);
>> plot(x,y1,’--’,x,y2,’-’,x,y3,’:’)
>> xlabel(’0 \leq x \leq 2\pi’)
>> ylabel(’Cosine functions’)
>> legend(’2*cos(x)’,’cos(x)’,’0.5*cos(x)’)
>> title(’Typical example of multiple plots’)
>> axis([0 2*pi -3 3])
The result of multiple data sets in one graph plot is shown in Figure 2.3.
Typical example of multiple plots
1
Cosine functions
−1
−2
−3
0 1 2 3 4 5 6
0 x 2
By default, MATLAB uses line style and color to distinguish the data sets plotted in the graph.
However, you can change the appearance of these graphic components or add annotations to the
graph to help explain your data for presentation.
plot(x,y,’style_color_marker’)