Читать книгу Applied Numerical Methods Using MATLAB - Won Y. Yang - Страница 20

1.1.6 Mathematical Functions

Оглавление

Mathematical functions and special reserved constants/variables defined in MATLAB are listed in Table 1.3.

Table 1.3 Functions and variables inside MATLAB.

Function Remark Function Remark
cos(x) exp(x) Exponential function
sin(x) log(x)| Natural logarithm
tan(x) log10(x) Common logarithm
acos(x) cos−1(x) abs(x) Absolute value
asin(x) sin−1(x) angle(x) Phase of a complex number (rad)
atan(x) π/2 ≤ tan−1(x) ≤ π/2 sqrt(x) Square root
atan2(y,x) π ≤ tan−1(y/x) ≤ π real(x) Real part
cosh(x) (ex + e−x)/2 imag(x) Imaginary part
sinh(x) (exe−x)/2 conj(x) Complex conjugate
tanh(x) (exe−x)/(ex + e−x) round(x) The nearest integer (round‐off)
acosh(x) cosh−1(x) fix(x) The nearest integer toward 0
asinh(x) sinh−1(x) floor(x) The greatest integer ≤x
atanh(x) tanh−1(x) ceil(x) The smallest integer ≥x
max Maximum and its index sign(x) 1 (positive)/0/−1 (negative)
min Minimum and its index mod(y,x) Remainder of y/x
sum Sum rem(y,x) Remainder of y/x
prod Product eval(f) Evaluate an expression
norm Norm feval(f,a) Function evaluation
sort Sort in the ascending order polyval Value of a polynomial function
clock Present time poly Polynomial with given roots
find Index of element(s) satisfying given condition roots Roots of polynomial
flops(0) Reset the flops count to zero tic Start a stopwatch timer
flops Cumulative # of floating point operations (no longer available) toc Read the stopwatch timer (elapsed time from tic)
date Present date magic Magic square
Reserved variables with special meaning
i, j pi π
eps Machine epsilon Inf, inf Largest number (∞)
realmax, realmin Largest/smallest positive number NaN Not_a_Number (undetermined)
end The end of for‐loop or if, while, case statement or an array index break Exit while/for loop
nargin # of input arguments nargout # of output arguments
varargin Variable input argument list varargout Variable output argument list

MATLAB also allows us to define our own function and store it in a file named after the function name so that it can be used as if it were a built‐in function. For instance, we can define a scalar‐valued function:


and a vector‐valued function


as follows:

function y=f1(x) y=1./(1+8*x.̂2); function y=f49(x) y(1)= x(1)̂2+4*x(2)̂2 -5; y(2)=2*x(1)̂2-2*x(1)-3*x(2) -2.5;

Once we store these functions as M‐files, each named “f1.m” and “f49.m” after the function names, respectively, we can call and use them as needed inside another M‐file or in the MATLAB Command window.

>f1([0 1]) % several values of a scalar function of a scalar variable ans= 1.0000 0.1111 >f49([0 1]) % a value of a 2D vector function of a vector variable ans= -1.0000 -5.5000 >feval('f1',[0 1]), feval('f49',[0 1]) % equivalently, yields the same ans= 1.0000 0.1111 ans= -1.0000 -5.5000

1 (Q7) With the function f1(x) defined as a scalar function of a scalar variable, we enter a vector as its input argument to obtain a seemingly vector‐valued output. What's going on?

2 (A7) It is just a set of function values [f1(x1) f1(x2) …] obtained at a time for several values [x1 x2 …] of x. In expectation of one‐shot multioperation, it is a good practice to put a dot ( .) just before the arithmetic operators *(multiplication), /(division), and ̂(power) in the function definition so that the element‐by‐element (element‐wise or term‐wise) operation can be done any time.

Note that we can define a simple function not only in an independent M‐file but also inside a script by using the function handle operator @, the ‘ inline()’ command, or just in a form of literal expression that can be evaluated by the command ‘ eval()’.

>f1h=@(x)1./(1+8*x.̂2); % Using function handle >f1i=inline('1./(1+8*x.̂2)','x'); % Usng inline() >f1h([0 1]), feval(f1h,[0 1]), f1i([0 1]), feval(f1i,[0 1]) ans= 1.0000 0.1111 ans= 1.0000 0.1111 >f1='1./(1+8*x.̂2)'; x=[0 1]; eval(f1) ans= 1.0000 0.1111

As far as a polynomial function is concerned, it can simply be defined as its coefficient vector arranged in descending order. It may be called to yield its value for certain value(s) of its independent variable by using the command ‘ polyval()’.

>p=[1 0 –3 2]; % Polynomial function p(x) = x3-3x + 2 >polyval(p,[0 1]) % Polynomial function values at x=0 and 1 ans= 2.0000 0.0000

The multiplication of two polynomials can be performed by taking the convolution of their coefficient vectors representing the polynomials in MATLAB, since


where


This operation can be done by using the MATLAB built‐in command ‘ conv()’ as illustrated beneath:

>a=[1 –1]; b=[1 1 1]; c=conv(a,b) c= 1 0 0 -1 % meaning that (x-1)(x2 + x + 1) = x3 + 0 ⋅ x2 + 0 ⋅ x-1

But, in case you want to multiply a polynomial by only xn, you can simply append n zeros to the right end of the polynomial coefficient vector to extend its dimension.

>a=[1 2 3]; c=[a 0 0] % equivalently, c=conv(a,[1 0 0]) c= 1 2 3 0 0 % meaning that (x2 + 2x + 3)x2 = x4 + 2x3 + 3x2 + 0 ⋅ x + 0

Applied Numerical Methods Using MATLAB

Подняться наверх