MATLAB Examples

# space125: Almost equally spaced points in logarithmic space

When plotting some function, it is often desirable to cover the function domain uniformly. In this short document, somewhat different way to create almost equally-spaced values for semilogx, semilogy, or loglog plots is described.

## Linspace and logspace

When creating a plot with both axes in linear scale, we can use the colon operator : or the linspace command.

```x = -1:0.5:3 x = linspace(-1,3,9) ```
```x = -1.0000 -0.5000 0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 x = -1.0000 -0.5000 0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 ```

In the first case we have to set the spacing between the values, in the second case the number of values we want to generate.

The logspace command behaves similarly to the linspace. It generates points equally spaced (precisely) on the logarithmic scale.

```x = logspace(-1,3,9) logx = log10(x) ```
```x = 1.0e+003 * 0.0001 0.0003 0.0010 0.0032 0.0100 0.0316 0.1000 0.3162 1.0000 logx = -1.0000 -0.5000 0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 ```

## Integer values in logarithmic scale

In experiment design, it is pretty common that we know in advance that some function will have logarithmic, power, or exponential behavior, yet it can be measured only in integer points.

This is often the case when we want to measure the behavior of the time demands of some function in relation to the number of points, N, and their dimensionality, D, that form the input to that function.

Let's say we want to measure the time demands of an algorithm A and we want to study how the demands change when changing the dimensionality of the space from 1 to 1000. Say, we want to measure them in 10 points with the boundaries included. If we use the logspace function we would get something like this:

```Dlogspace = logspace(log10(1),log10(1000),10) ```
```Dlogspace = 1.0e+003 * 0.0010 0.0022 0.0046 0.0100 0.0215 0.0464 0.1000 0.2154 0.4642 1.0000 ```

These values are generally not integers. OK, we can round them.

```Drounded = round( logspace(log10(1),log10(1000),10) ) ```
```Drounded = 1 2 5 10 22 46 100 215 464 1000 ```

These values look quite nice. They are ALMOST equally spaced in the log space and are all integers. But they are not easy to remember.

## space125

Finally, we can use the function space125. It generates vectors that contain values (1,2,5)*10^n, between some boundary points:

```D125 = space125(1,1000) ```
```D125 = 1 2 5 10 20 50 100 200 500 1000 ```
```D = space125(1,789) ```
```D = 1 2 5 10 20 50 100 200 500 ```
```D = space125(0.0235, 5.7) ```
```D = 0.0500 0.1000 0.2000 0.5000 1.0000 2.0000 5.0000 ```

## Comparison of logspace, rounded logspace, and space125

Let's compare the generated values graphically:

```semilogx(Dlogspace, ones(1,10), 'k+', 'Linewidth', 1.5); hold on; semilogx(Drounded, 2*ones(1,10), 'b+', 'Linewidth', 1.5); semilogx(D125, 3*ones(1,10), 'r+', 'Linewidth', 1.5); set(gca,'YTickMode','manual'); set(gca,'YTick', [1 2 3]); set(gca,'YTickLabel',{'logspace','round(logspace)','space125'}); ax = axis; ax(3:4) = [0.5 3.5]; axis(ax); for i = 1:length(Dlogspace), plot([Dlogspace(i) Dlogspace(i)], [0 4], 'k--'); end title('Placement of the points on logarithmic scale'); xlabel('Logarithmic scale'); ```

## Pros and cons of space125

The function space125 generates 3 integer easy-to-remember points in each decade of the scale. You cannot change it (that's the minus), but very often you needn't change it (that's the plus). The values 1, 2, and 5 are based on my personal experience and are sufficient for the majority of my needs.

If the domain should be covered with higher density, I suggest using the round( logspace(log10(xmin),log10(xmax),nPoints) ) command.

Of course, if there is no need for integer values, use the logspace command.

Petr Pošík