Solve ordinary differential equations using SciPy - python

I have a following
ordinary differential equation
and numeric parameters Sigma=0.4, x(0) = 4 and dx(0)/dt = 0
My task is to get Cauchy problem solution (Initial value problem solution) of differential equation using ode function
Can someone help me? I don't even know how to write equation and especially numeric parameters in correct way for SciPy.
P.S. Sorry for not posting images, I've just registered.

Like Warren said, scipy.integrate.odeint is the 'SciPy' way to solve this.
But before you take your problem to SciPy (or whatever solver you end up using) you'll want to convert your 2nd order ODE to a first order ODE using something like: http://tutorial.math.lamar.edu/Classes/DE/SystemsDE.aspx
To get things into SciPy you need to get your equation looking like:
y' = f(y)
But right now your equation is written like:
y'' = f(y, y')
The solution is to add more variables to your system, but the link will explain it more thoroughly.

Related

Solution to nonlinear differential equation with non-constant mass matrix

If I have a system of nonlinear ordinary differential equations, M(t,y) y' = F(t,y), what is the best method of solution when my mass matrix M is sometimes singular?
I'm working with the following system of equations:
If t=0, this reduces to a differential algebraic equation. However, even if we restrict t>0, this becomes a differential algebraic equation whenever y4=0, which I cannot set a domain restriction to avoid (and is an integral part of the system I am trying to model). My only previous exposure to DAEs is when an entire row is 0 -- but in this case my mass matrix is not always singular.
What is the best way to implement this numerically?
So far, I've tried using Python where I add a small number (0.0001) to the main diagonals of M and invert it, solving the equations y' = M^{-1}(t,y) F(t,y). However, this seems prone to instabilities, and I'm unsure if this is a universally appropriate means of regularization.
Python doesn't have any built-in functions to deal with mass matrices, so I've also tried coding this in Julia. However, DifferentialEquations.jl states explicitly that "Non-constant mass matrices are not directly supported: users are advised to transform their problem through substitution to a DAE with constant mass matrices."
I'm at a loss on how to accomplish this. Any insights on how to do this substitution or a better way to solve this type of problem would be greatly appreciated.
The following transformation leads to a constant mass matrix:
.
You need to handle the case of y_4 = 0 separately.

How to solve numerically a nonlinear ODE (a BVP) with singular points in python?

So, I have to solve numerically the following ODE y''+f(y)*(y')^2 = 0: ODE
originally between [y_i,y_f] where y = y(t) and y(0) = y_i. The main problem is that the function f(y) has a (regular) singular point (couldn't post the image).
Also, f(y) is obtained from long numerical calculations, so I have no analytical expression for it. The issue is that the singular point lies between y_i and y_f. So far, I could not find any method which helps me to solve this kind of problem. It doesn't matter if it can be solved as a BVP o IVP, but I need to cross the singularity.
What I have tried:
I approximated f(y) as -2/(y-y*), where y* is the singularity, and tried to solved the problem as a IVP using Runge-kutta, odeint and Runge-Kutta-Fehlberg method. But I cannot cross it.
I tried to cheat using RKF, as y tends to be constant I manually increased it, forcing it to cross the singular point, but then the solution increase to infinity, so not a valid solution.
Same as before, but using the numerically defined function f(y), not its approx.
The same but as BVP using the shooting method and solve_bvp from Python.
You can immediately integrate after dividing by y' to get ln(y')+F(y)=c or y'*exp(F(y))=C, F'=f, so that in principle you could compute t(y) via simple quadrature, and then get the solution y(t) via inversion of the function value table.
Your example integrates to y'=C*(y-y*)^2, y(t)=y(0)/(1-C*y(0)*t), which could also lead to a singularity in the solution formula due to the denominator. This means that even if a solution exists, you will have to start quite close to it, else the solution process might have to cross a surface of singular Jacobians or other impossibilities, and will fail to continue at this point.

Is there a way to solve yB = c without computing the right inverse?

I would like to solve an equation of the form yB = c, where y is my unknown (possibly a matrix). However the B matrix is not well conditioned, and I would like to have a method similar to numpy.linalg.solve in order to maintain the numerical accuracy of the solution.
I have tried to simply use the inverse of B, with numpy.linalg.inv, to find the solution y = cB^-1 as well as using the pseudo-inverse (numpy.linalg.pinv), but they prooved to be not accurate enough...
I have also looked into the QR decomposition, since numpy provides the method for it, in order to adapt it to the right inverse case, but here I struggle with the algebra.
Is there an accurate way to solve this equation ? Or is there an equivalent to numpy.linalg.solve for the right inverse ?
You can transpose the equation and then use linalg.solve.

Sympy Cannot create mpf

So, I'm trying to solve this nonlinear system of equations and I am getting the error "cannot create mpf". Here's the sympy link . Anyone know how to solve thsi?
mpmath is made for doing numerical calculations and l is a symbol, thus the error (you used an mpmath tanh in your second attempt, but SymPy's tanh in the first example). You already have the solution for k from your first attempt. Solving for l requires solving a 12th order polynomial in exp(1.401*l) which you won't be able to do symbolically in general.

Solve N^2 nonlinear equations system

I am trying to solve a system of N*N nonlinear equations, but I get stuck and do not understand what is the problem.
My equations are :
h_{j,i} = (T/2) \sum_{k=1..N}{f(h_{k,j})} - (T/2) f(h_{i,j})
for i and j in [1..N]^2, and where the h are the unknowns, f is a known function and T is a parameter.
In all the examples I have found, there are maybe two or three equations/unknowns, so one can implement the equations directly. Nevertheless, I have too many equations here, and I do not understand how to implement a code without explicitly writing all the equations, and using fsolve (on python).
Thanks for your help

Categories

Resources