Hi,

I've been trying to implement Tobler's hiking equation in script. The equation goes:

Where:

W is walking speed,

S is slope in degrees.

The list of slopes is:

k7=[-1.0312126698879267, 1.1457628381751035, -3.890124404061796, -0.6875278121450912, 0.0, -1.7820676357665195, -1.0532676344873988, 1.7010978323487986]

And my code is:

for f in k7: tob=6*math.expm1(-3.5*abs(f+0.05)) k8.append(tob)

Which should be right, but the results are:

k8=[-5.80650137556095, -5.908682283616555, -5.999991269395487, -5.355698099597122, -0.963257875384756, -5.986024888143845, -5.8208760494265395, -5.986925387994366]

First, they are negative and they shouldn't be, second, notice that the slopes are very similar and so should be the results but notice the fifth element of the results! When I tried this with math.exp I got something completly different and also totally wrong.

I know that this should be very simple but I cant figure it out.

Have I missed something in the equation or is there some trick that I should know about.

I would be really grateful for your help.

Aleš

You shouldn't be plugging in the slopes into the function. The equation says that it takes the tangent of the slope. Also, I would doubt that slope should be in degrees if it is going into a trig function. In any case, python uses radians so you'll need to convert to radians to use the tangent function:

6 * math.exp(-3.5 * abs(math.tan(math.radians(degrees)) + 0.05)))

For a sanity check, put in a values of the 5th value (0) and the 1st value:

def hike(degrees):

return 6 * math.exp(-3.5 * abs(math.tan(math.radians(degrees)) + 0.05))

hike(0)

5.036742124615245

hike(-1.0312126698879267)

5.3642655450021355

Both of which checks yield results around 5 km/h which is expected given the input of 0 and a near 0 slope.

Hopefully this helps!

Could you tell me - how is it you format code on the new forums?