Hot answers tagged

10 votes

How to solve a second order differential equation (diffusion) with boundary conditions using Python

I have found that I must keep the value of dt near dx or the results become unstable This behavior you have noticed is known as the Courant–Friedrichs–Lewy (CFL) condition. Indeed, there are ...
user avatar
8 votes
Accepted

Python: Underflow vs. exp of large negative numbers

If your final result is of the order of magnitude of exp(-1000) $\approx 5 \cdot 10^{-435}$, then you are out of luck; no matter how you compute it, it will always ...
user avatar
7 votes

Why Is This Python Code Faster Than Fortran Wrapper with F2PY?

Shoutout to Kyle Mandli and Endulum who each contributed to this answer in the comments. First, I took Endulum's suggestion and removed the redundant reshapes. ...
user avatar
7 votes
Accepted

Why is my curve_fit not producing the covariance matrix and the correct values for the unknown variables?

The problem seems to be one of scaling. When I added the jacobian of the function an overflow warning appeared. Thus, I divided the data by their maximum values and it worked. Following is the code. <...
user avatar
  • 8,110
7 votes

Going to try to move some of my scipy/numpy calculation to a new GPU, how to avoid disappointing results?

I buy the wrong CUDA GPU and the speed up is minimal or nonexistent. It is highly unlikely that your choice of GPU will have a significant impact on your speed-up unless your model is very big. To a ...
user avatar
  • 3,131
6 votes
Accepted

Calculate determinant of unitary matrices based on SVD implementation

If you are prepared to go digging around in the fortran code: The SVD algorithm consists of a few parts: Bidiagonalization (usually using Householder reflectors) Use QR shifts to reduce the ...
user avatar
  • 1,189
6 votes

Is there any way/any python function to calculate the condition number of the roots of a polynomial directly?

