# Function Representation and Manipulation


```
%matplotlib inline
```


```
import numpy as np
import matplotlib
matplotlib.rcParams.update({'font.size': 14})
import matplotlib.pyplot as plt
```

From a mathematical point of view, a central point in numerical methods is how we represent a general function $f(x)$. As $x$ is a real number the function encodes an (uncountably) infinite amount of information: its value $f(x)$ for every point $x$. A computer can only usefully store and manipulate a finite amount of information, so it would appear impossible to represent even the simplest function of one variable.

Of course, most functions can be represented straightforwardly using little information. We often see functions that can be represented symbolically - $f(x) = x^2 + x$ is a simple example - but numerical methods are precisely for cases where symbolic methods are not applicable. Instead we must consider how a function is represented, or approximated, in terms of a finite amount of information.

Two standard representations can be used to make the central point. The first representation is in terms of polynomials, using Taylor series:

\begin{equation}
  f(x ; x_0) = \sum_{n=0}^{\infty} \frac{c_n}{n!} (x - x_0)^n.
\end{equation}

Here $x_0$ is a parameter about which the Taylor series is centred.

The second representation is in terms of trigonometric functions, using Fourier series. For simplicity we consider only the Fourier cosine series

\begin{equation}
  f(x ; L) = \frac{1}{2} a_0 + \sum_{n=1}^{\infty} a_n \cos \left( \frac{n \pi x}{L} \right).
\end{equation}

The Fourier series representation is $2L$-periodic.

Given a set of coefficients ($\{c_n\}$ for the Taylor series or $\{a_n\}$ for the Fourier series) we define a function.


```
an = np.zeros((10, 1))
cn = np.zeros_like(an)
for n in range(len(an)):
    an[n] = 1.0 / (n + 1)**2
    cn[n] = an[n]
x = np.linspace(-1.0, 1.0, 100)
taylor = an[0]*np.ones_like(x)
fourier = cn[0]*np.ones_like(x)
for n in range(1, len(an)):
    taylor += an[n] * x**n
    fourier += cn[n] * np.cos(n * np.pi * x)
fig = plt.figure(figsize = (8,6))
ax = fig.add_subplot(111)
ax.plot(x,taylor,label = 'Taylor')
ax.plot(x,fourier, label = 'Fourier')
plt.legend()
ax.set_xlabel('$x$')
plt.show()
```

Given a function, the standard analytical methods will give you the coefficients. This can rarely be directly applied in numerical methods - as noted above, it is usual that you're dealing with a function that isn't known in symbolic form.

Above, when we defined the function $f(x)$ from the coefficients, we were noting that we could evaluate the representation for any point $x$ given 

1. the form of the representation (e.g., as a Taylor series about $x_0=0$), and
2. the value of the coefficients.

We can (nearly always) go in the opposite direction: given the value of the function at enough points $x_n$ we can compute the coefficients of the representation.

For example, let us assume that 

1. the function will be represented as a polynomial of order 2, i.e. a Taylor series about $x_0=0$ such that all coefficients $c_n$ vanish for $n > 2$, and
2. the value of the function is known at three points $x_1, x_2, x_3$.

This is precisely the information needed to compute the coefficients $c_0, c_1, c_2$ and hence write the function as

\begin{equation}
  f(x) \approx c_0 + c_1 x + \frac{c_2}{2} x^2.
\end{equation}

Explicitly, we note that the function representation matches the known value of the function at the three points only if

\begin{equation}
  \begin{pmatrix} 1 & x_1 & \tfrac{1}{2} x_1^2 \\ 1 & x_2 & \tfrac{1}{2} x_2^2 \\ 1 & x_3 & \tfrac{1}{2} x_3^2 \end{pmatrix} \begin{pmatrix} c_0 \\ c_1 \\ c_2 \end{pmatrix} = \begin{pmatrix} f(x_1) \\ f(x_2) \\ f(x_3) \end{pmatrix}
\end{equation}

which, given the information we know, is a linear system for the coefficients.

Note in particular that the number of points at which the function is known must match the number of coefficients in the function representation.

The previous example hints at one of the standard methods of numerically representing functions. First, we take a representation that we know, given an infinite amount of information, converges to the function we want (in some sense). Second, assume that taking a *finite* number of terms is sufficient: e.g., truncate the sum in the Taylor series expansion after a finite number of terms. Then, using a finite amount of information about the function (such at its value at an appropriate number of points) we compute the coefficients of the representation. We then manipulate the representation *as if* it were the true function.


```

```

86e908a6ef7593ac55c818a6431eb890449e4a6d
ipynb
Jupyter Notebook
Lectures/Function Representation and Manipulation.ipynb
alistairwalsh/NumericalMethods
fa10f9dfc4512ea3a8b54287be82f9511858bd22
MIT
2021-12-01T09:15:04.000Z
2021-12-01T09:15:04.000Z
Lectures/Function Representation and Manipulation.ipynb
indranilsinharoy/NumericalMethods
989e0205565131057c9807ed9d55b6c1a5a38d42
MIT
Lectures/Function Representation and Manipulation.ipynb
indranilsinharoy/NumericalMethods
989e0205565131057c9807ed9d55b6c1a5a38d42
MIT
2021-04-13T02:58:54.000Z
2021-04-13T02:58:54.000Z
Qwen/Qwen-72B
1. YES
2. YES