All Questions
Tagged with numerical-methods scipy
186 questions
3
votes
2
answers
118
views
Using SciPy minimize to solve the Catenary problem
I am attempting to use SciPy optimization to find the function that minimizes the potential energy of a hanging rope between 2 points, the catenary problem. The objective function is defined by
The ...
0
votes
1
answer
41
views
SciPy's solve_bvp going solving for the wrong parameter values
I am solving a ordinary differential equation with scipy's solve_bvp to get a family of eigenstates phi_n and eigenvalues omega_n. The equation looks something like
y'' = -(omega_n - f(x)) ** 2 * y,
...
3
votes
1
answer
122
views
Inaccurate Eigenvectors from LAPACK's ZHEEVR routine
I was trying to replace Scipy's eigh routine in my cython code with C-level LAPACK. Essentially I want to compute eigenvectors corresponding to the largest and smallest eigenvalues of a hermitian ...
0
votes
1
answer
228
views
How do I use numpy.gradient() with coordinate arrays?
I have three 1D arrays representing some spatial data (u) over a rectangular region of space: the first array contains the x coordinates, the second contains the y coordinates, and the third contains ...
1
vote
0
answers
104
views
how to prevent scipy's solve_ivp solution error from increasing in large domain
My code is used to simulate various forms of the differential equation -2 / (h(t) - λ(t)) for some starting point h0 and driver λ(t). I've ran this for some h0 and λ(t) with good success (such as λ(t) ...
0
votes
1
answer
151
views
scipy fsolve can't solve equation with logarithm
from scipy.optimize import fsolve
def func(x):
return [x[0] * 0.00096 + x[1]*0.0259*np.log(0.00096/x[2]) - 1.2505,
x[0] * 0.00301 + x[1]*0.0259*np.log(0.00301/x[2]) - 1.2829,
...
0
votes
0
answers
55
views
what happens if solve_ivp's solution contains a complex values?
I'm attempting to use scipy's solve_ivp in order to perform some numerical analysis on Loewner equations, specifically on the first hitting time between the solution and a certain function. My ...
2
votes
1
answer
227
views
scipy's solve_ivp having trouble solving simple ivp
I'm attempting to solve a very simple IVP in Python in order to do error analysis:
import numpy as np
from scipy.integrate import solve_ivp
def dh_dt_zerodriver(t, h):
return -2 / h
t = 50
steps ...
1
vote
2
answers
138
views
Checking Event in solve_ivp
What is a fast way of checking which event has been triggered in scipy.solve_ivp()?
For context, I am simulating a double pendulum. I want to check if either the first rod or the second rod has been ...
0
votes
1
answer
200
views
Scipy solve_ivp extremely slow/freezing
I have been working on a problem that involves solving 2 pairs of coupled ode's. It has been recommended that we use scipy solve_ivp to do this however it seems to be either freezing or running ...
0
votes
0
answers
76
views
Backward in time integration of numerical ODE
I have an array of size (n, 2, 2) that represent A( x(t), u(t) ), x[n] and u[n] been one dimensional arrays. I have the equation
p'(t) = A( x(t), u(t) ) p(t) , with the final condition been p(T) = p_T....
1
vote
0
answers
110
views
Given a function from an array to an array, compute the linear approximation of the function
Suppose I have a function that takes an input as a numpy array (vector) and outputs another vector after performing some non-linear transformation. I have provided a simple, illustrative example here :...
1
vote
1
answer
252
views
Cholesky decomposition of a positive-semidefinite matrix using some Python library
I am looking for a built-in function that implements a Cholesky decomposition of a positive semidefinite matrix in Python.
There exist implementations of NumPy (numpy.linalg.cholesky) and SciPy (scipy....
1
vote
0
answers
91
views
How to transform U to V in Copula
I have two set of variables, one is a function of the other one. I am trying to first fit a copula to them using "copulas package" and then perform extreme value analysis for the dependent ...
-1
votes
1
answer
93
views
Hermite interpolation in python with out using scipy library
def hermite_interpolation(x, y, yp, xi):
n = len(x) - 1
result = 0.0
for j in range(n + 1):
term = y[j]
for i in range(n + 1):
if i != j and x[j] != x[i]:
...
0
votes
0
answers
93
views
minimizing log likelihood to find the parameters for my heston nandi model using python
I have an objective function which I would like to use MLE to find the parameters for my model.This is my code so far.Also my returnarr is an array of log returns of the stock
Here is the first 30 ...
1
vote
0
answers
74
views
fsolve + pchip + max: not finding root
I am trying to find the roots of multiple equations over a grid (want the equations to be 0 evaluated at the points in the grid) and am stacking all the function values as a vector.
I was able to do ...
2
votes
0
answers
112
views
Solving non-linear differential equation (2-order, with singularity)
Well, I have to numerically solve the a non-linear DE for x in (0,1), which is presented in the picture below. Moreover, I know, that at x=0 y should be equal to chosen constant (0.25), and at x = 1 y ...
0
votes
0
answers
49
views
Solving a kinky secondary order ODE via Python Scipy
I have the following second order ODE and I wish to solve it via Python Scipy:
How can I accomplish this?
1
vote
2
answers
438
views
How To Solve A Boundary Value Problem In Python Using A Function Like Mathematica's NDSolve[] (method LSODA and sub-method Newton)?
I have a modified steady-state 1-D conduction equation:
a T(z) + a z T'(z) + b T'(z) = g T''(z) + h
where a, b, g, h > 0, z ranges from z1 = 0 m and z2 > 0 m, and T ranges from T1 and T2 (with ...
0
votes
0
answers
44
views
Can the fun argument of scripy.optimize.root be longer than the x0 argument?
I am solving a non linear partial differential equation in python and I am using scripy.optimize.root. My initial guess x0 has a length of N and my function that I want to optimize has a length N+1 ...
0
votes
1
answer
111
views
Different results when computing integral analytically/with scipy VS approximating integral
I'm trying to calculate the integral of a variant of the Hill Equation shown here.
When I try implementing this with the integrate function in SciPy, I get the following:
from scipy import integrate
...
0
votes
1
answer
653
views
How do optimize a function of many independent variables by SciPy if variables are limited by range?
I'm trying to optimize a function obtained with machine learning based on polynomial regression, so I don't have any analytical relationship.
This function has 17 input parameters/independent ...
0
votes
1
answer
473
views
Hypergeometric function 1F2 in scipy
How can I write the Hypergeometric function 1F2 in scipy/numpy?
There was an implementation in scipy, but now it is deprecated:
https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy....
1
vote
0
answers
71
views
Unable to fit a function onto a givin set of data points in Python using the Scipy library
I have been trying to fit a function(the function is given in the code under the name concave_func) onto data points in python but have had very little to no success. I have 7 parameters(C_1, C_2, ...
0
votes
0
answers
112
views
TypeError: cannot unpack non-iterable NoneType object (after number of iterations)
When ever maximum number of iterations (max_n) were smaller than 34 , I get TypeError: cannot unpack non-iterable NoneType object . But if its bigger than 34 there's no problem it works just fine . I ...
2
votes
0
answers
68
views
Modeling hanging chain using numerical methods in python
I am trying to model a hanging chain using numerical methods (this pde ) by following the example here for a wave on a string. I am having trouble getting a good approximation from my differencing ...
1
vote
0
answers
191
views
Numerically integrate using SciPy library in Python
I have an acceleration vs time graph like this:
I am using SciPy library and cumtrapz function to get the velocity vs time graph:
The code snippet:
velocity_int = sp.integrate.cumtrapz(acceleration, ...
1
vote
1
answer
412
views
Solving a tridiagonal matrix in python
I have been looking at numerical methods to solve differential equations for chemical reactions. Usually I put the differential equation into a tridiagonal matrix using finite difference method, and ...
0
votes
1
answer
133
views
How to check the accuracy of the results obtained from a Numerical simulation which used ODE solver solver_ivp in scipy?
I have a question regarding the results we are obtaining from ODE solvers. I will try my best to briefly explain the question I have with me. For an example if we ran a simulation with ANSYS or may be ...
1
vote
0
answers
61
views
How can I solve this matrix ODE using python?
I would like to numerically compute this ODE from time 0 -> T :
ODE equation where all of the sub-matrix are numerically given in a paper. Here are all of the variables :
import numpy as np
T = 1
...
0
votes
1
answer
394
views
having trouble with finding multiple roots of a system of equations with scipy fsolve
Having a system of two coupled equations as below:
two_exponential = lambda x, kernel, c: np.array([x[0] - np.exp(kernel[0] * x[0] + kernel[2] * x[1] + c), x[1] - np.exp(kernel[1] * x[1] + kernel[3] * ...
0
votes
0
answers
256
views
Is there an odeint equivalent for solve_ivp's event parameter?
I am currently using solve_ivp to solve a system of ODEs. I am hoping to switch to ODEint due to the decreased simulation time, however, I have not found an easy way to track events occurring using ...
5
votes
1
answer
879
views
Bifurcation diagram of dynamical system
TL:DR
How can one implement a bifurcation diagram of a seasonally forced epidemiological model such as SEIR (susceptible, exposed, infected, recovered) in Python? I already know how to implement the ...
1
vote
1
answer
414
views
Unable to reproduce simple figure from textbook (possible numerical instability)
I am trying to reproduce figure 5.6 (attached) from the textbook "Modeling Infectious Diseases in Humans and Animals (official code repo)" (Keeling 2008) to verify whether my implementation ...
0
votes
0
answers
122
views
Solving system of nonlinear equations numerically in python
I want to solve the following problem numerically and first defined it as a function:
def F(x, na=1, nb=2, L=6, A=0.09, V=0.54, dh=0.03, k=10**-2, dB=0.005, fB=38, ur=0.39, rhoW=998, rhoG=1.2, g=9.81, ...
2
votes
2
answers
2k
views
Is there a method to check for convergence when using newon's method?
I have a function that I want to solve using newton's method. The problem is that the convergence depends on the initial guess.
I want to write an if statement. If the function converges then to store ...
0
votes
1
answer
259
views
Finding the roots of a system of non-linear equations that has multiple roots with python fsolve
I have the following system of non-linear equations which I want to find its roots:
x - exp(a x + b y) = 0
y - exp(c x + d y) = 0
The code I'm using to find its roots is:
equations = lambda x, ...
0
votes
0
answers
107
views
Optimize with another variable
sorry i am a real beginner in Python and I really need help to understand this:
So basically I have a function who takes 4 arguments:
def expected_utility(p,y,x,q):
return p*u(y-x+q-premium(p,q), ...
0
votes
1
answer
917
views
Solve ODE with Scipy solve_ivp where ODE functional form changes with time
If I have the following model:
dy/dt = 2- 6t + 3t^2, t <= 1,
dy/dt = y, t >1.
How would I be able to implement this in python using scipy solve_ivp. When I naively implement a while loop within ...
2
votes
1
answer
420
views
Minimization of a function with constraints in Python
I'm trying to minimize a function of three N-sized arrays that contain the minimization parameters that I want to find. For example, suppose the parameters I want to find in order to minimize the ...
0
votes
1
answer
509
views
Minimization of a function with iterative bounds in Python
I'm trying to minimize a function of N parameters (e.g. x[1],x[2],x[3]...,x[N]) where the boundaries for the minimization depend on the minimized parameters themselves. For instance, assume that all ...
2
votes
1
answer
5k
views
Lagrange multipliers numerical approach
I have the following function that I'm trying to optimize that is subject to a constraint:
def damage(a, e, cr, cd):
return 100*(1+a)*(1+e)*(1+cr*cd)
def constraint(a, e, cr, cd):
return (100/...
2
votes
0
answers
42
views
Scipy solution of coupled ODEs flatlines after a critical value
I am trying to solve a system of two coupled ODEs using Scipy's solve_ivp function. Namely the hydro-static equilibrium and the mass for a white dwarf with full units.
My initial conditions are that ...
1
vote
0
answers
817
views
"Cannot unpack non-iterable float object error " in Scipy's ivp
I am trying to integrate a second-order ODE using Scipy's ivp. I have separated the 2nd order ODE into a system of two first-order ODE's using Sympy. This is the code I am using
""" ...
0
votes
0
answers
93
views
Simplifying ODE with sympy and then solving numerically with Scipy
I want to solve numerically the equations of motion for a relativistic particle in a E/M field:
where, ,
and,
.
I have started by simplifying with Sympy to solve the first equation for . Using
"...
2
votes
2
answers
561
views
Vectorized coupled ODE’s for a N body Problem in python
I’m trying to solve a n-Body problem using hamilton equations of motion; here is what I’v done:
1.) First I define random initial values for momentums and the vectors positions:
import numpy as np
...
0
votes
0
answers
523
views
Solving system of 1-D partial differential equations numerically in python (py-pde lib or any other)
I have the following system of partial differential equations:
Where
c0 - constant,
r - independent spatial variable,
t - time variable,
f(r, t) - 1st unknown function,
f_t(r, t) - 2nd unknown ...
1
vote
0
answers
671
views
How can I compute numerically mixed partial derivatives of a function without SymPy?
How can I calculate mixed partial derivatives a function: ? I understand there is a SymPy way of doing this, but I would prefer numeric computation with the standard and established numeric techniques ...
0
votes
1
answer
118
views
Best approach for solving equation with truncations in Python
I am trying to solve an equation that can include truncations in Python with a numerical approach. I am wondering what the best library and approach would be? Following is more detail about the ...