# The work-energy theorem with a variable force in JavaScript

The work-energy theorem says the net work done on a body equals its change in kinetic energy: W = ΔKE = ½mv² − ½mu². With a force that changes along the path, work is the area under the F–x graph, which a short loop can compute. (I build [PractiSci](https://www.practisci.in), where the theorem is a free virtual lab.)

## How do I find work done by a variable force?

Integrate F(x) over the path. Take F = 10 + 5x newtons acting on a 2 kg body that starts at rest, from x = 0 to 4 m.

```js
const F = (x) => 10 + 5 * x;
let W = 0; const dx = 1e-4;
for (let x = 0; x < 4; x += dx) W += F(x + dx / 2) * dx;
console.log(W.toFixed(2));                       // 80.00 J
console.log(Math.sqrt((2 * W) / 2).toFixed(2));  // 8.94 m/s
```

The exact area is 10·4 + ½·5·4² = 80 J, and v = √(2W/m) = 8.94 m/s.

## How far does a braking car travel?

A 1000 kg car at 20 m/s is stopped by a constant 5000 N force. Work removes all its kinetic energy: F·d = ½mv².

```js
console.log(((0.5 * 1000 * 20 ** 2) / 5000).toFixed(1)); // 40.0 m
```

## FAQ

### Does the theorem work with friction?
Yes, as long as W is the net work from all forces, friction included.

### Is work a vector?
No. It is a scalar, positive when the force has a component along the motion.

### Where can I try it?
The free [work-energy theorem lab](https://www.practisci.in/labs/11-5-work-energy-theorem) lets you change the force and mass. The [work, energy and power guide](https://www.practisci.in/topics/work-energy-power) has the theory.
