Search code examples
cmathexponentialinverse

Inverse-square law equation


I have a light sensor which reads light intensity from 0 to 4095. I struggling to to write an equation using the inverse-square law of light so that when the light is lowest (let's say lowest ambient light is 50), it returns 1, and if highest (4095), it returns 26, but crucially the increments from 1 to 26 require the light to increase exponentially i.e.:

123 4 5  6   7      8         9              10                          ...27
light intensity ->

Any suggestions of an equation? I can't seem to figure it out. Language is C.


Solution

  • You are wanting to divide your 4095 to 50 interval into 25 equal segments (26-1). That would mean the width of each "intensity segment" is:

    (4095-50)/25 = 161.8
    

    So if variable x is ranging 1 to 26, your equation for distance would be:

    D = sqrt( 1 / (4095 - (x * 161.8)) )
    

    This is from taking Intensity_value = 1/D^2 as the proportion (I say "intensity value" since this doesn't include the proportion constant if it were a real intensity - we're dealing with arbitrary units for this problem).

    In other words, if you plotted x on a line and each x value was a distance D from the origin, you'd get the result you are showing for 1 through 26. I am assuming, from your diagram, that the intensity is DECREASING as you go to the right.

    You should be able to generalize this for different ranges of intensities and different ranges of corresponding x.