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

Numeric integration returns wrong result for N[ integrate(1/x,{x,0,1}) ] #1064

Open
tranleduy2000 opened this issue Sep 13, 2024 · 4 comments

Comments

@tranleduy2000
Copy link
Sponsor Collaborator

tranleduy2000 commented Sep 13, 2024

There is the input, the integral of 1/x from 0 to 1 is a divergent integral:

N[ integrate(1/x,{x,0,1}) ] 

Error result:

𝟣𝟢.𝟥𝟩𝟦𝟩𝟨
image

Expected result:

Indeterminate
axkr added a commit that referenced this issue Sep 13, 2024
- idiv - Integral of `1` does not converge on `2`.
- JUnit test for this issue `testXReciprocalIssue1064()`
- `NIntegrate` with `LegendreGauss` method was called as default, other
`NIntegrate` methods don't return a result
@axkr
Copy link
Owner

axkr commented Sep 13, 2024

Integrate function switches to NIntegrate function in numeric mode:

For the NIntegrate function the LegendreGauss method is the default numeric method for the calculation.

See these JUnit tests:

Maybe we should use another default method for NIntegrate?
Any suggestions?

@tranleduy2000
Copy link
Sponsor Collaborator Author

Each numerical integration method has its own strengths and weaknesses. I think the LegendreGauss method is effective in general cases.

@tranleduy2000
Copy link
Sponsor Collaborator Author

@axkr I suggest to use the Romberg as the default method, since LegendreGauss fails in many common cases:

This algorithm divides the integration interval into equally-sized sub-interval and on each of them performs a Legendre-Gauss quadrature. Because of its non-adaptive nature, this algorithm can converge to a wrong value for the integral (for example, if the function is significantly different from zero toward the ends of the integration interval). In particular, a change of variables aimed at estimating integrals over infinite intervals as proposed here should be avoided when using this class.

https://commons.apache.org/proper/commons-math/javadocs/api-3.6.1/org/apache/commons/math3/analysis/integration/IterativeLegendreGaussIntegrator.html

@tranleduy2000
Copy link
Sponsor Collaborator Author

tranleduy2000 commented Sep 14, 2024

I have some suggestions for determining numerical integral methods:

  • If the expression has Abs function, use the LegendreGauss method

    Example input: Integrate[Abs(x^2-2x), {x, -10, 10}] // N
    Result should be 669.3282335875249

  • If the expression contains a variable that occurs in the exponent of Power function, use the GaussKronrod method
    Example input: x^x, 3^(2x), E^(-Sin(t))

  • Otherwise, use the Romberg method, since it is the optimized version of trapezoid and Simpson methods

axkr added a commit that referenced this issue Sep 14, 2024
- If the expression has `Abs` function, use the `LegendreGauss` method

  Example input: `Integrate[Abs(x^2-2x), {x, -10, 10}] // N`
  Result should be `669.3282335875249`

- If the expression contains a variable that occurs in the exponent of
`Power` function, use the `GaussKronrod` method
  Example input: `x^x`, `3^(2x)`, `E^(-Sin(t))`

- Otherwise, use the `Romberg` method, since it is the optimized version
of trapezoid and Simpson methods
axkr added a commit that referenced this issue Sep 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants