phetsims / calculus-grapher

"Calculus Grapher" is an educational simulation in HTML5, by PhET Interactive Simulations.
GNU General Public License v3.0
4 stars 4 forks source link

Scaling of the second derivative is off: 🤦 #303

Closed veillette closed 1 year ago

veillette commented 1 year ago

While looking at #302, I noticed an odd relationship between the scaling of the second and first derivative.

Although numerical values are not emphasized, it should be important to get the values right!!

Calculus Grapher screenshot (54)

The slope of the derivative is the second derivative. Evaluating from the above screen, we notice that the second derivative has the correct sign, but it numerical value should be about -1.

The culprit is here.

   // Determine the second derivative using the naive assumption that all original points are smooth. We will handle exceptions later
        this.points[ index ].y = ( point.getSlope( nextPoint ) - point.getSlope( previousPoint ) ) / ( 2 * this.deltaX );

whereas it should be

   // Determine the second derivative using the naive assumption that all original points are smooth. We will handle exceptions later
        this.points[ index ].y = ( point.getSlope( nextPoint ) - point.getSlope( previousPoint ) ) / ( this.deltaX );
veillette commented 1 year ago

For posterity, here is the formula we want for the second derivative based on the central difference $$f''(x) \approx \frac{ \frac{f(x+h) - f(x)}{h} - \frac{f(x) - f(x-h)}{h} }{h} = \frac{f(x+h) - 2 f(x) + f(x-h)}{h^{2}}$$.

veillette commented 1 year ago

Fixed on master. I included a screenshot of the parabola and triangle function, and we can see that the calculation of the first and second derivative is correct. Calculus Grapher screenshot (55)

pixelzoom commented 1 year ago

Good catch.

veillette commented 1 year ago

I should note that this bug was introduced while I introduced the SecondDerivative class that calculates the SecondDerivative based on the function f(x) (previously the second derivative was merely the derivative of the first derivative).

We can close this issue as fixed.