Select to view content in your preferred language

Hiking equation in python returns wrong results

3044
6
Jump to solution
07-30-2014 08:32 AM
GrljAles
Deactivated User

Hi,

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

W=6e^{\displaystyle-3.5\left\vert\frac{dh}{dx}+0.05\right\vert}

\frac{dh}{dx}=S=\tan\Theta

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š

Tags (1)
1 Solution

Accepted Solutions
DouglasSands
Deactivated User

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?

View solution in original post

0 Kudos
6 Replies
DouglasSands
Deactivated User

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?

0 Kudos
GrljAles
Deactivated User

Hey Douglas,,

Thank you for advice, it solved my problem! I knew there had to be some simple solution but I'm new to this stuff.

About formatting the code:There is option for formatting in different styles when you are posting the question, however I don't see it here in the reply window.

When posting question it is under the quotation mark as in image:

Screenshot 2014-07-30 20.52.18.png

I tried copying from notepad++ via plugin nppExport but it doesn't work.

Thank you again!

Aleš

DouglasSands
Deactivated User

I'm happy to help. Thanks for pointing that out! I hadn't been able to figure it out till now.

0 Kudos
RhettZufelt
MVP Notable Contributor

You don't see the syntax option in the reply window, just the original post.  In a reply window, you need to click the "Use advanced editor" link in the upper right and then you will get the syntax highlight option.

R_

0 Kudos
ElleryChan
Occasional Contributor

The difficulty of finding the code formatter is being discussed in this thread:  https://community.esri.com/message/390783#390783

0 Kudos
GrljAles
Deactivated User

Hehe, thank you

Aleš

0 Kudos