Search code examples
matlabmatlab-figurelogarithm

How to plot x=0 in semilogx plot?


I need to plot a graph using semilogx(x,y). I have x=[0 1 2 ... 10 15 20 30 50 75 100]. The problem is that MATLAB does not plot x=0, which I understand because log(0)=undef. So is there another method in MATLAB to spread my points? Because using linear scale squeezes all first points in 1/10th of the graph's width!


Solution

  • Usually, what is done in cases like this is adding 1 to all x, so the first value (originally 0) appears at the origin, and also the back-transformation is the same for all values. You can add any other small values than 1, and get a similar result. However, you don't want to add a value that is too small (like eps) because then you get a huge distance from the next value, that will cause all other values to pack on the right side of the graph.

    Let's look at an example:

    x = [0 logspace(0,2,5)];
    % x =  0    1    3.1623    10    31.623    100
    y = 2.*(x+1); % add 1 to all x
    semilogx(x+1,y,'o','markerfacecolor','b') 
    

    small value

    While if you replace 0 with eps you get:

    x = [0 logspace(0,2,5)];
    y = 2.*(x+eps); % add a tiny value too all x
    semilogx(x+eps,y,'o','markerfacecolor','b')
    

    eps