runge-kutta

Is there a better way to tidy this chuck of code? It is the Runge-Kutta 4th order with 4 ODEs

喜夏-厌秋 提交于 2021-02-15 07:55:12
问题 def xpos(x,y,t): #dx/dt = v_x return vx def ypos(x,y,t): #dy/dt = v_y return vy def xvel(x,y,t): #dv_x/dt = -GMx/r^3 return -G*M*x/((x)**2 + (y)**2)**1.5 def yvel(x,y,t): # dv_y/dt = -GMy/r^3 return -G*M*y/((x)**2 + (y)**2)**1.5 xposk1 = h*xpos(x,y,t) yposk1 = h*ypos(x,y,t) xvelk1 = h*xvel(x,y,t) yvelk1 = h*yvel(x,y,t) xposk2 = h*xpos(x+(0.5*xposk1),y+(0.5*yposk1),t+(0.5*h)) yposk2 = h*ypos(x+(0.5*xposk1),y+(0.5*yposk1),t+(0.5*h)) xvelk2 = h*xvel(x+(0.5*xvelk1),y+(0.5*yvelk1),t+(0.5*h))

Simulation with RK4, update ODE variable as the simulation goes

守給你的承諾、 提交于 2021-02-11 13:40:56
问题 Problem : I’m currently making a python app that simulates a set of coupled ordinary differentials equations that depends on a variable, let’s call it « X ». As for now, I’m basically simulating this set of ODE with RK4 for given time then I’m plotting the graph on an animated plot with « matplotlib animation » embedded in tkinter. The fact is that I would like to be able to modify « X » as the equations are resolved so that the simulation can change as we modify this variable. Context : The

Julia challenge - FitzHugh–Nagumo model PDE Runge-Kutta solver

↘锁芯ラ 提交于 2021-02-11 11:52:59
问题 I am newbie in Julia programming language, so I don't know much of how to optimize a code. I have heard that Julia should be faster in comparison to Python, but I've written a simple Julia code for solving the FitzHugh–Nagumo model , and it doesn't seems to be faster than Python. The FitzHugh–Nagumo model equations are: function FHN_equation(u,v,a0,a1,d,eps,dx) u_t = u - u.^3 - v + laplacian(u,dx) v_t = eps.*(u - a1 * v - a0) + d*laplacian(v,dx) return u_t, v_t end where u and v are the

Julia challenge - FitzHugh–Nagumo model PDE Runge-Kutta solver

☆樱花仙子☆ 提交于 2021-02-11 11:52:49
问题 I am newbie in Julia programming language, so I don't know much of how to optimize a code. I have heard that Julia should be faster in comparison to Python, but I've written a simple Julia code for solving the FitzHugh–Nagumo model , and it doesn't seems to be faster than Python. The FitzHugh–Nagumo model equations are: function FHN_equation(u,v,a0,a1,d,eps,dx) u_t = u - u.^3 - v + laplacian(u,dx) v_t = eps.*(u - a1 * v - a0) + d*laplacian(v,dx) return u_t, v_t end where u and v are the

Fourth-order Runge–Kutta method (RK4) collapses after a few iterations

微笑、不失礼 提交于 2021-02-10 18:02:10
问题 I'm trying to solve: x' = 60*x - 0.2*x*y; y' = 0.01*x*y - 100* y; using the fourth-order Runge-Kutta algorithm. Starting points: x(0) = 8000, y(0) = 300 range: [0,15] Here's the complete function: function [xx yy time r] = rk4_m(x,y,step) A = 0; B = 15; h = step; iteration=0; t = tic; xh2 = x; yh2 = y; rr = zeros(floor(15/step)-1,1); xx = zeros(floor(15/step)-1,1); yy = zeros(floor(15/step)-1,1); AA = zeros(1, floor(15/step)-1); while( A < B) A = A+h; iteration = iteration + 1; xx(iteration)

Runge-Kutta 4th order method to solve second-order ODES

限于喜欢 提交于 2021-02-08 02:07:24
问题 I am trying to do a simple example of the harmonic oscillator, which will be solved by Runge-Kutta 4th order method. The second-order ordinary differential equation (ODE) to be solved and the initial conditions are: y'' + y = 0 y(0) = 0 and y'(0) = 1/pi The range is between 0 and 1 and there are 100 steps. I separated my 2nd order ODE in two first-order ODEs, using u as auxiliary variable: y' = u u' = -y The analytical solution is sinusoidal y(x) = (1/pi)^2 sin(pi*x). My Python code is below:

Runge-Kutta 4th order method to solve second-order ODES

夙愿已清 提交于 2021-02-08 02:03:43
问题 I am trying to do a simple example of the harmonic oscillator, which will be solved by Runge-Kutta 4th order method. The second-order ordinary differential equation (ODE) to be solved and the initial conditions are: y'' + y = 0 y(0) = 0 and y'(0) = 1/pi The range is between 0 and 1 and there are 100 steps. I separated my 2nd order ODE in two first-order ODEs, using u as auxiliary variable: y' = u u' = -y The analytical solution is sinusoidal y(x) = (1/pi)^2 sin(pi*x). My Python code is below:

Plot orbit of two body using rk4

元气小坏坏 提交于 2021-01-29 11:10:26
问题 I've been trying to solve the two body problem using rk4 in python. I have some problem with the plot however and can't seem to get an orbit instead it's just a slightly bent line. I've tried to change the stepsize and initial values but it hasn't gotten me anywhere. Here is my code of the sun and earth system. from __future__ import division import numpy as np from numpy import linalg as LA import matplotlib.pyplot as plt #from VPython import * #from VPython.graph import * AU=1.5e11 a=AU

Runge-Kutta 4th order to solve 2nd order ODE system using Python

旧街凉风 提交于 2021-01-29 07:44:25
问题 I'm trying to solve system of two odes numerically by runge-kutta 4th order method. initial system: system to solve: And I have very strange solution graph... I have: Correct graph: I can't find trouble in my runge-kutta. Please, help me. My code is here: dt = 0.04 #initial conditions t.append(0) zdot.append(0) z.append(A) thetadot.append(0) theta.append(B) #derrive functions def zdotdot(z_cur, theta_cur): return -omega_z * z_cur - epsilon / 2 / m * theta_cur def thetadotdot(z_cur, theta_cur)

How to create a Single Vector having 2 Dimensions?

不羁的心 提交于 2021-01-28 12:09:28
问题 I have used the Equation of Motion (Newtons Law) for a simple spring and mass scenario incorporating it into the given 2nd ODE equation y" + (k/m)x = 0; y(0) = 3; y'(0) = 0. I have then been able to run a code that calculates and compares the Exact Solution with the Runge-Kutta Method Solution. It works fine...however, I have recently been asked not to separate my values of 'x' and 'v', but use a single vector 'x' that has two dimensions ( i.e. 'x' and 'v' can be handled by x(1) and x(2) ).