I have a problem that might be a little more mathematical but the crux is that I want ot solve it in python and plot it. I have three ODEs which are related to one antoher in the following way:
x''(t)=b*x'(t)+c*y'(t)+d*z'(t)+e*z(t)+f*y(t)+g*x(t)
y''(t)=q*x'(t)+h*y'(t)+i*z'(t)+p*z(t)+l*y(t)+m*x(t)
z''(t)=a*x'(t)+w*y'(t)+v*z'(t)+u*z(t)+o*y(t)+n*x(t)
How would I solve them in order to plot them in a 3d graph, through their acceleration. I know, that I have to solve them, the difficulty lies in the problem that they are second order ODEs and interlinked through their dependency onone antoher.
For some additional Info, here is the code (it doesnt really work that good feel free to try it in a different way):
from sympy import symbols, Function, Eq
import numpy as np
from scipy.integrate import solve_ivp
t = symbols('t')
x = Function('x')(t)
y = Function('y')(t)
z = Function('z')(t)
b, c, d, e, f, g = symbols('b c d e f g')
q, h, i, p, l, m = symbols('q h i p l m')
a, w, v, u, o, n = symbols('a w v u o n')
eqx = b*x.diff(t) + c*y.diff(t) + d*z.diff(t) + e*z + f*y + g*x
eqy = q*x.diff(t) + h*y.diff(t) + i*z.diff(t) + p*z + l*y + m*x
eqz = a*x.diff(t) + w*y.diff(t) + v*z.diff(t) + u*z + o*y + n*x
#First I replaced the derivatives to turn it into a first order ODE
eqx = eqx.subs({ sp.Derivative(y,t):dy, sp.Derivative(z,t):dz, sp.Derivative(x,t):dx,})
eqy = eqy.subs({ sp.Derivative(y,t):dy, sp.Derivative(z,t):dz, sp.Derivative(x,t):dx,})
eqz = eqz.subs({ sp.Derivative(y,t):dy, sp.Derivative(z,t):dz, sp.Derivative(x,t):dx,})
#to solve them nummerically I started to lambdify them:
freex = eqx.free_symbols
freey = eqy.free_symbols
freez = eqz.free_symbols
Xl = sp.lambdify(freex , eqx , 'numpy')
Yl = sp.lambdify(freey , eqy , 'numpy')
Zl = sp.lambdify(freez , eqz , 'numpy')
#from here on I tried to solve them, but had trouble with the dependencies and the arguments
#(so here is only the line for x)
Xo = [0]
ExampleValues = np.array([0, 0, 0, 1, 9, 0, 1, 2, 4])
space= np.linspace(0, 10, 50)
Solx = solve_ivp(XSL, (0, 10), Xo, t_eval=sace, args=ExampleValues)
Thanks for your answer in advance!
Since
XSLis not defined, I can't be sure what the problem was, but here is a working version of your code (with commentary to describe the changes).(Note that that the use of
FunctionandDerivativewere not really needed here; this would work just as well defining the second derivatives in terms of regular symbolsx,y,z,dx,dy, anddz. I've left that part alone, though. Perhaps you needed to start withFunctions if you are deriving the real equations with SymPy.)