## 4. Matlab sofware

#### 4.0 Elementary functions 4.1 Linear algebra 4.2 Nonlinear functions 4.3 Ordinary differential equations 4.4 Fourier transformations

Easily accessed mathematical software is Matlab's raison d'ete. This page only provides a small glimpse into its capabilities. It is safe to say, that if you need to perform a calculation that is in a numerical analysis textbook, Matlab provides a subroutine to perform that calculation.

### 4.0 Elementary functions

Matlab provides a host of elementary mathematical functions. To see the functions provided, type help elfun. Matlab evaluates the elementary functions component-wise.

Computing the exponential function

`>> x = 0:10;  exp(-x)`

### 4.1 Linear algebra

Matlab provides easily computed matrix functions. These include the measures of the matrix size, standard decompositions of the matrix, LU, QR and SVD, the eigenvalues and eigenvectors. To see the functions provided type help matfun.

Compute the eigenvalues of a symmetric matrix.

```>> A = rand(4); A = A*A',  eig(A)

A =

1.5788    0.6565    1.2657    1.0939
0.6565    0.5826    0.4885    0.7305
1.2657    0.4885    1.0277    0.8001
1.0939    0.7305    0.8001    1.2804

ans =

0.0008
0.1217
0.5381
3.8089```
Compute the eigenvectors and eigenvalues.
`>> A = rand(4); A = A*A', [V,D] = eig(A)`

Compute the LU decomposition of a matrix

```>> A = [4 1; 1 4], [L U] = lu(A)

A =

4     1
1     4

L =

1.0000         0
0.2500    1.0000

U =

4.0000    1.0000
0    3.7500
```

### 4.2 Nonlinear functions

Type help funfun, or help fsolve

### 4.3 Ordinary differential equations

Type odedemo, or try the following demonstration that solves the Lotka-Voltera equation from mathematical biology. The equations are

```      u' =  u - 0.01 u v
v' = -v + 0.02 u v```
To see this code for this function type,
```>> type lotka
```
And to solve the ODE, type
```>> [t,y] = ode23('lotka',[0,15],[20,20]);
```
To see the trajectories of the solution type
```>> plot(t,y)
```
And to see the phase plane type
```>> plot(y(:,1),y(:,2))
```

### 4.4 Fourier Transforms

Matlab provides fast fourier transformations based on complex exponential functions (fft, fft2 fftn), sine functions (dst, dst2) and cosine functions (dct, dct2). The inverse functions are preceeded by the letter i, for example ifft. Advance applications of FFT's are also available. These include the Hilbert transforms, filters, and spectrum analysis among others.

Compute the FFT and inverse FFT of a vector x.

```>> x = rand(8,1)
>> x_hat = fft(x)
>> x2 = ifft(x_hat)
```

David Eyre
9/8/1998