Programming elements that are used to perform basic mathematical calculations include:

- Arrays
- Matrices
- Equations or Simple functions
- Math functions

See my previous post about the first two here. This post will deal with the latter two.

### Equations or Simple functions

The simplest problem to be solved via programming would be the solution to a single equation, or a ‘simple function’. For example, an exponential transform of the data would require the time points and parameter as input, and give a set of Y’s as output. Other examples of simple function processes would be shifting a dataset by 10 seconds, or plotting a set of data.

These simple functions take the data set as an input, and produce multiple outputs corresponding to different subsets of the data. The best way to handle these simple functions is with a loop. If you want to plot each subset of data, you are looking for *i *plots, each containing *j* data points. Then:

for n = 1:i # number of plots for r = 1:j # number of points per plot plot(dataset(n,r)) # plotting function end export (figure) # save figure after all points plotted end

### Math functions

Functions which do math can vary greatly in complexity. One such function could be a linear model such as

where are parameters. Or you may have a system of equations as your model, or perhaps a stochastic differential equation (SDE) or partial differential equation (PDE). In these examples, your data will most likely be informing the response *y* or the independent variables , and you will have to estimate the parameters.

In that case, you will need to define a function which accepts the data and the parameter set, and returns a set of estimates. Inside this function you will need an optimization routine. The optimization routine will use some formula to represent the goodness of fit, and look to minimize the formula to obtain the best fit. Often the best fit is heavily limited by the choice of initial parameters, unless you are using a global optimization technique. The best fit estimate may also be limited by the number of iterations or function calls it is allowed to have.