Search code examples
c++imagemathpixelcurve

Sigmoid Curve not working with formula C++


I'm using Microsoft Visual Studio 2010.

The formula is y = 1/(1+exp(-e))

Over the range of values where bih.biWidth is the range to iterate.

Yet when i try implementing in the codes it does not work, why? Can any experts please guide me along, thank you.

for(int y=0; y<bih.biHeight; y++) 
{ 
   for(int x=0; x<bih.biWidth; x++) 
   {   
      SetPixel(hdc, (double)1/((double)1+exp(double(-x)))*bih.biWidth, 
               bih.biHeight-x, red); 
   } 
} 

The lines start from nearly at the bottom right hand of the image, and ends it at the end with a slight curve on the top right hand of the image. Why is that so?


Solution

  • Because 0 is the center of the sigmoid curve. Your x starts at 0; if you want your curve to be plotted symmetrically, you need to compute an argument that is symmetric about 0:

    for(int x=0; x<bih.biWidth; x++)
    {
        double a= x - 0.5*bih.biWidth;
        SetPixel(hdc, bih.biWidth-x, 1.0/(1.0+exp(-a)) * bih.biHeight, red);
    }
    

    Scaling a by a constant factor will adjust the slope of the sigmoid function.

    (I also suspect that your original code has switched the scaling factors used in the SetPixel() arguments, so I have fixed that. It doesn't make sense to subtract x from bih.biHeight when it ranges from 0 to bih.biWidth instead...)

    [additional edit: I have additionally switched the arguments, so that biWidth and biHeight are in the x- and y-coordinates, respectively. This is the conventional way to plot functions, anyway -- so if you had wanted to flip the plot, you will need to switch it back]