1% MBDyn (C) is a multibody analysis code.
2% http://www.mbdyn.org
3%
4% Copyright (C) 1996-2017
5%
6% Pierangelo Masarati  <masarati@aero.polimi.it>
7%
8% Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
9% via La Masa, 34 - 20156 Milano, Italy
10% http://www.aero.polimi.it
11%
12% Changing this copyright notice is forbidden.
13%
14% This program is free software; you can redistribute it and/or modify
15% it under the terms of the GNU General Public License as published by
16% the Free Software Foundation (version 2 of the License).
17%
18%
19% This program is distributed in the hope that it will be useful,
20% but WITHOUT ANY WARRANTY; without even the implied warranty of
21% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22% GNU General Public License for more details.
23%
24% You should have received a copy of the GNU General Public License
25% along with this program; if not, write to the Free Software
26% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
28\section{Scalar functions}\label{sec:ScalarFunction}
29A \hty{ScalarFunction} object computes the value of a function.
30Almost every scalar function is of type \htmlref{\ty{DifferentiableScalarFunction}}{sec:ScalarFunction},
31derived from \hty{ScalarFunction}, and allows to compute the derivatives of
32the function as well. Currently implemented scalar functions are
33\begin{itemize}
34\item \kw{const}: $f(x)=c$
35\item \kw{exp}: $f(x)=m\cdot b^{c\cdot{x}}$
36\item \kw{log}: $f(x)=m\cdot\textrm{log}_b(c\cdot{x})$
37\item \kw{pow}: $f(x)=x^p$
38\item \kw{linear}: linear interpolation between the two points $(x_1,y_1)$
39and $(x_2,y_2)$
40\item \kw{cubicspline}: cubic natural spline interpolation between the
41set of points $\{(x_i,y_i), i\in[1,k\geq3]\}$
42\item \kw{multilinear}: multilinear interpolation between the
43set of points $\{(x_i,y_i), i\in[1,k\geq2]\}$
44\item \kw{chebychev}: Chebychev interpolation between the
45set of points $\{a,b\}$
46\item \kw{sum}: $f(x)=f_1(x) + f_2(x)$
47\item \kw{sub}: $f(x)=f_1(x) - f_2(x)$
48\item \kw{mul}: $f(x)=f_1(x) \cdot f_2(x)$
49\item \kw{div}: $f(x)=f_1(x) / f_2(x)$
50\end{itemize}
51
52\noindent
53Every \hty{ScalarFunction} card follows the format
54%\begin{verbatim}
55\begin{Verbatim}[commandchars=\\\{\}]
56    \bnt{card} :: = \kw{scalar function} : " \bnt{unique_scalar_func_name} " ,
57        \bnt{scalar_func_type} ,
58        \bnt{scalar_func_args}
59\end{Verbatim}
60%\end{verbatim}
61The name of the scalar function is a string, thus it needs to be enclosed
62in double quotes.
63
64The type of scalar function,
65\nt{scalar\_func\_type}, together
66with relevant arguments, \nt{scalar\_func\_args},
67are as follows:
68\subsubsection{Const Scalar Function}
69%\begin{verbatim}
70\begin{Verbatim}[commandchars=\\\{\}]
71    \bnt{scalar_func_type} ::= \kw{const}
72
73    \bnt{scalar_func_args} ::= \bnt{const_coef}
74\end{Verbatim}
75%\end{verbatim}
76Note: if the \nt{scalar\_func\_type} is omitted,
77a \kw{const} scalar function is assumed.
78
79\paragraph{Example.} \
80\begin{verbatim}
81    scalar function: "const_function", const, 1.e-2;
82\end{verbatim}
83
84\subsubsection{Exp Scalar Function}
85%\begin{verbatim}
86\begin{Verbatim}[commandchars=\\\{\}]
87    \bnt{scalar_func_type} ::= \kw{exp}
88
89    \bnt{scalar_func_args} ::=
90        [ \kw{base} , \bnt{base} , ]
91        [ \kw{coefficient} , \bnt{coef} , ]
92        \bnt{multiplier_coef}
93\end{Verbatim}
94%\end{verbatim}
95\begin{align}
96	f\plbr{x}
97	&=
98	\nt{multiplier\_coef} \cdot \nt{base}^{\plbr{\nt{coef} \cdot x}}
99\end{align}
100Note: the optional \nt{base} must be positive
101(defaults to `$\textrm{e}$', Neper's number);
102\nt{coef} defaults to 1.
103
104\paragraph{Example.} \
105\begin{verbatim}
106    scalar function: "exp_function", exp, 1.e-2; # 1.e-2*e^x
107\end{verbatim}
108
109\subsubsection{Log Scalar Function}
110%\begin{verbatim}
111\begin{Verbatim}[commandchars=\\\{\}]
112    \bnt{scalar_func_type} ::= \kw{log}
113
114    \bnt{scalar_func_args} ::=
115        [ \kw{base} , \bnt{base} , ]
116        [ \kw{coefficient} , \bnt{coef} , ]
117        \bnt{multiplier_coef}
118\end{Verbatim}
119%\end{verbatim}
120\begin{align}
121	f\plbr{x}
122	&=
123	\nt{multiplier\_coef} \cdot \log_{\nt{base}}\plbr{\nt{coef} \cdot x}
124\end{align}
125Note: the optional \kw{base} (defaults to `$\textrm{e}$', Neper's number,
126resulting in natural logarithms)
127must be positive.
128The optional value \nt{coef}, prepended by the keyword \kw{coefficient},
129(defaults to 1).
130The argument must be positive, otherwise an exception is thrown.
131
132\paragraph{Example.} \
133\begin{verbatim}
134    scalar function: "log_function", log, 1.e-2; # 1.e-2*log(x)
135\end{verbatim}
136
137\subsubsection{Pow Scalar Function}
138%\begin{verbatim}
139\begin{Verbatim}[commandchars=\\\{\}]
140    \bnt{scalar_func_type} ::= \kw{pow}
141
142    \bnt{scalar_func_args} ::= \bnt{exponent_coef}
143\end{Verbatim}
144%\end{verbatim}
145\begin{align}
146	f\plbr{x}
147	&=
148	x^{\nt{exponent\_coef}}
149\end{align}
150
151\paragraph{Example.} \
152\begin{verbatim}
153    scalar function: "pow_function", pow, 3.; # x^3.
154\end{verbatim}
155
156\subsubsection{Linear Scalar Function}
157%\begin{verbatim}
158\begin{Verbatim}[commandchars=\\\{\}]
159    \bnt{scalar_func_type} ::= \kw{linear}
160
161    \bnt{scalar_func_args} ::= \bnt{point} , \bnt{point}
162
163    \bnt{point}            ::= \bnt{x}, \bnt{y}
164\end{Verbatim}
165%\end{verbatim}
166A line passing through the two points,
167\begin{align}
168	f\plbr{x}
169	&=
170	\nt{y}_i
171	+
172	\frac{\nt{y}_f - \nt{y}_i}{\nt{x}_f - \nt{x}_i} \plbr{x - \nt{x}_i}
173\end{align}
174where $\nt{x}_i$, $\nt{y}_i$ are the coordinates of the first point,
175while $\nt{x}_f$, $\nt{y}_f$ are the coordinates of the second point.
176
177\paragraph{Example.} \
178\begin{verbatim}
179    scalar function: "linear_function", linear, 0., 0., 1., 1.;
180\end{verbatim}
181
182\subsubsection{Cubic Natural Spline Scalar Function}
183%\begin{verbatim}
184\begin{Verbatim}[commandchars=\\\{\}]
185    \bnt{scalar_func_type} ::= \kw{cubic spline}
186
187    \bnt{scalar_func_args} ::= [ \kw{do not extrapolate} , ]
188        \bnt{point} ,
189        \bnt{point}
190        [ , ... ]
191        [ , \kw{end} ]
192
193    \bnt{point}            ::= \bnt{x} , \bnt{y}
194\end{Verbatim}
195%\end{verbatim}
196The \kw{end} delimiter is required if the card needs to continue
197(i.e.\ the \hty{ScalarFunction} definition is embedded in a more complex
198statement); it can be omitted if the card ends with a semicolon
199right after the last point.
200
201\subsubsection{Multilinear Scalar Function}
202%\begin{verbatim}
203\begin{Verbatim}[commandchars=\\\{\}]
204    \bnt{scalar_func_type} ::= \kw{multilinear}
205
206    \bnt{scalar_func_args} ::= [ \kw{do not extrapolate} , ]
207        \bnt{point} ,
208        \bnt{point}
209        [ , ... ]
210        [ , \kw{end} ]
211
212    \bnt{point}            ::= \bnt{x} , \bnt{y}
213\end{Verbatim}
214%\end{verbatim}
215Unless \kw{do not extrapolate} is set, when the input is outside
216the provided values of \nt{x} the value is extrapolated using the slope
217of the nearest point pair.
218
219\subsubsection{Chebychev Scalar Function}
220%\begin{verbatim}
221\begin{Verbatim}[commandchars=\\\{\}]
222    \bnt{scalar_func_type} ::= \kw{chebychev}
223
224    \bnt{scalar_func_args} ::=
225        \bnt{lower_bound} , \bnt{upper_bound} ,
226        [ \kw{do not extrapolate} , ]
227        \bnt{coef_0} [ , \bnt{coef_1} [ , ... ] ]
228        [ , \kw{end} ]
229\end{Verbatim}
230%\end{verbatim}
231Chebychev polynomials of the first kind are defined as
232\begin{equation}
233	T_n\plbr{\xi} = \cos\plbr{n \cos^{-1}\plbr{\xi}} ,
234\end{equation}
235with
236\begin{equation}
237	\xi = 2 \frac{x}{b - a} - \frac{b + a}{b - a} ,
238\end{equation}
239where $a = \nt{lower\_bound}$ and $b = \nt{upper\_bound}$,
240which corresponds to the series
241\begin{align}
242	T_0\plbr{\xi} &= 1 \\
243	T_1\plbr{\xi} &= \xi \\
244	\ldots \nonumber \\
245	T_n\plbr{\xi} &= 2 \xi T_{n-1}\plbr{\xi} - T_{n-2}\plbr{\xi} .
246\end{align}
247For example, the first five coefficients are
248\begin{align}
249	T_0\plbr{\xi} &= 1 \\
250	T_1\plbr{\xi} &= \xi \\
251	T_2\plbr{\xi} &= 2\xi^2 - 1 \\
252	T_3\plbr{\xi} &= 4\xi^3 - 3\xi \\
253	T_4\plbr{\xi} &= 8\xi^4 - 8\xi^2 + 1
254\end{align}
255This scalar function implements the truncated series in the form
256\begin{equation}
257	f\plbr{x} = \sum_{k=0,n} c_k T_k\plbr{\xi} ,
258\end{equation}
259where $c_k = \nt{coef\_}\texttt{<k>}$.
260The first derivative of the series is obtained by considering
261\begin{align}
262	\frac{\mathrm{d}}{\mathrm{d}\xi}T_0\plbr{\xi} &= 0 \\
263	\frac{\mathrm{d}}{\mathrm{d}\xi}T_1\plbr{\xi} &= 1 \\
264	\ldots \nonumber \\
265	\frac{\mathrm{d}}{\mathrm{d}\xi}T_n\plbr{\xi} &=
266		2 T_{n-1}\plbr{\xi}
267		+ 2 \xi \frac{\mathrm{d}}{\mathrm{d}\xi}T_{n-1}\plbr{\xi}
268		- \frac{\mathrm{d}}{\mathrm{d}\xi}T_{n-2}\plbr{\xi} ,
269\end{align}
270so the first derivative of the scalar function is
271\begin{equation}
272	\frac{\mathrm{d}}{\mathrm{d}x}f\plbr{x} = \frac{\mathrm{d}\xi}{\mathrm{d}x} \sum_{k=1,n} c_k \frac{\mathrm{d}}{\mathrm{d}\xi} T_k\plbr{\xi} .
273\end{equation}
274Subsequent derivatives follow the rule
275\begin{equation}
276	\frac{\mathrm{d}^i}{\mathrm{d}\xi^i}T_n\plbr{\xi} =
277		2 i \frac{\mathrm{d}^{i-1}}{\mathrm{d}\xi^{i-1}}T_{n-1}\plbr{\xi}
278		+ 2 \xi \frac{\mathrm{d}^i}{\mathrm{d}\xi^i}T_{n-1}\plbr{\xi}
279		- \frac{\mathrm{d}^i}{\mathrm{d}\xi^i}T_{n-2}\plbr{\xi} .
280\end{equation}
281Differentiation of order higher than 1 is not currently implemented.
282
283
284
285
286\subsubsection{Sum Scalar Function}
287%\begin{verbatim}
288\begin{Verbatim}[commandchars=\\\{\}]
289    \bnt{scalar_func_type} ::= \kw{sum}
290
291    \bnt{scalar_func_args} ::= (\hty{ScalarFunction}) \bnt{f1} , (\hty{ScalarFunction}) \bnt{f2}
292\end{Verbatim}
293%\end{verbatim}
294\begin{align}
295	f\plbr{x}
296	&=
297	\nt{f1}\plbr{x} + \nt{f2}\plbr{x}
298\end{align}
299
300\paragraph{Example.} \
301\begin{verbatim}
302    scalar function: "first_function", const, 1.;
303    scalar function: "second_function", const, 2.;
304    scalar function: "sum_function", sum, "first_function", "second_function";
305\end{verbatim}
306
307
308\subsubsection{Sub Scalar Function}
309%\begin{verbatim}
310\begin{Verbatim}[commandchars=\\\{\}]
311    \bnt{scalar_func_type} ::= \kw{sub}
312
313    \bnt{scalar_func_args} ::= (\hty{ScalarFunction}) \bnt{f1} , (\hty{ScalarFunction}) \bnt{f2}
314\end{Verbatim}
315%\end{verbatim}
316\begin{align}
317	f\plbr{x}
318	&=
319	\nt{f1}\plbr{x} - \nt{f2}\plbr{x}
320\end{align}
321
322\paragraph{Example.} \
323\begin{verbatim}
324    scalar function: "first_function", const, 1.;
325    scalar function: "second_function", const, 2.;
326    scalar function: "sub_function", sub, "first_function", "second_function";
327\end{verbatim}
328
329\subsubsection{Mul Scalar Function}
330%\begin{verbatim}
331\begin{Verbatim}[commandchars=\\\{\}]
332    \bnt{scalar_func_type} ::= \kw{mul}
333
334    \bnt{scalar_func_args} ::= (\hty{ScalarFunction}) \bnt{f1} , (\hty{ScalarFunction}) \bnt{f2}
335\end{Verbatim}
336%\end{verbatim}
337\begin{align}
338	f\plbr{x}
339	&=
340	\nt{f1}\plbr{x} \cdot \nt{f2}\plbr{x}
341\end{align}
342
343\paragraph{Example.} \
344\begin{verbatim}
345    scalar function: "first_function", const, 1.;
346    scalar function: "second_function", const, 2.;
347    scalar function: "mul_function", mul, "first_function", "second_function";
348\end{verbatim}
349
350\subsubsection{Div Scalar Function}
351%\begin{verbatim}
352\begin{Verbatim}[commandchars=\\\{\}]
353    \bnt{scalar_func_type} ::= \kw{div}
354
355    \bnt{scalar_func_args} ::= (\hty{ScalarFunction}) \bnt{f1} , (\hty{ScalarFunction}) \bnt{f2}
356\end{Verbatim}
357%\end{verbatim}
358\begin{align}
359	f\plbr{x}
360	&=
361	\nt{f1}\plbr{x} / \nt{f2}\plbr{x}
362\end{align}
363Note: division by zero is checked, and an exception is thrown.
364
365\paragraph{Example.} \
366\begin{verbatim}
367    scalar function: "first_function", const, 1.;
368    scalar function: "second_function", const, 2.;
369    scalar function: "div_function", div, "first_function", "second_function";
370\end{verbatim}
371
372