lettura simple

Creating Logarithmic Plots in MATLAB

Let me tell you about using logarithmic plots in Matlab.

What is a logarithmic plot? It's a chart in which values on one or both axes are represented on a logarithmic scale. Logarithmic plots are particularly useful for displaying data that varies exponentially.
an example of a logarithmic plot

So, let's start with a simple example that will help you grasp the concept with ease

We'll create an array 'x' filled with integer values from 1 to 10.

>> x=0:1:10;

Now, we'll create another array, 'y', which contains the values of the function y = x2

>> y=x.^2

Both arrays contain 10 values.

However, the 'x' array increases linearly (1, 2, 3, 4, ...), while the 'y' array increases exponentially (1, 4, 9, 16, ...).

Now, let's plot this function on a Cartesian chart by typing plot(x, y).

>> plot(x,y)

The result? A beautiful curve showing the exponential growth of y=x2.

the chart on the Cartesian plane

But wait, there's more! We're going to transform this plot into a logarithmic scale on the y-axis. Just type semilogy(x, y), and watch the magic happen.

>> semilogy(x,y)

In this new plot, the y-axis is logarithmic, and the distance between 100, 101, and 102 becomes constant.

the logarithmic scale chart

To make things even clearer, let's add some grid lines by typing grid on.

>> grid on

There you go! The logarithmic scale is now much easier to read.

adding grid lines to the logarithmic scale

But what if we want a logarithmic scale on the x-axis too? No problem! Just type semilogx(x, y)

>> semilogx(x,y)

Now we have a chart with the x-axis on a logarithmic scale.

the chart with the x-axis on a logarithmic scale

But wait, we can go even further. Let's put both axes on a logarithmic scale by typing loglog(x, y)

>> loglog(x,y)

And there you have it – a chart with both axes on a logarithmic scale.

the chart on a logarithmic scale

So, to wrap things up, the functions semilogx(), semilogy(), and loglog() allow you to create any logarithmic plot in Matlab.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin