runge-kutta

How to set fixed step size with scipy.integrate?

帅比萌擦擦* 提交于 2020-01-25 10:33:06
问题 I am looking for a way to set a fixed step size for solving my initial value problem by Runge-Kutta method in Python. Accordingly, how I can tell the scipy.integrate.RK45 to keep a constant update (step size) for its integration procedure? Thank you very much. 回答1: By looking at the implementation of the step, you'll find that the best you can do is to control the initial step size (within the bounds set by the minimum and maximum step size) by setting the attribute h_abs prior to calling

Python : Speeding up my Runge-Kutta integration code challenge

不问归期 提交于 2020-01-11 07:52:47
问题 I am using the attached code to integrate a version of Fitzhugh-Nagumo model : from scipy.integrate import odeint import numpy as np import time P = {'epsilon':0.1, 'a1':1.0, 'a2':1.0, 'b':2.0, 'c':0.2} def fhn_rhs(V,t,P): u,v = V[0],V[1] u_t = u - u**3 - v v_t = P['epsilon']*(u - P['b']*v - P['c']) return np.stack((u_t,v_t)) def integrate(func,V0,t,args,step='RK4'): start = time.clock() P = args[0] result=[V0] for i,tmp in enumerate(t[1:]): result.append(RK4step(func,result[i],tmp,P,(t[i+1]

Howto pass a function to a function in Python?

让人想犯罪 __ 提交于 2020-01-04 09:39:23
问题 I am a beginner/intermediate in Python. I have coded a 4th-order Runge-Kutta method (RK4) into Python. It is basically solving a pendulum, but that is not the point here. I want to improve the RK4 method in the following way: I want to be able to pass the function f directly to the RK4 function, i.e. RK4(y_0, n, h) should become RK4(f,y_0,n,h). This would have the great advantage that I could use RK4 for other f functions that describe other systems, not just this one pendulum. I have played

Howto pass a function to a function in Python?

半腔热情 提交于 2020-01-04 09:39:12
问题 I am a beginner/intermediate in Python. I have coded a 4th-order Runge-Kutta method (RK4) into Python. It is basically solving a pendulum, but that is not the point here. I want to improve the RK4 method in the following way: I want to be able to pass the function f directly to the RK4 function, i.e. RK4(y_0, n, h) should become RK4(f,y_0,n,h). This would have the great advantage that I could use RK4 for other f functions that describe other systems, not just this one pendulum. I have played

Absolute error of ODE45 and Runge-Kutta methods compared with analytical solution

夙愿已清 提交于 2019-12-28 02:53:05
问题 I would appreciate if someone can help with the following issue. I have the following ODE: dr/dt = 4*exp(0.8*t) - 0.5*r ,r(0)=2, t[0,1] (1) I have solved (1) in two different ways. By means of the Runge-Kutta method (4th order) and by means of ode45 in Matlab. I have compared the both results with the analytic solution, which is given by: r(t) = 4/1.3 (exp(0.8*t) - exp(-0.5*t)) + 2*exp(-0.5*t) When I plot the absolute error of each method with respect to the exact solution, I get the

Writing Runge-Kutta ODE solver using gsl

社会主义新天地 提交于 2019-12-25 04:08:39
问题 It's been some time since I did any C/c++, but I wanted to write an ODE solver using the gsl library to solve the following ODE set $$ u'(r)=up(r)$$ $$ up'(r)=-(2*(r-1)/(r*(r-2)))*up(r)-((r*r/((r-2)*(r-2)))-(2/r*(r-2)))*u(r) $$ so in the gsl notation my y[0]=u, y[1]==up, and the RHS of the above defines f[0] and f[1]. From these definitions one can then compute the Jacobian and dfdr (usually their 'time' variable is called 't' not 'r'). The reason for doing this is because I am having speed

Python 2.7 Runge Kutta Orbit GUI

大城市里の小女人 提交于 2019-12-24 06:34:34
问题 I'm trying to create a GUI in Python using Tkinter that plots the orbital path of a mass using the Runge-Kutta method. My GUI works fine but my issue is that it only plots a straight line no matter what values I input to the GUI. I was hoping someone could show me what is wrong with my function within the GUI so that it will actually plot the orbit properly. def calcPath(self): M = float(self.entM.get()) m = float(self.entm.get()) G = 6.67e-11 c = 3e8 velocity = np.array([float(self.entvx.get

Integration of orbits with solar system gravity fields from Skyfield - speed issues

末鹿安然 提交于 2019-12-21 05:21:19
问题 In the time tests shown below, I found that Skyfield takes several hundred microseconds up to a millisecond to return obj.at(jd).position.km for a single time value in jd , but the incremental cost for longer JulianDate objects (a list of points in time) is only about one microsecond per point. I see similar speeds using Jplephem and with two different ephemerides. My question here is: if I want to random-access points in time, for example as a slave to an external Runge-Kutta routine which

Runge Kutta problems in JS

て烟熏妆下的殇ゞ 提交于 2019-12-19 10:26:20
问题 I'm attempting a Runge-Kutta implementation for a mass on a spring in Javascript and visualizing it with D3. The purpose is to compare it to Forward Euler and comment on the differences. My FE works fine and plots fine, but the Runge-Kutta is shooting off in a negative direction and never wrapping around. Here's a plunkr with the vis and the code, but I'll add the JS (only for the ODE solvers) too. // *** Functions for ODE Solvers *** // function FEx (x, v, h) { return x + h*v; } function FEv

Solving the Lorentz model using Runge Kutta 4th Order in Python without a package

孤者浪人 提交于 2019-12-18 07:11:50
问题 I wish to solve the Lorentz model in Python without the help of a package and my codes seems not to work to my expectation. I do not know why I am not getting the expected results and Lorentz attractor. The main problem I guess is related to how to store the various values for the solution of x,y and z respectively.Below are my codes for the Runge-Kutta 45 for the Lorentz model with 3D plot of solutions: import numpy as np import matplotlib.pyplot as plt #from scipy.integrate import odeint #a