I don't really know where to start with this problem, as I haven't had much experience with this but it is required to solve this part of the project using a computer.
I have a 2nd order ODE which is:
m = 1220
k = 35600
g = 17.5
a = 450000
and b is between 1000 and 10000 with increments of 500.
x(0)= 0
x'(0)= 5
m*x''(t) + b*x'(t) + k*x(t)+a*(x(t))^3 = -m*g
I need to find the smallest b such that the solution is never positive. I know what the graph should look like, but I just don't know how to use odeint to get a solution to the differential equation. This is the code I have so far:
from numpy import *
from matplotlib.pylab import *
from scipy.integrate import odeint
m = 1220.0
k = 35600.0
g = 17.5
a = 450000.0
x0= [0.0,5.0]
b = 1000
tmax = 10
dt = 0.01
def fun(x, t):
return (b*x[1]-k*x[0]-a*(x[0]**3)-m*g)*(1.0/m)
t_rk = arange(0,tmax,dt)
sol = odeint(fun, x0, t_rk)
plot(t_rk,sol)
show()
Which doesn't really produce much of anything.
Any thoughts? Thanks
To solve a second-order ODE using
scipy.integrate.odeint
, you should write it as a system of first-order ODEs:I'll define
z = [x', x]
, thenz' = [x'', x']
, and that's your system! Of course, you have to plug in your real relations:becomes:
Or, just call it
d(z)
:Now you can feed it to the
odeint
as such:(The
_
is a blank placeholder for thex'
variable we made)In order to minimize
b
subject to the constraint that the maximum ofx
is always negative, you can usescipy.optimize.minimize
. I'll implement it by actually maximizing the maximum ofx
, subject to the constraint that it remains negative, because I can't think of how to minimize a parameter without being able to invert the function.I don't think you can solve your problem as stated: your initial conditions, with
x = 0
andx' > 0
imply that the solution will be positive for some values very close to the starting point. So there is nob
for which the solution is never positive...Leaving that aside, to solve a second order differential equation, you first need to rewrite it as a system of two first order differential equations. Defining
y = x'
we can rewrite your single equation as:So your function should look something like this:
And you can solve and plot your equations doing: