# ndBasis

Basis functions for tunable gain surface

## Syntax

``shapefcn = ndBasis(F1,F2)``
``shapefcn = ndBasis(F1,F2,...,FN)``

## Description

You use basis function expansions to parameterize gain surfaces for tuning gain-scheduled controllers, with the `tunableSurface` command. The complexity of such expansions grows quickly when you have multiple scheduling variables. Use `ndBasis` to build N-dimensional expansions from low-dimensional expansions. `ndBasis` is analogous to `ndgrid` in the way it spatially replicates the expansions along each dimension.

example

````shapefcn = ndBasis(F1,F2)` forms the outer (tensor) product of two basis function expansions. Each basis function expansion is a function that returns a vector of expansion terms, such as returned by `polyBasis`. If ${F}_{1}\left({x}_{1}\right)=\left[{F}_{1,1}\left({x}_{1}\right),{F}_{1,2}\left({x}_{1}\right),\dots ,{F}_{1,i}\left({x}_{1}\right)\right]$ and ${F}_{2}\left({x}_{2}\right)=\left[{F}_{2,1}\left({x}_{2}\right),{F}_{2,2}\left({x}_{2}\right),\dots ,{F}_{2,i}\left({x}_{2}\right)\right]$, then `shapefcn` is a vector of terms of the form:${F}_{ij}={F}_{1,i}\left({x}_{1}\right){F}_{2,j}\left({x}_{2}\right).$The terms are listed in a column-oriented fashion, with i varying first, then j.```
````shapefcn = ndBasis(F1,F2,...,FN)` forms the outer product of three or more basis function expansions. The terms in the vector returned by `shapefcn` are of the form:${F}_{{i}_{1}\dots {i}_{N}}={F}_{1,{i}_{i}}\left({x}_{1}\right){F}_{2,{i}_{2}}\left({x}_{2}\right)\dots {F}_{N,{i}_{N}}\left({x}_{N}\right).$These terms are listed in sort order that of an N-dimensional array, with i1 varying first, then i2, and so on. Each `Fj` can itself be a multi-dimensional basis function expansion.```

## Examples

collapse all

Create a two-dimensional basis of polynomial functions to second-order in both variables.

Define a one-dimensional set of basis functions.

`F = @(x)[x,x^2];`

Equivalently, you can use `polyBasis` to create F.

`F = polyBasis('canonical',2);`

Generate a two-dimensional expansion from `F`.

`F2D = ndBasis(F,F);`

`F2D` is a function of two variables. The function returns a vector containing the evaluated basis functions of those two variables:

`$F2D\left(x,y\right)=\left[x,{x}^{2},y,yx,y{x}^{2},{y}^{2},x{y}^{2},{x}^{2}{y}^{2}\right].$`

To confirm this, evaluate `F2D` for x = 0.2, y = -0.3.

`F2D(0.2,-0.3)`
```ans = 1×8 0.2000 0.0400 -0.3000 -0.0600 -0.0120 0.0900 0.0180 0.0036 ```

The expansion you combine with `ndBasis` need not have the same order. For instance, combine `F` with first-order expansion in one variable.

```G = @(y)[y]; F2D2 = ndBasis(F,G);```

The array returned by `F2D2` is similar to that returned by `F2D`, without the terms that are quadratic in the second variable.

`$F2D2\left(x,y\right)=\left[x,{x}^{2},y,yx,y{x}^{2}\right].$`

Evaluate `F2D2` for x = 0.2, y = -0.3 to confirm the order of terms.

`F2D2(0.2,-0.3)`
```ans = 1×5 0.2000 0.0400 -0.3000 -0.0600 -0.0120 ```

Create a set of two-dimensional basis functions where the expansion is quadratic in one variable and periodic in the other variable.

First generate the one-dimensional expansions. Name the variables for improved readability.

```F1 = polyBasis('canonical',2,'x'); F2 = fourierBasis(1,1,'y');```

For simplicity, this example takes only the first harmonic of the periodic variation. These expansions have basis functions given by:

`$F1\left(x\right)=\left[x,{x}^{2}\right],\phantom{\rule{1em}{0ex}}F2\left(y\right)=\left[\mathrm{cos}\left(\pi y\right),\mathrm{sin}\left(\pi y\right)\right].$`

Create the two-dimensional basis function expansion. Note that `ndBasis` preserves the variable names you assigned to one-dimensional expansions.

`F = ndBasis(F1,F2)`
```F = function_handle with value: @(x,y)utFcnBasisOuterProduct(FDATA_,x,y) ```

The array returned by `F` includes all multiplicative combinations of the basis functions:

`$F\left(x,y\right)=\left[x,{x}^{2},\mathrm{cos}\left(\pi y\right),\mathrm{cos}\left(\pi y\right)x,\mathrm{cos}\left(\pi y\right){x}^{2},\mathrm{sin}\left(\pi y\right),x\mathrm{sin}\left(\pi y\right),{x}^{2}\mathrm{sin}\left(\pi y\right)\right].$`

To confirm this, evaluate `F` for x = 0.2, y = -0.3.

`F(0.2,-0.3)`
```ans = 1×8 0.2000 0.0400 0.5878 0.1176 0.0235 -0.8090 -0.1618 -0.0324 ```

## Input Arguments

collapse all

Basis function expansion, specified as a function handle. The function must return a vector of basis functions of one or more scheduling variables. You can define these basis functions explicitly, or using `polyBasis` or `fourierBasis`.

Example: `F = @(x)[x,x^2,x^3]`

Example: `F = polyBasis(3,2)`

## Output Arguments

collapse all

Basis function expansion, specified as a function handle. `shapefcn` takes as input arguments the total number of variables in `F1,F2,...,FN`. It returns a vector of functions of those variables, defined on the interval [–1,1] for each input variable. When you use `shapefcn` to create a gain surface, `tunableSurface` automatically generates tunable coefficients for each term in the vector.

## Tips

• The `ndBasis` operation is associative:

`ndBasis(F1,ndBasis(F2,F3)) = ndBasis(ndBasis(F1,F2),F3) = ndBasis(F1,F2,F3)`

## Version History

Introduced in R2015b