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

Continuity error with the function mathieu_modcem1 #12267

Open
brumann opened this issue May 29, 2020 · 5 comments
Open

Continuity error with the function mathieu_modcem1 #12267

brumann opened this issue May 29, 2020 · 5 comments
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected Fortran Items related to the internal Fortran code base scipy.special

Comments

@brumann
Copy link

brumann commented May 29, 2020

When looking at the shape of the derivative of the modified Mathieu function of order 1, it appears that there is a continuity problem.It sees to be a sign problem, I thought it would come from a square root branch mistake, but I cannot find that in the Mathieu function defined in Abramowitz and stegun.

python_issue

Here are the parameters to use to obtain the graph given:

  • q vary from 5 to 30 (200 points)
  • m = 6
  • x = 0.856
  • function ussed sp.mathieu_modcem1(m, q, x)[1]

Thank you in advance for your response.

Sincerely,

@AtsushiSakai AtsushiSakai added scipy.special defect A clear bug or issue that prevents SciPy from being installed or used as expected labels May 29, 2020
@rlucas7
Copy link
Member

rlucas7 commented May 30, 2020 via email

@brumann
Copy link
Author

brumann commented May 30, 2020

Thank you for your answer
Here is the code to obtain the graph given above.

import matplotlib.pyplot as plt
import numpy as np
import scipy.special as sp

q = np.linspace(5,30,200)
m = 6
x = 0.856
f_x = sp.mathieu_modcem1(m, q, x)[1]
plt.plot(q,f_x)
plt.xlabel('q')
plt.title('Derivative $Ce_m(x,q)$')
plt.show()

In fact for my code I was just using the mathieu fonction inside a Newton's method and this one was finding an erroneous value.

Sincerely

@rlucas7
Copy link
Member

rlucas7 commented May 30, 2020

On my side I can confirm that I see the same thing in master w/python 3.6

>>> scipy.__version__
'1.6.0.dev0+f57e5bf'

unfortunately it isn't what I thought it was and looks like it goes into the fortran codes.

@rlucas7
Copy link
Member

rlucas7 commented May 30, 2020

Some notes that may prove helpful to those who speak fortran:

mathieu_modcem1 is a wrapper here:

"mathieu_modcem1": {

which leads to the source .c file:

int mcm1_wrap(double m, double q, double x, double *f1r, double *d1r)

and that calls the fortran:

F_FUNC(mtu12,MTU12)(&kf,&kc,&int_m, &q, &x, f1r, d1r, &f2r, &d2r);

and the fortran source starts here:

SUBROUTINE MTU12(KF,KC,M,Q,X,F1R,D1R,F2R,D2R)

not immediately clear to me why an q swapping from 25.1xxx to 25.2xxx would do this but I'm not familiar with fortran or with the function in particular.

>>> q[160]
25.100502512562812
>>> q[161]
25.22613065326633

@rlucas7 rlucas7 added the Fortran Items related to the internal Fortran code base label May 30, 2020
@ianthomas23
Copy link
Contributor

The explanation of this problem is that the algorithm requires finding the root of a function that has multiple roots, and within a certain parameter region the initial estimate for the location of the root is poor such that the secant method root finding converges to a different root.

The Fortran function MTU12 calls CVA2

SUBROUTINE CVA2(KD,M,Q,A)
C
C ======================================================
C Purpose: Calculate a specific characteristic value of
C Mathieu functions

with KD=1, M=6 and Q=25.0 works fine but Q=25.25 shows the problem. CVA2 calls CV0 to obtain the initial estimate for A
SUBROUTINE CV0(KD,M,Q,A0)
C
C =====================================================
C Purpose: Compute the initial characteristic value of
C Mathieu functions for m ≤ 12 or q ≤ 300 or
C q ≥ m*m

and then calls REFINE to improve it
SUBROUTINE REFINE(KD,M,Q,A)
C
C =====================================================
C Purpose: calculate the accurate characteristic value
C by the secant method

It is best illustrated by the following plot.
mathieu
The code is trying to find a root of the blue line, i.e. where it crosses the orange line, and the initial estimate is indicated by the green line. For Q=25.0 the initial estimate is just within the central region of the plot and the secant method successfully finds the nearest root at about A=49.0. For Q=25.25the initial estimate is not within the central region of the plot so the secant method locates the root to the left at about A=27 rather than the required root at about A=49.25.

According to the file header, all this Fortran code was taken from the book "Computation of Special Functions" by Zhang and Jin, 1996, Wiley and Sons. To proceed any further with this probably requires the assistance of someone who has a copy of the book.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected Fortran Items related to the internal Fortran code base scipy.special
Projects
None yet
Development

No branches or pull requests

4 participants