ode

Error in RK4 algorithm in Python

巧了我就是萌 提交于 2019-12-17 20:31:31
问题 I am solving a nonlinear Schrodinger (NLS) equation: (1): i*u_t + 0.5*u_xx + abs(u)^2 * u = 0 after applying Fourier Transform, it becomes: (2): uhat_t = -0.5*i*k^2 * uhat + i * fft(abs(u)^2 * u) where uhat is the Fourier Transform of u . The equation (2) above is a pretty stated IVP, which can be solved by the 4th oder Runge-Kutta method. Here are my code for solving equation (2): import numpy as np import math from matplotlib import pyplot as plt from matplotlib import animation #-----

Solving a system of odes (with changing constant!) using scipy.integrate.odeint?

时光总嘲笑我的痴心妄想 提交于 2019-12-17 18:58:46
问题 I currently have a system of odes with a time-dependent constant. E.g. def fun(u, t, a, b, c): x = u[0] y = u[1] z = u[2] dx_dt = a * x + y * z dy_dt = b * (y-z) dz_dt = -x*y+c*y-z return [dx_dt, dy_dt, dz_dt] The constants are "a", "b" and "c". I currently have a list of "a"s for every time-step which I would like to insert at every time-step, when using the scipy ode solver...is this possible? Thanks! 回答1: Yes, this is possible. In the case where a is constant, I guess you called scipy

MATLAB: Is it possible to have two event values whilst using ode45?

感情迁移 提交于 2019-12-17 06:18:08
问题 I want two limitations to my ode45 calculation of a movement equation: position and time. I have already got the time event to work but I am not sure if and how I can add another event for limiting the position. EDIT: I also have many different particles coupled together in one ODE equation and need them to stop individually once they reach a 'roof' as they all travel at different speeds... would I be able to achieve this through events? I have an idea on how I would do this but its very

ValueError: too many values to unpack (expected 3)?

这一生的挚爱 提交于 2019-12-14 03:35:13
问题 I have been having issues with the code I am trying to right with the model I am trying to code the following error has appeared and being a relative novice I am unsure of how to resolve it. ValueError Traceback (most recent call last) <ipython-input-2-5f21a0ce8185> in <module>() 26 proposed[j] = proposed[j] + np.random.normal(0,propsigma[j]) 27 if (proposed[j]>0): # automatically reject moves if proposed parameter <=0 ---> 28 alpha = np.exp(logistic_loglik(proposed,time,ExRatio,sig)-logistic

Solving an ODE with ode45 after interpolating

依然范特西╮ 提交于 2019-12-14 03:18:29
问题 I am working with the following code and can't find what is wrong: xx = 0:1/50:1; v = 3.*exp(-xx)-0.4*xx; xq = xx; vq = @(xq) interp1(xx,v,xq); tspan = 0:1/50:1; x0 = 3; [~, y2] = ode45(@(t,x)vq(x), tspan, x0); I get that y2 = [3;NAN;NAN;NAN,.....] . Yet, when I plot both equations before calling ode45 , I get that they are equal, which is not a surprise. When I compute: f = @(t,r) 3.*exp(-r)-0.4*r; [~, y] = ode45(f,tspan,x0); it works fine. But I need to show that I can get the same results

Python ode first order, how to solve this using Sympy

断了今生、忘了曾经 提交于 2019-12-13 09:01:35
问题 When I try to solve this first ode by using Sympy as it shows below: import sympy y = sympy.Function('y') t = sympy.Symbol('t') ode = sympy.Eq(y(t).diff(t),(1/y(t))*sympy.sin(t)) sol = sympy.dsolve(ode,y(t)) csol=sol.subs([(t,0),(y(0),-4)]) # the I.C. is y(0) = 1 ode_sol= sol.subs([(csol.rhs,csol.lhs)]) print(sympy.pprint(ode_sol)) It gives me this error: Traceback (most recent call last): File "C:/Users/Mohammed Alotaibi/AppData/Local/Programs/Python/Python35/ODE2.py", line 26, in <module>

Vectorized SciPy ode solver

て烟熏妆下的殇ゞ 提交于 2019-12-13 04:38:59
问题 My question is with respect to the current scipy ode solver. From the scipy doc page, their usage is: # A problem to integrate and the corresponding jacobian: from scipy.integrate import ode y0, t0 = [1.0j, 2.0], 0 def f(t, y, arg1): return [1j*arg1*y[0] + y[1], -arg1*y[1]**2] def jac(t, y, arg1): return [[1j*arg1, 1], [0, -arg1*2*y[1]]] # The integration: r = ode(f, jac).set_integrator('zvode', method='bdf', with_jacobian=True) r.set_initial_value(y0, t0).set_f_params(2.0).set_jac_params(2.0

How to fix TypeError: in setindex! in DifferentialEquations.jl

跟風遠走 提交于 2019-12-13 03:57:19
问题 Recently, I got started with Julia's (v1.0.3) DifferentialEquations.jl package. I tried solving a simple ODE system, with the same structure as my real model, but much smaller. Depending on the solver which I use, the example either solves or throws an error. Consider this MWE, a Chemical Engineering model of a consecutive / parallel reaction in a CSTR: using DifferentialEquations using Plots # Modeling a consecutive / parallel reaction in a CSTR # A --> 2B --> C, C --> 2B, B --> D # PETERSEN

C++ class member function to GSL ODE solver

梦想的初衷 提交于 2019-12-13 03:07:30
问题 I am struggling with -- apparently -- a well known problem. I have an ODE system which is defined by a class member function, and I want to solve/integrate it by one of the GSL solvers. I.e., say my model is defined by: class my_model{ ... public: int NEQ = 4; double y[4], dydt[4]; double params[25]; int ode_gsl(double t, const double y[], double dydt[], void * params); ... }; int my_model::int ode_gsl(double t, const double y[], double dydt[], void * params){ dydt[0] = params[1]*params[0]*y

solve second order ODE in MATLAB/SIMULINK

自闭症网瘾萝莉.ら 提交于 2019-12-12 21:37:50
问题 I don't know how to solve this second order ODE in SIMULINK: I rewrote it to the system of first order ODEs: then giving My SIMULINK blocks are here: giving this Scope: This is the plot of symbolic solution from dsolve : It looks like the functions (plots) from symbolic and SIMULINK are little similar. 回答1: The solution was found by Phil Goddard . His answer in comments: In Simulink you are plotting y_dot, while the symbolic solution is a plot of y. So the problem was that Scope was plotting