DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #267 - Braking Speed

Braking distance d1 is the distance a vehicle will go from the point when it brakes to when it comes to a complete stop. It depends on the original speed v and on the coefficient of friction mu between the tires and the road surface.

Braking distance is just one of two principal components of the total stopping distance. The other component is the reaction distance, which is the product of the speed and the perception-reaction time of the driver. We can just assume that the reaction time is constant and equal to 1 second.

The kinetic energy E can be found using the formula 0.5*m*v**2, the work W given by braking is mu*m*g*d1. Finding E and W gives the braking distance: d1 = v*v / 2*mu*g where g is the gravity of Earth and m the vehicle's mass.

So, to complete this challenge, there are two tasks you need to complete.

  1. Calculate the total stopping distance in meters given v, mu (and the reaction time t = 1).
    dist(v, mu) -> d = total stopping distance

  2. Calculate v in km per hour knowing d in meters and mu. The reaction time is still t = 1.
    speed(d, mu) -> v such that dist(v, mu) = d

Examples

  1. dist(100, 0.7) -> 83.9598760937531

  2. speed(83.9598760937531, 0.7) -> 100.0

Tests

dist(144, 0.3)
dist(92, 0.5)

speed(159, 0.8)
speed(153, 0.7)

Good luck!


This challenge comes from g964 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (3)

Collapse
 
cipharius profile image
Valts Liepiņš

First part of the challenge is almost written out as is, but the second part requires solving quadratic equation.

Solution in Haskell:

dist :: Double -> Double -> Double
dist vKmh mu = d_reaction + d_braking
  where
    d_reaction = v*t_reaction
    d_braking = (v*v) / (2*mu*g)
    v = fromKmh vKmh

speed :: Double -> Double -> Double
speed d mu = toKmh v
  where
    -- Solve using quadratic formula
    v = (-b + (sqrt $ b*b - 4*a*c)) / (2*a)
    a = 1 / (2*mu*g)
    b = 1 / t_reaction
    c = -d

fromKmh :: Double -> Double
fromKmh v = v * 1000 / 3600

toKmh :: Double -> Double
toKmh v = v * 3600 / 1000

g :: Double
g = 9.81

t_reaction :: Double
t_reaction = 1

Tests:

> dist 144 0.3
311.83146449201496

> dist 92 0.5
92.12909477605366

> speed 159 0.8
153.79671564846308

> speed 153 0.7
142.14404997566152
Collapse
 
dry profile image
Hayden Mankin

Is there something about this problem I'm missing? This seems like it's just a math problem, is there some trick I'm missing that makes it faster? Or not lose precision? Or am I missing an edge case some how?

anyways, here's a javascript solution.

const g = 9.81; // gravity in m/s
const k = 3600 / 1000; // conversion coefficient: m/s to km/hr

const dist = (v, mu, t=1) => {
  v = v / k; 
  return v * t + v * v / (mu * 2 * g); 
}

const speed = (d, mu, t=1) => {
  return k * (Math.sqrt(g * mu * (2 * d + g * t * t * mu)) - g * t * mu);
}

console.log(dist(144, 0.3)) // 311.83146449201496
console.log(dist(92, 0.5)) // 92.12909477605365
console.log(speed(159, 0.8)) // 153.79671564846308
console.log(speed(153, 0.7)) // 142.14404997566152