# Questions tagged [python]

A general purpose high-level programming language that emphasizes ease of code syntax and readability.

737 questions
Filter by
Sorted by
Tagged with
72 views

### Errors in Integral Estimate of Gaussian using Trapezoidal Rule

I'm trying to estimate the percentage error in computing the integral of a Gaussian via composite trapezoidal rule versus via an exact formula. To do this I've generated a gaussian with mean 0, ...
73 views

### High Running Time and Suboptimal Accuracy of 2D Wave Equation Solver with Finite Differences

Im trying to solve the following 2D wave equation: $$u_{tt} = u_{xx} + u_{yy}, \hspace{3mm} u(x,y,0) = \cos(4 \pi x) \sin(4 \pi y), \hspace{3mm} u_t(x,y,0) = 0$$ with the periodic boundary condition ...
39 views

### Looping in Python to find additional vectors using a given initial condition

My question relates to looping in Python. I have an original $6$-sized vector, $F(n,t=0)=[1,0,0,0,0,0]$ Hence, the $n$-values range from $0 \rightarrow 5$. This output for the original vector is just ...
136 views

### Generating particles from a distribution function using Monte Carlo

I have been given a 4D ($x, y, v_x, v_y$) distribution function, $f(x,y,v_x, v_y)$, generated by an external code. I want to generate a set of particles from this distribution function, say 10k ...
63 views

### Divergence on wave equation simulation

I'm currenly working on my own PDE solver for non-linear simulations in python. I've done succesfully simulations for KdV and Fisher's equation, but now I'm playing with second order derivatives in ...
81 views

### illegal use of ODEINT

given the following system: $$\frac{dP}{dt} = \alpha P(1-\frac{P}{K}) - \beta P I$$ $$\frac{dI}{dt} = \beta P I - \rho I$$ how do I solve the system numerically. as when I attempt to solve this is the ...
104 views

### Implementation of Source Panel Method as Described in Katz Plotkin Book

I am currently trying to implement Source Panel method as described in Katz and Plotkin in Low Speed Aerodynamics. I have successfully implemented two previous methods. However, I am fully blocked on ...
407 views

### CUDA & Python for numerical integration and solving differential equations

Can anyone please suggest some libraries which allow use CUDA in Python for numerical integration and/or solving of differential equations? My goal is to solve large (~1000 equations) of coupled non-...
89 views

### 1D Poisson equation and quadratic basis functions assembly

I'm solving the simple Poisson problem $$-u''(x)=1$$ in the interval $[0,1]$ with $u(0)=u(1)=0$. I discretised my domain as done here, i.e. with ...
115 views

### Reason behind different outputs for Fast Fourier Transform in Numpy and Matlab

Here is the output of Numpy np.fft.ifft([0, 4, 0, 0]) array([ 1.+0.j, 0.+1.j, -1.+0.j, 0.-1.j]) # may vary Here is the output of Matlab ...
55 views

### Difference between wave vector and density matrix in numerical calculation of Schrödinger equation

I solved Schrödinger equation for a following tow-level time-dependent Hamiltonian numerically in two ways: ...
222 views

### Drawing saddle node bifurcation diagram for a non-linear ODE in Python

I'm trying to draw the bifurcation diagram of the following ODE, This ODE leads to a saddle-node bifurcation (see wiki) However what I get is not exactly right. There's a lot of "noise" as ...
169 views

### How can I learn Scientific Python?

I am an intermediate user of Matlab and Mathematica, but I would really love to start learning Python language for scientific purposes (I am interested in Maths and Physics). Could please someone ...
206 views

### Numerov method for solving Schrödinger equation

I have just begun learning computer science to apply it to Physics and I am trying to write a code for solving Schrödinger's equation of the harmonic oscillator (setting $V=\frac{x^2}{2}$) in one ...
190 views

### similar function as fmincon in python?

I am trying to solve an optimization problem where I do not have the analytic form of the objective function. I am doing analysis by FEM to find a value for displacement in each iteration but I don't ...
563 views

### Accurate Way to Calculate Matrix Powers and Matrix Exponential for Sparse Positive Semidefinite Matrices

I do need to numerically calculate the following forms for any $x\in\mathbb{R}^n$, possibly in python: $x^T M^k x$, where $M\in\mathbb{R^{n\times n}}$ is a PSD sparse matrix, $n$ can be quite large ...
1k views

I have managed to write the following code for the following problem: Projectile's horizontal and vertical displacement are given by: $$x = v_0 \, t \cos(\theta)$$ $$y = v_0 \, t \sin(\theta) - \... 2answers 136 views ### Numerically solving the equation of motion for inflation in cosmology I want to solve the equation of inflation involving a scalar field numerically using Python libraries such as odeint or scipy. ... 1answer 103 views ### Good method for correlated samples and estimating autocorrelation times I'm working on a Monte Carlo project similar to the Ising model. I've found many examples on which I've based my code. From some papers I read on binning analysis, the errors after each binning step ... 2answers 72 views ### bin 2d array such that each bin contains equal number of samples? I'm trying to bin a 2d array of points such that each bin has an equal number of samples contained in it. This would mean having bins of differing ranges and possibly shapes. I have seen this ... 0answers 84 views ### Finding the extrema of a transition probability function for a quantum walker on a graph The goal Implement some Python code to find the extrema points of a function that is strongly oscillating. The background Let G be a connected graph with n points with Laplacian matrix L(G). We ... 1answer 65 views ### Electrostatic Force - Simulate Trajectory of Test Particle using Runge Kutta - Force always Repels In the center of a 2D-Plane a positive static charge Q is placed with position r_prime. This charge creates a static electrical Field E. Now i want to place a test particle with charge Q and position ... 0answers 84 views ### Numerical simulation of magnetic dipole in a inhomogeneous magnetic field Goal: I want to use Python to illustrate how a magnetic dipole with magnetic moment m2 moves in a non-homogeneous magnetic field in a 2D-Plane. This field is generated by another magnetic dipole with ... 1answer 45 views ### Compute efficiently a 1D function relying on a 2D convolution Let X = [0,1], h the Gaussian function (i.e. \forall x \in X, h(x) = e^{-\frac{x}{2}}) and p \in L^2(X^2) I would like to compute numerically the following function :$$ \forall x \in X, \...
78 views

I have been using python to do algebraic calculations, but at many times when writing 1/2 Python writes 0.5, but I wanted you to write 1/2 and only when needed, convert it to a float. I'm having a ...
86 views

### plotting $F=K \frac{q_1 q_2}{r^3}$ in python

I tryin to plot the position of electron in the hidrogen atom by Coulomb's law, $F=K \frac{q_1 q_2}{r^3}$ This is mi code ...
405 views

### Solve a system of coupled differential equations in Python

I have a system of two coupled differential equations, one is a third-order and the second is second-order. I am looking for a way to solve it in Python. I would be extremely grateful for any advice ...
85 views

### Python routine to calculate shape resonances of H2

I am currently doing a project in which my aim is to write a program that can be used to calculate single and multi-channel shape resonances. So I'm looking at bound states and quasi-bound states. ...
45 views

### Calling User defined Matlab Functions in Python with Matlab.engine API

I am trying to combine Matlab and python scripts and figure out if I can achieve a better solution in terms of the solver-runtime. A very simple optimization problem is used just for now. But I got in ...
67 views

### Shape recognition of tangientially connected polygons using openCV

Lately, I have encountered the following problem, namely how to recognize the shapes of particular polygons. I have realized that quite straightforward to do so when the polygons are separated by ...
70 views

### Solve for large array of PD matrices

I have N matrices that are positive definite, and I have to solve for a M vectors. As M is large in my case, doing all solves simultaneously using np.linalg.solve ...
111 views

### Understanding Illumination Optimisation Problem

