ode

Difficulty running an ODE model in R including a parameter that varies by time (forcing function)

风格不统一 提交于 2021-01-28 14:22:16
问题 I am trying to fit a reasonably basic ODE model using deSolve, including within the model a parameter that varies by time (force of infection; FOI). While running the model without this parameter works fine, inclusion of the time dependent parameter gives an error (see below). I am relatively new to R and mathematical modelling, and have been trying to solve this problem for some time now. I have created the FOI parameter as a matrix of values and then used the approxfun function for

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) ).

Solving set of Boundary Value Problems

点点圈 提交于 2021-01-28 11:26:25
问题 I am trying to solve a set of boundary value problems given by 4 differential equations. I am using bvp_solver in python, and I am getting errors which state 'invalid value encountered in division'. I am assuming this means I am dividing by NaN or 0 at some point, but I am unsure where. import numpy as np from scipy.integrate import solve_bvp import matplotlib.pyplot as plt %matplotlib inline alpha = 1 zeta = 1 C_k = 1 sigma = 1 Q = 30 U_0 = 0.1 gamma = 5/3 theta = 3 m = 1.5 def fun(x, y): U,

Fit differential equation with scipy

不问归期 提交于 2021-01-27 18:37:29
问题 how can I fit the differential function of the followint scipy tutorial Scipy Differential Equation Tutorial? In the end I want to fit some datapoints that follow a set of two differential equations with six parameters in total but I'd like to start with an easy example. So far I tried the functions scipy.optimize.curve_fit and scipy.optimize.leastsq but I did not get anywhere. So this is how far I came: import numpy as np import scipy.optimize as scopt import scipy.integrate as scint import

How do I use pylab to plot a phase plane for pendulum motion?

折月煮酒 提交于 2021-01-27 05:21:25
问题 I have code that will work for plotting the following predator prey model: dx/dt = x − xy, dy/dt = −y + xy from pylab import * xvalues, yvalues = meshgrid(arange(0, 3, 0.1), arange(0, 3, 0.1)) xdot = xvalues - xvalues * yvalues ydot = - yvalues + xvalues * yvalues streamplot(xvalues, yvalues, xdot, ydot) show() But I am not sure how to use these functions to draw a phase plane (using streamplot) to model pendulum motion, defined as d^2θ/dt^2 = (−g/L)sin(θ) How can I implement this model to

Understanding scipy integrate's internal behavior

允我心安 提交于 2020-12-27 05:34:56
问题 I am trying to understand what scipy.integrate is doing internally. Namely, it seems that something weird and inconsistent is happening. How get it working properly? I need it to perform one integration step at a time, because I do some stuff with t inside the ODE and need it to be consistent So, here is my MWE import numpy as np from scipy.integrate import ode t0 = 0 t1 = 1 def myODE(t, x): print('INTERNAL t = {time:2.3f}'.format(time=t)) Dx = np.zeros([2, 1]) Dx[0] = -x[0]**2 Dx[1] = -x[1]*

Understanding scipy integrate's internal behavior

戏子无情 提交于 2020-12-27 05:33:12
问题 I am trying to understand what scipy.integrate is doing internally. Namely, it seems that something weird and inconsistent is happening. How get it working properly? I need it to perform one integration step at a time, because I do some stuff with t inside the ODE and need it to be consistent So, here is my MWE import numpy as np from scipy.integrate import ode t0 = 0 t1 = 1 def myODE(t, x): print('INTERNAL t = {time:2.3f}'.format(time=t)) Dx = np.zeros([2, 1]) Dx[0] = -x[0]**2 Dx[1] = -x[1]*

Rk4 Integrator of a nonlinear second order ODE in Python

和自甴很熟 提交于 2020-12-26 20:02:25
问题 I'm in a project in my University and I have to implement the Runge-Kutta 4-order integrator using Python. I know I can use for example Sympy, but the goal here is implement the method, the code is ready written in Fortran language, so basically I have a data base with the correct values of solutions and I have to get similar solutions in my code. However, we have some problems; I did the same several times using linear equations ( first and second order ), however this is a second order

Rk4 Integrator of a nonlinear second order ODE in Python

给你一囗甜甜゛ 提交于 2020-12-26 19:58:20
问题 I'm in a project in my University and I have to implement the Runge-Kutta 4-order integrator using Python. I know I can use for example Sympy, but the goal here is implement the method, the code is ready written in Fortran language, so basically I have a data base with the correct values of solutions and I have to get similar solutions in my code. However, we have some problems; I did the same several times using linear equations ( first and second order ), however this is a second order