Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaling of the second derivative is off: 🤦 #303

Closed
veillette opened this issue Mar 21, 2023 · 4 comments
Closed

Scaling of the second derivative is off: 🤦 #303

veillette opened this issue Mar 21, 2023 · 4 comments
Assignees
Labels

Comments

@veillette
Copy link
Contributor

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 veillette self-assigned this Mar 21, 2023
veillette added a commit that referenced this issue Mar 21, 2023
Signed-off-by: Martin Veillette <martin.veillette@gmail.com>
@veillette
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
Contributor

Good catch.

@veillette
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants