I know R an Python, could you help me find a simple technique to plot the solutions of a system of differential equations?(In this case a Lagrangian)
For example, I have this system:
$$ kx=mx''+ml\theta''\cos(\theta)-ml\theta'^2\sin(\theta)\\ -mlx'\theta'\sin(\theta)-mgl\sin(\theta)+k\frac{l}{2}(l+\frac{l}{2}\cos(\theta))\sin(\theta)=ml^2\theta''+mlx''\cos(\theta)-mlx'\theta'\sin(\theta)+\frac{m}{8}l^2\theta'' $$ Where x and $\theta$ are my variables, and the others letters are parameters.
This is the solution I found from an exercise I'm doing, but I don't know if I'm correct because I don't have the solutions, so, maybe visualising the graph I understand.
Thank you.
If you can figure out Scipy and matplotlib, then you can solve and plot ODEs with the least amount of effort. Use the integrate function from Scipy to solve the system and matplotlib to plot it. However, it's also possible to achieve what you want without installing anything new, so I'll describe that procedure here. Regardless of which method you use, we still have to put the DEs into a more suitable form. Specifically, we must convert the system of two coupled second-order equations into four coupled first-order equations (you can always do this.)
The first step is to solve each of the equations for $x''$ and $\theta''$, note that we can isolate $\theta ''$ by solving for $x''$ in the first equation and eliminating it from the second. This will give $\theta ''$ and $x''$ as (messy) functions of $x, \theta, x'$ and $\theta'$. Now we make the substitutions $y_{1} = x, y_{2} = \theta, y_{3} = x', y_{4} = \theta '$. Note that we have $y_{1}' = y_{3}$ and $y_{2}' = y_{4}$. Furthermore, $y_{3}' = x''$ and $y_{4}' = \theta ''$. But since we just solved for $x''$ and $\theta''$, we can use those here to get $y_{3}'$ and $y_{4}'$ as functions of $y_{1-4}$. The net result is that we have a four dimensional vector $\vec{y}$ whose first derivative $\vec{y}\,'$ we know. See Example $1$ here for an example of this procedure being carried out explicitly.
Now that we have a first-order system, we can use an integration scheme to solve it. Fourth-order Runge-Kutta is easy to use; here is a quick Python script I wrote that implements it:
This example solve two simultaneous simple harmonic oscillator problem simultaneously. For your plot, you'll need to modify the expressions for the derivatives in the function
derivativesto what I described earlier. Furthermore, you'll have to set the initial conditions. RunningRK4()generates a file calledfile.txtwhich contains a list of points with column names. Next, enter the following commands into $R$ to make a plot (you'll have to set the working directory to point to the location offile.txt):data <- read.csv(file="file.txt",head=TRUE,sep=",")plot(data$t, data$x)orplot(data$t, data$theta)This should generate the required plots.