The condition number of a root $r$ of a polynomial $p$ is $$ \kappa := \frac{\left\| p \right\|}{|rp'(r)|} $$ There is some arbitrariness in the choice of norm which affects the definition of the ...
user avatar
  • 2,021
6 votes
Accepted

Is there something unique you can get out of a set of numbers?

One simple way to do this is with a product of powers of primes. Let the hash function be $f(x_{1},x_{2},\ldots,x_{n})=2^{x_{1}}3^{x_{2}}\cdot p_{n}^{x_{n}}$ where $p_{n}$ is the $n$th prime number. ...
user avatar
5 votes

Why do problems arise in FFT for smaller value of df in Python?

The discrete Fourier transform for a signal of period $T$ with $N$ samples reads in its inverse or reconstruction form as $$ y(t)=\frac1{N}\sum_{k=-N/2}^{N/2}c_k e^{i2\pi k\frac{t}{T}} $$ with ...
user avatar
  • 3,651
5 votes
Accepted

How to solve the integral-like energy equation with Sagdeev potential numerically in Python?

A simple approach here would be to use the shooting method, by integrating from $\xi$=0 to infinity (some large value) the ODE written as $ \frac{d}{d\xi} \phi = \sqrt{-2 S(\phi)} $ Due to the ...
user avatar
5 votes
Accepted

A method for finding the number of eigenvectors with a given, known eigenvalue

If you know $\lambda$, then the eigenvalue problem $Ax=\lambda x$ comes down to finding a vector $x$ so that $(A-\lambda I)x=0$. In other words, you want to characterize the null space of the matrix $...
user avatar
5 votes
Accepted

Solving and Plotting Mutualism Model in Python

Looking at the documentation at scipy, they recommend to use scipy.integrate.solve_ivp rather than scipy.integrate.odeint as can ...
user avatar
  • 88
5 votes

Calculate determinant of unitary matrices based on SVD implementation

For part 1: To my knowledge, the answer is no. For part 2: This question had several good answers, all of which were negative (there isn't really a faster way). I don't believe there is meaningful new ...
user avatar
5 votes

solve_ivp from scipy does not integrate the whole range of tspan

You can examine the sol object to see why the integration failed. It provides the message 'Required step size is less than spacing between numbers.' This usually ...
user avatar
5 votes
Accepted

Stochastic SIR using SDEint python package

This model is implemented using Julia's DifferentialEquations.jl in this tutorial. Here's a version of that code: ...
user avatar
4 votes

Is there a Python version of the ODE tool pplane?

I do not really know how much this can help you, but maybe you can use this code for rough sketches of the dynamics of planar vector fields. I know that it does not have the functionality that you may ...
user avatar
4 votes

How can I color my Mandelbrot set like this?

To get the filaments drawn in such contrast, a (inverse?) distance estimator is used. This is based on the derivative $dz_n/dc$, see, e.g., http://mrob.com/pub/muency/distanceestimator.html for ...
user avatar
  • 3,651
4 votes

How to solve the integral-like energy equation with Sagdeev potential numerically in Python?

Quite hastily, I admit, I wrote the code below and generated the solutions. I did not bother to center them. One issue you have with your code is that you are trying to integrate $$\frac{d\phi}{d\xi} \...
user avatar
4 votes

Implementation of $[X, \cdot]$ as an $n^2 \times n^2$ matrix, where $X$ is an $n \times n$ matrix

Instead of using 4 levels of nested loops, you can take advantage of Kronecker products to simply your commutator_matrix function to ...
user avatar
4 votes

Storing large amounts of interpolation data

Since you are on a uniform $x-y-z$ grid, you are in luck that others have had similar issues before. Specifically, I would suggest that you take a look at HDF5 for a low-level way of storing this kind ...
user avatar
4 votes
Accepted

How to deal with solving coupled ODE systems where variables are updated multiple times within each timestep?

It would have been helpful to see the ODE you're actually trying to solve, but the way I interpret what you write is that the right hand side of your ODE system consists of a number of terms ...
user avatar
4 votes
Accepted

Boundary value problem with singularity and boundary condition at infinity

The left boundary is really singular, and the desired right boundary behavior is unstable. \begin{align} f''(r)+\frac{f'(r)}{r}-\frac{f(r)}{r^2}&=-f(r)+2\lambda f(r)^3 \\ r^2f''(r)+rf'(r)-f(r)&...
user avatar
  • 3,651
4 votes
Accepted

2nd order differential equation coupled to integro-differential equation in python

The next transformation step would see the system reformulated as a boundary value problem with the additional equations and conditions replacing the integral $$ I'(r)=12πf(r)ϕ(r)r^4,~~I(0)=0,~~ I(\...
user avatar
  • 3,651
4 votes
Accepted

Easy way to perform solver over pandas dataframe

The problem is that y gets values close to zero so the solver steps over the axis and tries negative values during the derivative computation via difference ...
user avatar
  • 3,651
4 votes

How to ensure the numeric value is always positive in Optimization Python?

Consider a variable transform. Assuming you care about the individual entries of $x$ and not something like its determinant, you can transform the $x_i$ such that the new variable $y_i$ is not bounded ...
user avatar
4 votes

Fitting gauss-hermite-parametrization to data?

You're not providing an initial guess for the parameters, and so optimize.curve_fit is defaulting to [1.,1.,1.,1.]. The solver ...
user avatar
3 votes
Accepted

Logistic growth curve using scipy is not quite right

To repeat the answers you got at your cross-post https://stackoverflow.com/questions/69292456/logistic-growth-curve-using-scipy-is-not-quite-right The logistic curve is rather rigid in its symmetry. ...
user avatar
  • 3,651
3 votes
Accepted

Implementation of $[X, \cdot]$ as an $n^2 \times n^2$ matrix, where $X$ is an $n \times n$ matrix

output[i*n + j][k*n + l] = com[k][l] That's your mistake I think -- reversed indices. To compute the matrix $M$ associated to a linear operator $f$ (the way it's ...
user avatar
3 votes
Accepted

Single hexahedral element stiffness matrix problem, help me find the mistake

Your intuition is right, the stiffness matrix should be singular. Actually, as pointed by @BillGreene, it has an eigenvalue that is zero. Ordering the nodes as follows (-1, -1, -1,), (1, -1, -1), (1, ...
user avatar
  • 8,110
3 votes
Accepted

Question about energy in the shallow water equations on a staggered grid

I'm going to work on the assumption that the disturbances aren't large enough to create shock waves, which are a whole other can of worms. Your energy functional is close but not quite right -- the ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible