Questions tagged [python]

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

Filter by
Sorted by
Tagged with
0
votes
1answer
41 views

Sum of random variables - Check your derived distribution against a numerical calculation/histogram

Consider independent random variates $X_0, X_1, . . .$ each uniformly distributed on the support $[0, 1)$ Let's say $Y = X_0 + X_1$, where $X_0$ and $X_1$ are independent uniform random variables with ...
1
vote
1answer
58 views

Calculating Error for Poisson Equation using Successive Over-Relaxation technique, Python

I am trying to solve the Poisson Equation $\frac{\partial^2 T}{\partial x^2} + \frac{\partial^2 T}{\partial y^2} = 32(x(x-1) + y(y-1))$ for a 61x61 grid using Python3 with boundary conditions being $T=...
-1
votes
0answers
53 views

I am plotting too many figures the animations get slower as a result. Need to clear figure or something

I am trying to embed an animation using FuncAnimation from matplotlib into a tkninter GUI. In the execute button at the bottom I am calling the Execute function. If I click the execute button many ...
2
votes
2answers
91 views

Use Monte Carlo integration to compute the volume and centre of mass in Python

In particular, I want to focus on finding the volume $V$ because I will need it to start working on solving the centre of mass This $3D$ homogenous body (Torus section) is defined by $$x^2 + \left(\...
2
votes
1answer
96 views

(Lack of) Availability of Finite-Difference library for simple 2D PDEs

I would like to solve two types of simple 2D problems, namely the stationary heat equation on an L shaped geometry like this: And also compute the magnetostactic field in an air gap of the following ...
0
votes
0answers
24 views

Negative binomial expansion of general symbolic polynomial

Using Sympy, I would like to compute the negative binomial expansion of a general symbolic polynomial, e.g., $(x_1 + x_2 + x_3 + 4 x_4)^{-1}$. I understand that I can go by recursively partitioning ...
1
vote
1answer
46 views

Numerical integration problem: IntegrationWarning The integral is probably divergent, or slowly convergent

I am trying to get the numerical integration of a function using scipy's integrate.quad as follows. $$ \begin{equation} G (\alpha) = \frac{4\alpha}{\pi}\int_0^{\...
0
votes
1answer
28 views

Odeint error for nonlineal differential equations

I receive the following error when I run the code. ODEintWarning: Excess work done on this call (perhaps wrong Dfun type). Run with full_output = 1 to get quantitative information. warnings.warn(...
0
votes
0answers
26 views

How can I change th code to implement the Fourier series as defined?

I want to compute the Fourier series expansion of the function $$f:[-1,1] \to \mathbb{R}$$ with $$f(x) = \begin{cases} -x & x < 0 \\ 0 & x \geq 0 \end{cases} $$ .The code ...
2
votes
2answers
182 views

Python scipy eigh(Arpack) giving wrong eigenvalues for generalized eigenvalue problem

I am trying to solve a generalized eigenvalue problem using Arpack, right now the code is using LAPACK but that's too slow, we only need a few eigenvalues and the matrices are sparse so using Arpack ...
3
votes
1answer
77 views

Meshing surface of a sphere with a subdomain

I am trying to build a triangle mesh of the surface of a sphere which also includes a subdomain defined by a 'polygon'. Here is a successful example (subdomain defined by the red dots): Note that the ...
17
votes
1answer
2k views

How can I avoid catastrophic cancellation?

I have the following formula that I need to rewrite in order to avoid catastrophic cancellation. $$y =\sqrt{\frac{1}{2}\left(1-\sqrt{1-x^{2}}\right)}$$ As $x$ becomes smaller, $\sqrt{1-x^{2}}$ ...
1
vote
1answer
126 views

FEM does not match exact solution

I am trying to solve : $$-u''(x) + u(x) = \sin(2\pi x)\, ,\quad 0<x<1\, ,$$ $t>0$, with $u(0) = u(1) = 0$. That has as exact solution $$u(x) = \frac{\sin(2\pi x)}{1 + 4\pi^2}\, .$$ But the ...
0
votes
0answers
22 views

Library/project on python for solving conjugate heat transfer problems

Can someone recommend a python library that can help me solve heat transfer problems between turbulent fluids and solids? Thanks.
0
votes
0answers
43 views

How to make fuzzy rules?

I have a dataset with weather factors(rainfall, temperature, humidity etc.) and crop yield. I want to make fuzzy rules. Considering the large number of features, it cannot be done manually by ...
0
votes
0answers
47 views

How to take convolution of two arrays in Python by using NumPy?

Generally, we know that if we have this relation between Fourier transforms of three functions in frequency domain as: $$\mathfrak{F}\{\mathsf{P}(t)\} = \mathfrak{F}\{\mathsf{Z}(t)\}\mathfrak{F}\{\...
3
votes
1answer
135 views

How to get a more accurate cancelation

I shall try to get to the point, so let me know if there is something left and you need more details. I am solving a couple of equations that are not coupled explicitly, but their corresponding ...
0
votes
0answers
85 views

The inverse of the numpy.gradient function

The numpy.gradient function is computed using second order accurate central differences in the interior points and either first or second order accurate one-sides (forward or backwards) differences at ...
1
vote
1answer
72 views

DOP853 integration method is missing (SciPy)

I was checking some integration methods provided by SciPy, in which the DOP853 should be included according to the webpage (https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate....
1
vote
1answer
69 views

Coupled second-order differential equations using runge kutta 45

As a little summer project I have tried to make a ballistic calculator for when I play football, (following an example from a book), just to learn some numerical methods while doing so. My problem is ...
1
vote
1answer
44 views

Effecient method for iterating over sparse dataset

Apologies if this isn't the appropriate forum for this question. I have a set of elements that I need to iterate over as part of a modeling workflow. The elements exists over a set of dimensions (i, ...
2
votes
2answers
150 views

Generate random smooth 2D closed curves

I would like to know how can I generate a collection of random 2D closed smooth curves. I thought about generating a random 3D surface with random peaks, and then intersecting the Z=0 plane with it, ...
0
votes
0answers
26 views

Set of integrators do not consistently solve an equation in Python

I must solve the following second order differential equation: $\delta \phi^{''}_{\mathbf{k}}+(3-\epsilon)\delta \phi^{'}_{\mathbf{k}}+\left(\frac{k^2}{a^2 H^2}+\frac{V_{,\phi\phi}}{H^2}-6\epsilon +4\...
4
votes
1answer
95 views

Imposing no flux boundary conditions on variants of the Cahn-Hilliard equation using Finite Differences in Python

I have been looking into simulations of phase separation in variants of the Cahn-Hilliard system and have been running into issues with implementing no flux boundary conditions on certain variants. ...
1
vote
1answer
49 views

How to optimize nuclear norm subject to positive semidefinite constraints?

For finite dimensional symmetric positive semidefinite matrices $A$ and $B$, I would like to solve \begin{align}&\min |X - A|_1 \\ &\text{subject to}\\ &X \preceq B \\ &0 \preceq X\...
2
votes
1answer
165 views

Solving Lotka-Volterra Equations on Python

I'm trying to plot Lotka-Volterra Equations using Python. I am a real beginner when it comes to Python. I have these two equations: $$\frac{dR}{dt}=\alpha R-\gamma RF$$ and $$\frac{dF}{dt}=-\beta F+\...
2
votes
1answer
112 views

Why is $1/r^2$ force law giving spiral trajectory?

I have written a program to solve for Newton's 2nd Law of motion for a given force law, in 2D polar coordinates. It is known that if the force law is of the form $k/r^2$,we get conic sections as ...
0
votes
0answers
51 views

Non-linear differential equation

I have this equation $$y\left(\dot y^2+1\right)=m + \Lambda y^3,$$ where $\Lambda=1.1\cdot 10^{-52} $ (Cosmological constant). I want to get the graph of the solution of this equation (2-parametric ...
0
votes
1answer
55 views

How to use cumtrapz correctly?

I have tried to do a trapeze integration with f(x)=x^2, where I know how the antiderivative looks like, so F(x) = (1/3)x^3 Here's my code, just like I tried: ...
0
votes
1answer
33 views

Integrating Matrix Elements TypeError: f() takes 1 positional argument but 3 were given

I'm working on a linear variational problem for a general PIB and I keep encountering the same problem, and I know its a rather simple solution. Any suggestions? ...
0
votes
0answers
45 views

Plotting a Magnetic Field in Spherical Coordinates in Python

I am modeling a Helmholtz Coil as two dipoles from far away and I want to plot the magnetic field. $$\mathbf{B}(\mathbf{r}) = \frac{\mu_0 |\mathbf{m}|}{4\pi r^3}\left(2\cos\theta\,\hat{\mathbf{r}} + \...
0
votes
0answers
16 views

How to multiply 2 decision variables and a matrix using python

So, basically our agenda is to assign tour guides to tour groups based on this equation and that will be done by these 2 decision variables z(u,g) and y(g,p) where z(u,g) will be 1 if tour guide 'u' ...
4
votes
3answers
91 views

I wrote a 2D Finite Element program for Axial Loaded Plates, but the results are unexpected

TLDR: I used Python to write a 2D Finite Element program using 'Constant Strain Triangles' and my beam keeps pointing slightly upwards instead of straight sideways (like the force). I'm new to FEA and ...
0
votes
0answers
42 views

scipy's solve_ivp returns erroneous results for a stiff differential equation

I'm using scipy's solve_ivp for solving a stiff differential equation. I'm using method BDF for solving the same. I have already used MATLAB's ode23s and I'm getting correct results in MATLAB. However,...
0
votes
0answers
77 views

A parallelized GMRES solver?

My application calls for solving a dense, 40,000 x 40,000, ill-conditioned linear system. The native SciPy GMRES solver with preconditioning has worked well for my application and solving a single ...
3
votes
1answer
31 views

Ising NiO model energy

I am simulating Ising model for NiO. I have simulated for 2d,3d,triangular lattices, and have tried to do the same with NiO model. There are papers which say that the ground state energy is around -...
0
votes
1answer
42 views

Norm constraint in CVXPY

I'm trying to implement the algorithm outlined in https://arxiv.org/abs/1211.5608 on a small scale. I have a linear operator $\mathcal{A}$ which is defined as $$\text{trace}(A^*_l(hm^*))$$ where $$A_l ...
0
votes
0answers
29 views

Comparing custom linear regression solver to SciPy equivalent in Python

From a given data set, I set out to complete a task which is below Fit the data of the previous exercise to fit Eq. (8.18) using the SciPy function ...
2
votes
1answer
40 views

Question regarding the energy computation of the Ising-Spin Model

In most of the Monte-Carlo-Algorithms I studied, I found, at the place where they compute the energy, always a line of code, where they divided by four. For example, this code-snippet is taken from ...
3
votes
1answer
171 views

Why is 'scipy.sparse.linalg.spilu' less efficient than 'scipy.linalg.lu' for sparse matrix?

I have a matrix B which is sparse and try to utilize a function scipy.sparse.linalg.spilu specialized for sparse matrix to ...
5
votes
1answer
154 views

Algorithm to factorize matrix whose many rows are already of upper triangular form?

I have a matrix whose many rows are already in the upper triangular form. $$\begin{bmatrix} x_{11} & x_{12} & x_{13} & x_{14} & x_{5} \\ 0 & x_{22} & x_{23} & x_{...
3
votes
1answer
111 views

Computing numeric derivative via FFT - SciPy

I wrote the following code to compute the approximate derivative of a function using FFT: ...
3
votes
1answer
59 views

Fast nonzero indices per row/column for (sparse) 2D numpy array

I am looking for the fastest way to obtain a list of the nonzero indices of a 2D array per row and per column. The following is a working piece of code: ...
1
vote
0answers
45 views

Determining the voxels between two boundary surfaces

Update (Solution) -17 October 2020- I finally did it! You can check the code on my GitLab repo. The function is to do this is the domain_extract which identifies ...
2
votes
1answer
25 views

Getting a list of coordinate points of a Cartesian grid in Python

I have a regular three dimensional Cartesian Grid with numpy.linspace(a1,an,na), numpy.linspace(b1,bn,nb), numpy.linspace(c1,cn,nc) along their respective ...
0
votes
0answers
22 views

How to plot a function for multiple values of a parameter in the same set of axes in Python?

I'm currently trying to plot a graph wich describes a photoionization cross section as a function of incident photon energy for optical transition in a semiconductor for different values of the $\...
5
votes
0answers
102 views

How to numerically evaluate this double Integral?

I want to evaluate the following integral: $$\int_{0}^{60} \ \left(\int_{0}^{2z} 0.5\cdot t \left(\mathrm{erf}(t-a) -1 \right)J_{0}(qt)\mathrm{d}t \right)^2 \mathrm{exp}\left(-\frac{(z-a)^2}{2s^2}\...
1
vote
1answer
100 views

Composite matrices in Numpy

Lets say I have four matrices A, B, C and D, and I want to combine them together into one new matrix for computation: $$ \left( \begin{matrix} A & B\\ C & D \end{matrix}\right) $$ How can I ...
3
votes
1answer
123 views

Suggestions for scientific computing projects to undertake to sharpen core skills

Currently I am completing all exercises in books like "Introduction to Python for Science and Engineering, David Pine" and "Guid to Scientific Computing in C++, Pitt-Francis, Whitley". I am looking ...
0
votes
1answer
29 views

How to implement tolerance checking within a for loop whilst performing a numerical integration using a trapezoidal function?

Currently, I have this working code where I have been able to successfully calculate the integration for standard results. But in terms of precision, how could I achieve a good tolerance? ...

1
2 3 4 5
13