Tuesday, June 7, 2016

Maxima - Differential Equation

Solving differential equations can be very tricky when doing it analytically, it's the same for a mathematical application as Maxima, which can't solve differential equations which have an order higher than 2. There is also a big complexity to solve partial differential equations. But when you lead to a differential equation which can't be solved analytically, you always have the numerical methods as Runge-Kutta. You must take into account that the mathematical applications will mainly be used to obtain numerical as it's easiest to compute, and at the end you're more often looking for a numerical value.
Let's take a look over the differential equations functions: ode2() to solve differential equations of order 1 or 2; ic1() to solve differential equations of order 1 with initial values; ic2() to solve differential equations of order 2 with initial values; and bc2() to solve differential equations of order 2 with boundary values. Well, the functions exists, but how to use them? ic1(), ic2() and bc2() have as inputs a general solution given by ode2(). This means that the amount of equations which can be solved is reduced. But we always can use some analytics way to transform a higher order differential equation into a differential equation of order 2 with a separation of variables. But this leads that the differential equations which can be solved are only the one which have initial values (not the boundaries values).
Let's try to see all the resume into a single exercise:
Solve numerically for 0 < t < 50 this system of differential equations:
 
Here are the initial values:
 
For the Runge-Kutta method, take steps as 0.1.
As we want to use the Runge-Kutta method as the numerical method to obtain all the data of the given equations, we need to have a system of differential equation of order 1. To do so, we will do a change of variables introducing u(t) and v(t) as follows:


Let's see each lines in details. The first two inputs are the given differential equations written as f(x, y) = 0. In the third input, we create a list with all the equations, where we will add the two new definitions of u(t) and v(t) later. In the forth and sixth, we define the u(t) and v(t) as the derivatives of the functions x(t) and y(t) respectively. And in the fifth and seventh inputs, we make the substitution of the new functions in our differential equations. Now we're with system of differential equations of order 1.
Then we prepare the inputs for the rk() function.


In the input 8, we add the definition of u(t) and v(t) to our list of differential equations. We add them as the previous, as f(x, y) = 0. In the input 9, we sum our list of differential equations with derivatives of each dependent variable, having this way the prepared input for the Runge-Kutta function. And in the input 10, we substitute the variables without the term t (the (t) was necessary for the derivatives functions, as it needs to know who depends on who). We build the missing inputs for the Runge-Kutta function in the inputs 11, 12 and 13 as the list of the dependent values, the list of the initial conditions, and a list with the independent variable and its range. Then we call the function rk() and store its output.
Now we can get the results to plot them:

First we apply the matrix function to the rk() output, then, transposing the matrix, we can get the independent values for each variable, t, x, y, u or v. 
Let's see now some graphs. First at all, the two initial differential equations are from a system of 2 coupled harmonic oscillators. So, if we plot x vs t and y vs t we can see the position of the 2 harmonic oscillators:
Another interesting graph, is if we plot y vs x, which is known as the Lissajous figure:
And finally, we can plot the speeds vs the positions (u vs x and v vs y):
This is also known as the phase diagram of the system.










10 comments:

  1. Hola, buenas noches.. estoy tratando de resolver un sistema de ecuaciones diferenciales en Maxima, pero me aparece un error. Por favor podría darme una idea de a qué se refiere ese error? Gracias.. copio en seguida lo que he realizado y el error aparece al final.

    (%i4) B:5;
    k1:0.1;
    k2:0.05;
    k3:0.08;

    (B) 5
    (k1) 0.1
    (k2) 0.05
    (k3) 0.08
    (%i5) eq1:'-diff(C(t),t,1)+k1*B-k2*Z(t)*C(t);
    (eq1) mminus('diff(C(t),t,1))-0.05*C(t)*Z(t)+0.5
    (%i6) eq2:'-diff(D(t),t,1)+k2*Z(t)*C(t);
    (eq2) mminus('diff(D(t),t,1))+0.05*C(t)*Z(t)
    (%i7) eq3:'-diff(Z(t),t,1)-k2*Z(t)*C(t)-k3*Z(t);
    (eq3) mminus('diff(Z(t),t,1))-0.05*C(t)*Z(t)-0.08*Z(t)
    (%i8) eq4:'-diff(P(t),t,1)+k3*Z(t);
    (eq4) mminus('diff(P(t),t,1))+0.08*Z(t)
    (%i9) eqs:[eq1,eq2,eq3,eq4];
    (eqs) [mminus('diff(C(t),t,1))-0.05*C(t)*Z(t)+0.5,mminus('diff(D(t),t,1))+0.05*C(t)*Z(t),mminus('diff(Z(t),t,1))-0.05*C(t)*Z(t)-0.08*Z(t),mminus('diff(P(t),t,1))+0.08*Z(t)]
    (%i21) dependent_variables:'[C(t),D(t),Z(t),P(t)];
    (dependent_variables) [C(t),D(t),Z(t),P(t)]
    (%i11) initial_conditions:[0,0,1,0];
    (initial_conditions) [0,0,1,0]
    (%i12) t_range:[t,0,50,0.1];
    (t_range) [t,0,50,0.1]
    (%i20) data: rk(eqs,dependent_variables,initial_conditions,t_range);
    rk: variable name expected; found: C(t)
    -- an error. To debug this try: debugmode(true);

    ReplyDelete
    Replies
    1. Hola, para empezar, he visto que la declaración de las ecuaciones empieza con una tilde adicional (igual que la declaración de las variables independientes).
      Pero el problema principal, es que cuando defines las ecuaciones, usas "C(t)" dentro y fuera de la función "diff" pero deberías usar "C(t)" dentre de la función "diff" y "C" fuera, así:
      eq1: -diff(C(t), t, 1) + k1 * B - k2 * Z * C;

      Es así, puesto que defines la dependencia de C con t dentro de la función "diff" y fuera, los paréntesis ya tienen otro significado: order de ejecución de las operaciones.
      Espero que se sea de ayuda,
      Un saludo

      Delete
    2. Muchas gracias por las aclaraciones. Modifiqué lo que me recomendaste, aunque aún no soluciono. Me aparece otro error:
      apply: found C evaluates to 0.0 where a function was expected.
      apply: found D evaluates to 0.0 where a function was expected.
      apply: found Z evaluates to 1.0 where a function was expected.
      apply: found P evaluates to 0.0 where a function was expected.
      Veo que no reconoce las variables. Seguiré revisando. Muchas gracias.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Hola, el objetivo del blog es que pueda ayudar a mas personas con una explicación genérica. Si te resuelvo las dudas por mensajería privada, no será de ayuda a nadie más que tenga un problema similar. Trata de explicarlo por partes, o trata de exponer la parte que te resulte compleja y haré lo que pueda para serte de ayuda

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Hola, entiendo la explicación del ejercicio, pero cuál es tu pregunta?
    El objetivo es que hayas hecho por tu parte todo lo posible, pero que un paso sea más complicado y necesites ayuda u orientación, no que yo te diga la solución de todo el problema.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete