runge-kutta

Python : Speeding up my Runge-Kutta integration code challenge

拟墨画扇 提交于 2019-12-01 14:10:59
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]-t[i]))) print "Integration took ",time.clock() - start, " s" return np.array(result) def RK4step(rhs,V

Cannot get RK4 to solve for position of orbiting body in Python

…衆ロ難τιáo~ 提交于 2019-11-29 12:56:56
I am trying to solve for the position of a body orbiting a much more massive body, using the idealization that the much more massive body doesn't move. I am trying to solve for the position in cartesian coordinates using 4th order Runge-Kutta in python. Here is my code: dt = .1 t = np.arange(0,10,dt) vx = np.zeros(len(t)) vy = np.zeros(len(t)) x = np.zeros(len(t)) y = np.zeros(len(t)) vx[0] = 10 #initial x velocity vy[0] = 10 #initial y velocity x[0] = 10 #initial x position y[0] = 0 #initial y position M = 20 def fx(x,y,t): #x acceleration return -G*M*x/((x**2+y**2)**(3/2)) def fy(x,y,t): #y

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

筅森魡賤 提交于 2019-11-29 12:15:25
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) Defining the Runge-Kutta45 method def fx(x,y,z,t): dxdt=sigma*(y-z) return dxdt def fy(x,y,z,t): dydt

Runge-Kutta code not converging with builtin method

Deadly 提交于 2019-11-28 14:28:39
I am trying to implement the runge-kutta method to solve a Lotka-Volterra systtem, but the code (bellow) is not working properly. I followed the recomendations that I found in other topics of the StackOverflow, but the results do not converge with the builtin Runge-Kutta method, like rk4 method available in Pylab, for example. Someone could help me? import matplotlib.pyplot as plt import numpy as np from pylab import * def meurk4( f, x0, t ): n = len( t ) x = np.array( [ x0 ] * n ) for i in range( n - 1 ): h = t[i+1] - t[i] k1 = h * f( x[i], t[i] ) k2 = h * f( x[i] + 0.5 * h * k1, t[i] + 0.5 *

ODE Runge Kutta MATLAB error

China☆狼群 提交于 2019-11-28 12:02:30
问题 so I'm trying to create a Runge Kutta function and this is my code : function [t,U] = RK(f, n, eta, interv) h = (interv(2)-interv(1))/n; t = interv(1):h:interv(2); v(1) = eta(1); w(1) = eta(2); for i=1:n k1 = f([v(i),w(i)]); k2 = f([v(i),w(i)]+h*k1/2); %f(t(i)+h/2, u(:,i)+h*k1/2); k3 = f([v(i),w(i)]+h*k2/2); k4 = f([v(i),w(i)]+h*k3); v(i+1) = v(i) + h*(k1(1)+2*k2(1)+2*k3(1)+k4(1))/6; w(i+1) = w(i) + h*(k1(2)+2*k2(2)+2*k3(2)+k4(2))/6; end U = [v;w]; end Where U is a matrix of 2 lines and n+1

Cannot get RK4 to solve for position of orbiting body in Python

ぐ巨炮叔叔 提交于 2019-11-28 06:45:04
问题 I am trying to solve for the position of a body orbiting a much more massive body, using the idealization that the much more massive body doesn't move. I am trying to solve for the position in cartesian coordinates using 4th order Runge-Kutta in python. Here is my code: dt = .1 t = np.arange(0,10,dt) vx = np.zeros(len(t)) vy = np.zeros(len(t)) x = np.zeros(len(t)) y = np.zeros(len(t)) vx[0] = 10 #initial x velocity vy[0] = 10 #initial y velocity x[0] = 10 #initial x position y[0] = 0 #initial

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

梦想与她 提交于 2019-11-27 08:29:25
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 following: For RK-method, my code is: h=1/50; x = 0:h:1; y = zeros(1,length(x)); y(1) = 2; F_xy = @(t,r) 4.*exp

Solve a system of equations with Runge Kutta 4: Matlab

心已入冬 提交于 2019-11-27 08:17:58
问题 I want to solve a system of THREE differential equations with the Runge Kutta 4 method in Matlab ( Ode45 is not permitted). After a long time spent looking, all I have been able to find online are either unintelligible examples or general explanations that do not include examples at all. I would like a concrete example on how to implement my solution properly, or the solution to a comparable problem which I can build on. I have come quite far; my current code spits out a matrix with 2 correct