I am a newbie to convex optimisation and I am learning with the aid of CVXPY. I am requesting for clarity on the illumination problem as described in Boyd & Vandenberghe lecture 1 slides here. I ...
91 views

### Solve integral $\int_{-\infty}^{\infty} e^{-x^2}dx$

i trying to solve this integral $$\int_{-\infty}^{\infty} e^{-x^2}dx$$ I'm using this CODE ...
164 views

### Why is CVXPY throwing a DCP error with cp.sqrt but no error with cp.norm

I am trying to use CVXPY to optimise signal-to-noise-plus interference ratio (SINR) for a visible light communication (VLC) system. I have one of my SINR constraints stated as \...
126 views

### Exponent log to compute reciprocal power?

A MATLAB library seems to overcomplicate a computation: exp( (log(a) - log(b))/b ) which is mathematically equivalent (assuming real & positive ...
126 views

### Compute 1st derivative with backward difference approximation in python

I am trying to write a function to compute 1st derivative with backward difference approximation. $u'(x_i) = \frac{u(x_i) - u(x_i - \Delta_x)}{\Delta x} \equiv D_- u(x_i).$ And for the first point, ...
197 views

### Backwards Difference Implicit Method for Nonlinear Parabolic PDE in Python

Original Stack Overflow Question: https://stackoverflow.com/questions/65683788/indexerror-index-31-is-out-of-bounds-for-axis-1-with-size-31?noredirect=1#comment116218335_65683788 PDE: u_t = u_xx + u(...
89 views

### Error too large in leapfrog method for solving the wave equation of a vibrating string

I have been trying to figure out what I did wrong for the last two days. I do not know if I actually did something wrong or if the error is supposed to be this large in usual leapfrog problems. I ...
579 views

### ODEintWarning: Excess work done on this call (perhaps wrong Dfun type)

I was messing around with some numerical integration functions. I wrote an arbitrary differential equation to test my understanding, the code is as follows: ...
119 views

### Partition mesh into predetermined submeshes

I have a mesh already partitioned into disjoint groups of cells. What I want to achieve is the following. Obtain the adjacency graph for the cell groups. Partition the mesh, i.e. generate submeshes ...
326 views

### Trouble with backwards time integration in Python

I am struggling with a rather basic numerical integration task: Using Python's scipy.integrate.solve_ivp module to integrate an ODE sytem backwards in time. As a test, I am using the following ODE ...
278 views

### Projectile Motion Simulation with Aerodynamics using Euler's Method

I'm trying to simulate the motion of a projectile taking into account aerodynamic drag. The code works perfectly if the aerodynamic drag is zero. However, if the drag coefficient or velocity is too ...
597 views

### FEM Python book

Is there any book or site available with Finite element Method for partial differential equations with python code apart from Fenics?
1k views

### How to compute Singular value decomposition of a large matrix with Python

Language: Python3 Problem: I have a matrix Q of shape [51200 rows x 51200 cols] stored in a binary file, each of the element in this matrix has a data type of complex64. To load the data into memory I ...
248 views

### Efficiently computing $e^{tX}$ for many different values of $t$

Given an anti-Hermitian and sparse matrix $X$, I am using Python (NumPy and SciPy) to compute the matrix exponential $f(t) := e^{tX}$ for many values of $t$. The method I am currently using is to ...
156 views

### The derivative of a gauss function via FFT and IFFT in Python

I have a problem with computing a derivative of a Gauss function using FFT and IFFT from NumPy library. I use the fact that  \frac{d}{dx}f(x) = \frac{1}{\sqrt{2\pi}}\int{ike^{ikx}\...
121 views

### Help with modified bessel functions

I'm trying to parse the following expression (Eq. 35 in The Magnetic Field in the Vicinity of Parallel and Twisted Three-Wire Cable Carrying Balanced Three-Phased Current) in Python and calculate its ...
23 views

### petsc4py path error running PETSC program in python

I tried to run an example PETSC program in python. I used pyinstaller --onefile example.py to create a binary form and ...