Vous êtes sur la page 1sur 64

Solution to Ordinary Differential Equations using Maxima

In this chapter we present examples of solutions to ordinary differential equations (ODes) using Maxima. Review of ODE solution methods from the wxMaxima interface In the introduction of menu options and interface buttons for the wxMaxima interface in previous chapters, we came across some simple examples of ODE solutions including general solutions, initial value problems, and boundary value. We review them next. Function ode2 (Equations > Solve ODE or [Solve ODE...] button) General solution to ODE In Chapter 1 (Introduction to Maxima) we introduced a simple ODE solution when illustrating the use of the [ Solve ODE...] button in the wxMaxima interface. This solution is repeated here. Press the [ Solve ODE...] button, or use the menu item Equations > Solve ODE ... in the wxMaxima interface, and try the following input:

Press the [ OK ] button to obtain:

The [ Solve ODE... ] button can be used, therefore, to activate function ode2 to produce the general solution of first and second order ODEs. For example, the 2nd order ODE:
d2 y y =sin 3x , 2 dx

can be solved by using the input:

9-1

(c) Gilberto Urroz / Luigi Marino - 2009

The result is the following:

The constant of integration in the first solution is referred to as %c, while the two constants of integration in the second example are %k1 and %k2. An attempt to find a general solution to a third-order ODE fails:

This input produces a false result:

Function ic1 - Solving an initial value, first-order ODE problem To solve an initial value, first-order ODE problem, you can use the menu item Equations > Initial value problem (1), after you have used function ode2 (see above) to produce a general solution. The procedure, thus, requires two steps as illustrated in this example. Solve the ODE
dy y= x dx

subject to the initial condition y(0) = 1. First, use Equations > Solve ODE... to produce the general solution:

9-2

(c) Gilberto Urroz / Luigi Marino - 2009

which produces:

Next, use Equations > Initial Value Problem(1) :

which produces:

Alternatively, you could just enter the two inputs directly into an input cell in the wxMaxima 0.8.2 interface, e.g.,

Attempting the solution of a second-order ODE followed by an initial value evaluation produces an error, e.g.,

9-3

(c) Gilberto Urroz / Luigi Marino - 2009

Function ic2 - Solving an initial value, second-order ODE problem The solution to an initial value, second-order ODE problem requires two initial conditions at the initial point, one is a value of the function, and the second one is a value of the derivative at the same initial point. To solve an initial value, second-order ODE problem in Maxima, first solve the general solution, and then use the menu option Equations > Initial Value Problem(2), as illustrated next. Solve the ODE
d 2 y dy y =0 2 dx dx

subject to the initial conditions y(0) = 1, and First, select Equations > Solve ODE... and enter:
dy =1 . dx

This produces:

9-4

(c) Gilberto Urroz / Luigi Marino - 2009

Then, select Equations > initial Value Problem(2) and enter:

The result is now:

Function bc2 - Solving a boundary value, second-order ODE problem To solve a boundary value, second-order ODE problem it is necessary to provide values of the function at two different values of the independent variable. To obtain the solution to a boundary value, second-order ODE problem in Maxima, first obtain a general solution, and then replace the boundary values using the menu item: Equations > Boundary Value Problem ... For example, solve the ODE
d y 1 dy 3y =0 2 2 dx dx
2

subject to the boundary conditions y(0) = 1, and y(5) = -1. First, obtain a general solution by using the menu item Equations > Solve ODE and enter:

9-5

(c) Gilberto Urroz / Luigi Marino - 2009

This produces the general solution:

Next, select the menu item Equations > Boundary Value Problem and enter:

The result is:

Function desolve - Solving an ODE using Laplace transforms The menu item Equations > Solve ODE with Laplace uses Laplace transforms to obtain the general solution of an ODE of any order. The solutions thus provided could be quite involved since they are shown in terms of derivatives of the solution at the initial value of zero for the independent variable. For example:

That is, the solution to the ODE


d3 y y= 0 dx 3

is:

9-6

(c) Gilberto Urroz / Luigi Marino - 2009

Replacing initial conditions If the initial conditions are known, you can replace them into the general solution by using function subst, available, for example, through the [Subst...] button in the wxMaxima interface. However, in order to produce this substitutions you should be able to write expressions for the derivatives at x = 0. For example, to produce the result

use the command:

Thus, if you have produced a general solution such as the one shown above, you can proceed to produced the required substitutions as indicated below. First, produce the solution using the command:

9-7

(c) Gilberto Urroz / Luigi Marino - 2009

Then, substitute the values of the second and first derivatives, and of the function, at x= 0, in that order:

The final result is easier to understand, showing only missing constant such as D2y0, Dy0, and y0, instead of the full expressions for the derivatives. If you have specific values for these constants, you could substitute them at once using, for example:

Using initial conditions with the atvalue function If you want to solve an ODE using Laplace transforms, and provide the initial conditions from the start, rather than substituting them after obtaining the solution, you can load those initial conditions using function atvalue. This function can be obtained by using the menu item Equations > At Value Consider the solution to the second order equation:
dy d y 2 5y =0 2 dx dx
2

9-8

(c) Gilberto Urroz / Luigi Marino - 2009

subject to the initial conditions y(0) = 1, and


dy =1 . dx

To solve this problem we could solve this problem as follows. First, define the ODE:

Then, use function atvalue to provide the initial conditions as listed above:

These two entries can be obtained by using the menu item Equations > At Value..., e.g.,

Finally, use function desolve to obtain the solution to the ODE initial value problem:

which produces:

9-9

(c) Gilberto Urroz / Luigi Marino - 2009

Additional examples of first-order, initial value ODE solutions (*) This section illustrates the use of functions ode2, and ic1, in the solution of 1st-order ODEs. The examples in this section, and in all sections of this chapter whose titles are marked with an asterisk (*), were generously contributed by: Dr. Luigi Marino Professor of Informatics and Mathematics Liceo Camilo Golgi BRENO, Brescia, Italy All the examples shown in this section start with a given differential equation (ODE) and an initial condition y = y0, at x = x0. The general solution consists in using function ode2 to produce the general solution, followed by a call to function ic1 to substitute the initial condition into the solution. Plots of the solution then follow. Example 1 Solve
dy = xy 2 x , subject to the initial conditions: y(0) = 3. The solution dx using wxMaxima, including a plot y(x), is accomplished as follows:

9 - 10

(c) Gilberto Urroz / Luigi Marino - 2009

Notice that the solution can be simplified algebraically by using function expand, namely:

Example 2 - Solve Solution and plot:

dy =cos x y exp sin x , subject to the initial conditions: y(0) = 3. dx

Example 3 - Solve Solution and plot:

dy y = x , subject to the initial conditions: y(1) = 3. dx x

9 - 11

(c) Gilberto Urroz / Luigi Marino - 2009

Example 3 - Solve

dy = y 21 , subject to the initial conditions: y(0) = 0. dx

The solution results in an implicit expression:

9 - 12

(c) Gilberto Urroz / Luigi Marino - 2009

An attempt to solve for y, out of this expression produces:

These can be re-written as:

Now we can solve for y:

and produce a plot of the solution:

9 - 13

(c) Gilberto Urroz / Luigi Marino - 2009

x y dy , subject to the initial conditions: y(0) = 0. Also, solve = dx 2xy the ODE using the initial conditions y(2) = 3. Example 4 - Solve
2 2

First, we obtain a general solution:

This result includes y(x) in an implicit expression. We can use function ic1 to substitute the initial condition y(0) = 0, i.e.,

Solving for y indicates that the solutions are imaginary:

Next, we use function ic1 to substitute the second initial condition proposed, y(2) = 3, i.e.,

Solving for y we now find two real solutions:

9 - 14

(c) Gilberto Urroz / Luigi Marino - 2009

A plot of the positive solution is shown next:

Example 5 - Solve Solution:

dy 4 x y 4 x y = , subject to the initial conditions: y(0) = 1. dx 1 x2

A plot is produced using:

9 - 15

(c) Gilberto Urroz / Luigi Marino - 2009

Example 6 - Solve

dy 2 = y , subject to the initial conditions: y(0) = 0. Also, solve the dx ODE using the initial conditions y(1) = 1.

Solution:

Additional examples of second order, initial value ODE solutions (*) This section illustrates the use of functions ode2, and ic2, in the solution of initial-value, 2nd-order ODEs. The examples in this section were also generously contributed by: Dr. Luigi Marino Professor of Informatics and Mathematics Liceo Camilo Golgi BRENO Brescia, Italy All the examples shown in this section start with a given differential equation (ODE) and an initial conditions y = y0 and diff(y,x) = y0p, at x = x0. The general solution consists in using function ode2 to produce the general solution, followed by a call to function ic2 to substitute the initial condition into the solution. Plots of the solution then follow.

9 - 16

(c) Gilberto Urroz / Luigi Marino - 2009

Example 1 Solve

dy d y 2 2 y =0 , subject to the initial conditions: y = 2, and 2 dx dx

dy =1 , at x = 0. dx Solution:

Example 2 Solve dy =0 , at x = 0. dx Solution:

dy d y =2 y =0 , subject to the initial conditions: y = 1, and 2 dx dx

9 - 17

(c) Gilberto Urroz / Luigi Marino - 2009

Example 3 Solve dy =0 , at x = 0. dx Solution:

dy d y =6 13 y =0 , subject to the initial conditions: y = 4, and 2 dx dx

The plot of the solution is shown next:

9 - 18

(c) Gilberto Urroz / Luigi Marino - 2009

Example 4 Solve dy =0 , at x = 0. dx Solution:

dy d y =6 13 y =0 , subject to the initial conditions: y = 4, and 2 dx dx

A plot of the solution is shown next:

9 - 19

(c) Gilberto Urroz / Luigi Marino - 2009

Example 5 Solve dy =1 , at x = 1. dx Solution:

dy d y 6 8 y =0 , subject to the initial conditions: y = 1, and 2 dx dx

A plot of the solution follows:

9 - 20

(c) Gilberto Urroz / Luigi Marino - 2009

Example 6 Solve dy =2 , at x = 0. dx Solution:

d y dy 6 y = 0 , subject to the initial conditions: y = 4, and 2 dx dx

A plot of the solution is shown next:

9 - 21

(c) Gilberto Urroz / Luigi Marino - 2009

Example 7 Solve

dy d y 10 25 y =0 , subject to the initial conditions: y = 3, and 2 dx dx

dy =3 , at x = 1. dx

Solution:

A plot of the solution is shown next:

9 - 22

(c) Gilberto Urroz / Luigi Marino - 2009

Example 8 Solve

dy d y 2 2 y =0 , subject to the initial conditions: y = 2, and 2 dx dx

dy =1 , at x = 0. dx Solution:

A plot of the solution is shown next:

9 - 23

(c) Gilberto Urroz / Luigi Marino - 2009

Example 9 Solve

d y dy 8 5 y = 0 , subject to the initial conditions: y = 6, and 2 dx dx

dy =9 , at x = 0. dx

Solution:

A plot of the solution follows:

9 - 24

(c) Gilberto Urroz / Luigi Marino - 2009

Example 10 Solve x = 1.

d y = y , subject to the initial conditions: y = 0.5, and 2 dx

dy =1 , at dx

Solution:

A plot of the solution follows:

9 - 25

(c) Gilberto Urroz / Luigi Marino - 2009

Solutions to systems of ODEs(*) Function desolve can be used to solve systems of ODEs, in combination with function atsolve to evaluate initial/boundary conditions. The general procedure consists of the following steps (Use the command kill(all) to clear Maxima's memory): 1 - Define the various differential equations that constitute the system by assigning them to variables, e.g., ode1, ode2, etc., for example: ode1 : 'diff(f(x), x) + f(x) = ; ode2 : 'diff(g(x),x) + g(x) = ; 2 Put together all the equations thus defined into a single variable, e.g., ode_syst : [ ode1, ode2, ... ]; 3 Use function atvalue to load initial/boundary conditions for the ODEs, e.g., atvalue( f(x), x = a, b); atvalue( g(x), x = c, d); 4 Call function desolve with the form: desolve(ode_syst, [f(x),g(x), ]), where f(x), g(x), , etc., are the functions we are solving for in the system of equations. The following examples were provided by Dr. Luigi Marino, Professor of Informatics and Mathematics, Liceo Camilo Golgi BRENO, Brescia, Italy.

9 - 26

(c) Gilberto Urroz / Luigi Marino - 2009

031ode_systems.wxm

1 / 6

Example 1 - A system of two second order ODEs


(%i1) kill(all); (%o0) done (%i1) diff_eq1: 'diff(f(x),x,2) + 'diff(g(x),x) + 3*f(x) = 15*exp(-x);
d d 2

(%o1)
dx

g( x )+
dx 2

f( x )+ 3 f( x )= 15 %e -

(%i2) diff_eq2: 'diff(g(x), x, 2) - 4*'diff(f(x), x) + 3*g(x) = 15*sin(2*x);


d 2 2

(%o2)
dx

g( x )- 4

d x

f( x ) + 3 g( x )= 15 sin( 2 x )

(%i3) ode_syst: [diff_eq1, diff_eq2];


d d 2

(%o3) [
dx

g( x )+
dx 2

f( x )+ 3 f( x )= 15 %e -

2 2

,
dx

g( x )- 4

d x

f( x ) + 3 g( x )= 15 sin( 2 x )]

(%i4) atvalue (f(x), x=0, 35); (%o4) 35 (%i5) atvalue ('diff(f(x),x),x=0, -48); (%o5) - 48 (%i6) atvalue (g(x), x=0, 27); (%o6) 27 (%i7) atvalue ('diff(g(x), x), x=0, -55); (%o7) - 55 (%i8) desolve(ode_syst, [f(x), g(x)]); (%o8) [ f( x )= - 15 sin( 3 x )+ 2 cos( 2 x )+ 30 cos( x )+ 3 %e sin( x )- 3 %e
-x x

, g( x )= 30 cos( 3 x )+ sin( 2 x )- 60

Example 2 - A system of three first-order ODEs


(%i9) kill(all); (%o0) done (%i1) eq1:'diff(f(x),x)=g(x)+m(x)-3*f(x);
d

(%o1)
dx

f( x )= m( x )+ g( x )- 3 f( x )

9 - 27

(c) Gilberto Urroz / Luigi Marino - 2009

031ode_systems.wxm (%i2) eq2:'diff(g(x),x)=f(x)+m(x)-3*g(x);


d

2 / 6

(%o2)
dx

g( x )= m( x )- 3 g( x )+ f( x )

(%i3) eq3:'diff(m(x),x)=f(x)+g(x)-3*m(x);
d

(%o3)
dx

m( x )= - 3 m( x )+ g( x )+ f( x )

(%i4) sist:[eq1,eq2,eq3];
d d d

(%o4) [
dx

f( x )= m( x )+ g( x )- 3 f( x ),
dx

g( x )= m( x )- 3 g( x )+ f( x ),
dx

m( x )= - 3 m( x )+ g( x )+

f( x )] (%i5) atvalue(f(x),x=0,1); (%o5) 1 (%i6) atvalue(g(x),x=0,2); (%o6) 2 (%i7) atvalue(m(x),x=0,-1); (%o7) - 1 (%i8) desolve(sist,[f(x),g(x),m(x)]);
2 %e -x %e -4x 2 %e -x 4 %e -4x 2 %e -x 5 %e -4x

(%o8) [ f( x )=
3

+
3

, g( x )=
3

+
3

, m( x )=
3

(%i9) wxplot2d([(2*%e^(-x))/3+%e^(-4*x)/3,(2*%e^(-x))/3+(4*%e^(-4*x))/3,(2*%e^(-x))/3-(5*%e^([y,-5,5], [gnuplot_preamble, "set grid;"])$ plot2d: expression evaluates to non-numeric value somewhere in plotting range. plot2d: expression evaluates to non-numeric value somewhere in plotting range. plot2d: expression evaluates to non-numeric value somewhere in plotting range.

(%t9)

9 - 28

(c) Gilberto Urroz / Luigi Marino - 2009

031ode_systems.wxm

3 / 6

Example 3 - A system of 2 first-order ODEs


(%i10) eq1:'diff(f(x),x)=f(x)+g(x)+sin(x);
d

(%o10)
dx

f( x )= sin( x )+ g( x )+ f( x )

(%i11) eq2:'diff(g(x),x)=-f(x)+3*g(x);
d

(%o11)
dx

g( x )= 3 g( x )- f( x )

(%i12) sist:[eq1,eq2];
d d

(%o12) [
dx

f( x )= sin( x )+ g( x )+ f( x ),
dx

g( x )= 3 g( x )- f( x )]

(%i13) atvalue(f(x),x=0,-9/25);
9

(%o13) 25

(%i14) atvalue(g(x),x=0,-4/25);
4

(%o14) 25

(%i15) desolve(sist,[f(x),g(x)]);
13 sin ( x ) 9 cos ( x ) 3 sin ( x ) 4 cos ( x )

(%o15) [ f( x )= 25

25

, g( x )= 25

25

(%i16) wxplot2d([-(13*sin(x))/25-(9*cos(x))/25,-(3*sin(x))/25-(4*cos(x))/25], [x,-5,5], [gnuplot_preamble, "set grid;"])$

(%t16)

9 - 29

(c) Gilberto Urroz / Luigi Marino - 2009

031ode_systems.wxm

4 / 6

Example 4 - A system of two second-order ODEs


(%i17) kill(all); (%o0) done (%i1) eq1:'diff(f(x),x,2)=-4*f(x)-3*g(x);
d 2

(%o1)
dx 2

f( x )= - 3 g( x )- 4 f( x )

(%i2) eq2:'diff(g(x),x,2)=-8*f(x)-2*g(x);
d 2

(%o2)
dx 2

g( x )= - 2 g( x )- 8 f( x )

(%i3) sist:[eq1,eq2];
d 2 d 2

(%o3) [
dx 2

f( x )= - 3 g( x )- 4 f( x ),
dx 2

g( x )= - 2 g( x )- 8 f( x )]

(%i4) atvalue(f(x),x=0,3); (%o4) 3 (%i5) atvalue('diff(f(x),x),x=0,0); (%o5) 0 (%i6) atvalue(g(x),x=0,4); (%o6) 4 (%i7) atvalue('diff(g(x),x),x=0,0); (%o7) 0 (%i8) desolve(sist, [f(x), g(x)]); (%o8) [ f( x )= 3 cos( 2 3 / 2 x ), g( x )= 4 cos( 2 3 / 2 x )]

9 - 30

(c) Gilberto Urroz / Luigi Marino - 2009

031ode_systems.wxm (%i9) wxplot2d([3*cos(2^(3/2)*x),4*cos(2^(3/2)*x)], [x,-5,5], [y,-5,5] ,[gnuplot_preamble, "set grid;"])$

5 / 6

(%t9)

Example 5 - A system of two first-order ODEs


(%i10) kill(all); (%o0) done (%i1) eq1:'diff(f(x),x)=-g(x)+x*sin(x);
d

(%o1)
dx

f( x )= x sin( x )- g( x )

(%i2) eq2:'diff(g(x),x)=-f(x)+x*cos(x);
d

(%o2)
dx

g( x )= x cos( x )- f( x )

(%i3) sist:[eq1,eq2];
d d

(%o3) [
dx

f( x )= x sin( x )- g( x ),
dx

g( x )= x cos( x )- f( x )]

(%i4) atvalue(f(x),x=0,1); (%o4) 1 (%i5) atvalue(g(x),x=0,1); (%o5) 1 (%i6) desolve(sist, [f(x), g(x)]);
sin ( x ) %e x 3 %e -x cos ( x ) %e x 3 %e -x

(%o6) [ f( x )= 2

+
4

+
4

, g( x )= x sin( x )+
2

+
4

9 - 31

(c) Gilberto Urroz / Luigi Marino - 2009

031ode_systems.wxm

6 / 6

(%i7) wxplot2d([-sin(x)/2+%e^x/4+(3*%e^(-x))/4,x*sin(x)+cos(x)/2-%e^x/4+(3*%e^(-x))/4], [x,-5, [y,-10,10] ,[gnuplot_preamble, "set grid;"])$ plot2d: expression evaluates to non-numeric value somewhere in plotting range. plot2d: expression evaluates to non-numeric value somewhere in plotting range.

(%t7)

(%i8) f1:-sin(t)/2+%e^t/4+(3*%e^(-t))/4;f2:t*sin(t)+cos(t)/2-%e^t/4+(3*%e^(-t))/4;
sin ( t ) %e t 3 %e -t

(%o8) 2

+
4

+
4 %e t 3 %e -t cos ( t )

(%o9) t sin( t )+
2

+
4

(%i10) wxplot2d([['parametric, f1, f2, [t, 0, 2], [nticks, 300]]], [x,0.45,0.78], [y,0.695,0.715], [gnuplot_preamble, "set grid;"])$

(%t10)

9 - 32

(c) Gilberto Urroz / Luigi Marino - 2009

Converting higher order ODEs to a system of first-order ODEs A couple of examples presented earlier demonstrate that it is possible to solve higher-order ODEs using functions desolve and atvalue directly. It is also possible to convert a higherorder ODE into a system of first-order ODEs and solved the system as indicated in the section immediately above. To illustrate the second approach, consider the following equation:
d y dy d y 4 2 8 5 y =0 , 3 dx dx dx
3 2

subject to y(0) = 1, y'(0) = -1, y''(0) = 0.5. The equation can be written also as y''' = -5y 8y' 4y'', and the following substitutions made: u1 = y, u2 = y ' = u1', u3 = y'' = u2' Next, replace y''' with u3' = y''' in the ODE, together with the other variable replacements, to get: u3' = -5 u1 -8 u2 4 u3. The variable conversions defined above can be re-written as: u1' = u2, u2' = u3, which together with u3' = -5 u1 -8 u2 4 u3., produce the following system of first-order ODEs: u1' = u2 u2' = u3 u3' = -5 u1 -8 u2 4 u3. which can be written as a single, first-order, vector ODE:
u u2 d 1 u = u3 dx 2 u3 5 u18 u 2 4 u3

[ ][

The initial conditions get converted to u1(0) = 1, u2(0) = -1, u3 (0)= 0.5. The following Maxima commands show the solution to this system of equations, including plots of the three functions solved for, namely, u1, u2, and u3, representing the solution y(x) and its first two derivatives, respectively.

9 - 33

(c) Gilberto Urroz / Luigi Marino - 2009

ThirdOrderToLinearSystem.wxm

1 / 3

Example 1 - Solving a 3rd-order ODE as a system of 1st-order ODEs


First, define the three ODEs: (%i3) ode1: diff(u1(x),x)=u2(x);
d

(%o3)
dx

u1( x )= u2( x )

(%i4) ode2: diff(u2(x),x)=u3(x);


d

(%o4)
dx

u2( x )= u3( x )

(%i5) ode3: diff(u3(x),x)=-5*u1(x)-8*u2(x)-4*u3(x);


d

(%o5)
dx

u3( x )= - 4 u3( x )- 8 u2( x )- 5 u1( x )

Next, create the system: (%i6) ode_syst : [ode1,ode2,ode3];


d d d

(%o6) [
dx

u1( x )= u2( x ),
dx

u2( x )= u3( x ),
dx

u3( x )= - 4 u3( x )- 8 u2( x )- 5 u1( x )]

Replace the initial conditions: (%i7) atvalue(u1(x),x=0,1); (%o7) 1 (%i8) atvalue(u2(x),x=0,-1); (%o8) - 1 (%i9) atvalue(u3(x),x=0,0.5); (%o9) 0.5

Solve for the ODE system, store solution into variable 'solns':

9 - 34

(c) Gilberto Urroz / Luigi Marino - 2009

ThirdOrderToLinearSystem.wxm (%i12) solns : desolve(ode_syst,[u1(x),u2(x),u3(x)]); rat: replaced -0.5 by -1/2 = -0.5


3 x sin 2 3x 5 %e - x + + , u2( x )= %e 2 6 6 6 11 x 11 x 11 x 3 x 8 sin 2 cos 2 -x 5 %e 5 %e - x 2 , u3 ( )= %e x + 6 ] 6 3 3 11

2 / 3

(%o12) [ u1( x )= %e

11 x 2

cos

11 x 2

7 sin 6

11 x 2 11

cos

11 2

Extract solution components using function "rhs" (right-hand side): (%i14) wxplot2d(rhs(solns[1]),[x,-5,5], [y,-5,5],[gnuplot_preamble, "set grid;"])$ plot2d: expression evaluates to non-numeric value somewhere in plotting range.

(%t14)

9 - 35

(c) Gilberto Urroz / Luigi Marino - 2009

ThirdOrderToLinearSystem.wxm (%i15) wxplot2d(rhs(solns[2]),[x,-5,5], [y,-5,5],[gnuplot_preamble, "set grid;"])$ plot2d: expression evaluates to non-numeric value somewhere in plotting range.

3 / 3

(%t15)

(%i16) wxplot2d(rhs(solns[3]),[x,-5,5], [y,-5,5],[gnuplot_preamble, "set grid;"])$ plot2d: expression evaluates to non-numeric value somewhere in plotting range.

(%t16)

9 - 36

(c) Gilberto Urroz / Luigi Marino - 2009

Numerical solutions to ODEs Function rk Function rk (runge-kutta) is used to obtain the numerical solution of a single ODE or of a system of ODEs using a fourth-order Runge-Kutta numerical approach. Find information about function rk by opening the Maxima manual, using Help > Maxima Help, and typing rk in the index search box, then pressing [Enter]. The following information is provided: _______________________________________________________________________________

_______________________________________________________________________________ The following examples illustrate the use of rk for the numerical calculation of single ODEs or systems of ODEs. The single ODE or the system of ODEs must be represented by a firstorder equation of the form dx/dt = f(t,x). Follow the examples to understand the different functions involved in their solutions. The results of function rk are presented in the form of a list of points, each containing two or more elements. These lists can be manipulated to produce lists of points for plotting. Some of the list functions that you will see used in these examples include: makelist, first, second, third, length, and rest. Information on these functions is available through the Maxima manual (Help > Maxima Help). Select the Contents tab and find the Lists chapter. Click on it to open it, and then click on each of the two sections therein contained: Introduction to Lists, and Functions and Variables for Lists. Plots of the solutions are presented using functions wxplot2d or wxdraw2d (see Chapter 4).

9 - 37

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm

1 / 24

Load "draw" package to use for plotting solutions. (%i11) load(draw)$

All the examples presented below were provided by Dr. Luigi Marino, Professor of Informatics and Mathematics, Liceo "Camillo Golgi" BRENO, Brescia, Italy.

Example 1
dx/dt = t - x^2, x(0)=1, 0 < t < 8, Dt = 0.1

rk solution: (%i2) sol: rk(t-x^2,x,1,[t,0,8,0.1])$

Plot using "wxplot2d" (%i3) wxplot2d([discrete,sol], [x,0,8], [gnuplot_preamble, "set grid;"])$

(%t3)

Prepare a list of values to plot using "wxdraw2d" from the "draw" package (%i4) sol_pts: makelist( rest(sol[i]),i,1,length(sol))$

9 - 38

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm (%i5) wxdraw2d( color=magenta,point_size=0,points_joined=true, points(sol))$

2 / 24

(%t5)

Example 2
Cooling law: dx/dt = -k*(T-Ta) (%i6) kill(all); (%o0) done (%i1) dx/dt=-0.0866188656*(x-75);
dx

(%o1)
dt

= - 0.0866188656( x - 75 )

(%i2) sol:rk(-0.0866188656*(x-75) ,x ,350,[t,0,65,0.5] )$ (%i3) wxdraw2d(grid=true,color=magenta,point_size=0.1,points_joined=true, points(sol))$

9 - 39

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm (%i4) wxplot2d([discrete,sol], [x,0,65], [y,0,350], [gnuplot_preamble, "set grid;"])$

3 / 24

(%t4)

Example 3:
dy/dx = cos(x)+exp(sin(x)) (%i5) sol:rk(cos(x)*y+exp(sin(x)),y,3,[x,0,15,0.1])$ (%i6) wxdraw2d(grid=true,yrange=[-1,35],color=green,point_size=0.1,points_joined=true, points

(%t6)

Example 4 - System of equations:


9 - 40 (c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm

4 / 24

dx/dt =4-x^2-4*y^2, dy/dt = y^2-x^2+1 x(0)=-1.25, y(0)=0.75, 0<t<4, Dt=0.02 (%i7) sol: rk([4-x^2-4*y^2,y^2-x^2+1],[x,y],[-1.25,0.75],[t,0,4,0.02])$

"sol1" = list of x-vs-t, "sol2" = list of y-vs-t, "sol3" = list of y-vs-x (%i8) sol1:makelist([first(sol[i]),second(sol[i])],i,1,length(sol))$ (%i9) sol2:makelist([first(sol[i]),third(sol[i])],i,1,length(sol))$ (%i10) sol3:makelist([second(sol[i]),third(sol[i])],i,1,length(sol))$

Drawing the three lists produced above.

Click off graph to continue.

(%i11) load("draw"); (%o11) C:/PROGRA~2/MAXIMA~1.1/share/maxima/5.18.1/share/draw/draw.lisp

(%i12) wxdraw2d(grid=true,yrange=[-1,2],color=red,point_size=0.1,points_joined=true, points(so

(%t12)

9 - 41

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm (%i13)

5 / 24

wxdraw2d(grid=true,yrange=[-1,2],color=blue,point_size=0.1,points_joined=true, points

(%t13)

(%i14)

wxdraw2d(grid=true,yrange=[-1,2],color=green,point_size=0.1,points_joined=true, points

(%t14)

Example 5 - 2nd-order equation into a system


Solve: y''=(2*(y')^2+y^2)/y with init. conds: x=4, y=2, y'=-2*tan(1)= -3.114815 producing a system of ODEs by sustitution u1 = y, u2 = y'. The system now is: du1/dx = u2 du2/dx = (2*u2^2+u1^2)/u1 with u1 = 2, u2 = -3.114815 at x = 4 (%i15) kill(all); sol : rk([u2,(2*u2^2+u1^2)/u1],[u1,u2],[2,-3.114815],[t,4,6,0.1])$ (%o0) done
9 - 42 (c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm (%i2) sol_m:transpose(sol)$ (%i3) sol_pts: makelist( rest(sol[i]),i,1,length(sol))$ (%i4) sol1:makelist([first(sol[i]),second(sol[i])],i,1,length(sol))$ (%i5) sol2:makelist([first(sol[i]),third(sol[i])],i,1,length(sol))$

6 / 24

(%i6) wxdraw2d(grid=true,yrange=[0,2],color=green,point_size=0.1,points_joined=true, points(so

(%t6)

(%i7) wxdraw2d(points_joined = true, yrange=[0,2], grid=true, line_width = 1, color = "light-red", points(sol) )$

9 - 43

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm

7 / 24

(%i8) wxplot2d([discrete,sol1],[style,[lines,1,6]],[gnuplot_preamble, "set grid;"]);

(%t8)

(%o8) (%i9) wxplot2d([discrete,sol2],[style,[lines,1,2]],[gnuplot_preamble, "set grid;"]);

(%t9)

(%o9) (%i10) wxdraw2d(grid=true,yrange=[-4,4],color=green,point_size=0.1,points_joined=true, points

9 - 44

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm

8 / 24

(%i11) wxdraw2d(grid=true,yrange=[-3,3],color=green,point_size=0.1,points_joined=true, points

(%t11)

Example 6 - system of equations:


dy1/dt = y2, dy2/dt = y3, dy3/dt = y1 (%i12) kill(all);sol:rk([y2,y3,y1],[y1,y2,y3],[2,-1,-1],[t,0,8,0.1])$ (%o0) done

(%i2) wxdraw2d(grid=true,yrange=[-1,2],color=green,point_size=0.1,points_joined=true, points(s

(%t2)

9 - 45

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm

9 / 24

(%i3) wxplot2d([discrete,sol],[style,[points,1,2]],[gnuplot_preamble, "set grid;"]);

(%t3)

(%o3)

Example 7 - system of equations


dy1/dt = y2+y3-2*y1, dy2/dt = y1+y3-3*y2, dy3/dt = y1+y2-3*y3 (%i4) kill(all); (%o0) done (%i1) sol:rk([y2+y3-3*y1,y1+y3-3*y2,y1+y2-3*y3],[y1,y2,y3],[1,2,-1],[t,0,8,0.1])$ (%i2) sol1:transpose(sol)$

9 - 46

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm

10 / 24

(%i3) load(draw); wxdraw2d(grid=true,yrange=[-1,1],color=green,point_size=0.1,points_joined=true, points(s (%o3) C:/PROGRA~2/MAXIMA~1.1/share/maxima/5.18.1/share/draw/draw.lisp

(%t4)

(%i5) sol2:makelist([first(sol[i]),second(sol[i])],i,1,length(sol))$ (%i6) sol3:makelist([first(sol[i]),third(sol[i])],i,1,length(sol))$ (%i7) sol4:makelist([first(sol[i]),fourth(sol[i])],i,1,length(sol))$

(%i8) wxdraw2d(grid=true,yrange=[-1,1],color=green,point_size=0.1,points_joined=true, points(s

(%t8)

9 - 47

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm

11 / 24

(%i9) wxplot2d([[discrete,sol4],[discrete,sol3],[discrete,sol2]] ,[style,[points,1]],[y,-1,2], [gnuplot_preamble, "set grid;"]);

(%t9)

(%o9)

Example 8 - system of equations


dy1/dt = y1+y2+sin(x), dy2/dt = -y1+3*y2 (%i10) kill(all); (%o0) done (%i1) sol:rk([y1+y2+sin(x),-y1+3*y2],[y1,y2],[-9/25,-4/25],[x,0,15,0.5])$ (%i2) sol1:transpose(sol)$ (%i3) solList1:makelist([first(sol[i]),second(sol[i])],i,1,length(sol))$ solList2:makelist([first(sol[i]),third(sol[i])],i,1,length(sol))$ solList3:makelist([second(sol[i]),third(sol[i])],i,1,length(sol))$

9 - 48

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm

12 / 24

(%i6) wxdraw2d(grid=true,yrange=[-1,10],color=green,point_size=0.1,points_joined=true, points

(%t6)

(%i7) wxdraw2d(grid=true,yrange=[-1,10],color=green,point_size=0.1,points_joined=true, points

(%t7)

9 - 49

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm

13 / 24

(%i8) wxdraw2d(grid=true,color=green,point_size=0.1,points_joined=true, points(solList3))$

(%t8)

Example 9 - system of equations


dy1/dt = y2, dy2/dt = -4*y1-3*y3, dy3/dt = y4, dy4/dt = -8*y1-2*y3 (%i9) kill(all); (%o0) done (%i1) sol:rk([y2,-4*y1-3*y3,y4,-8*y1-2*y3],[y1,y2,y3,y4],[3,0,4,0],[x,0,4.442882938,0.1])$

(%i2) wxdraw2d(grid=true,yrange=[-5,5],color=green,point_size=0.1,points_joined=true, points(s

(%t2)

(%i3) sol1:makelist([first(sol[i]),second(sol[i])],i,1,length(sol))$
9 - 50 (c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm (%i4) sol2:makelist([first(sol[i]),third(sol[i])],i,1,length(sol))$

14 / 24

(%i5) wxdraw2d(grid=true,yrange=[-8,8],color=blue,point_size=0.1,points_joined=true, points(so

(%t5)

(%i6) sol3:makelist([first(sol[i]),fourth(sol[i])],i,1,length(sol))$

(%i7) wxdraw2d(grid=true,yrange=[-8,8],color=magenta,point_size=0.1,points_joined=true, points

(%t7)

9 - 51

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm

15 / 24

(%i8) wxplot2d([[discrete,sol1],[discrete,sol3],[discrete,sol2]] ,[style,[points,1]],[y,-6,6], [gnuplot_preamble, "set grid;"]);

(%t8)

(%o8) (%i9) wxplot2d([[discrete,sol1],[discrete,sol3],[discrete,sol2]] ,[style,lines],[y,-9,9], [gnuplot_preamble, "set grid;"]);

(%t9)

(%o9)

Example 10 - system of equations


dx/dt = y, dy/dt = -sin(x) (%i10) kill(all); (%o0) done
9 - 52 (c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm (%i1) sol:rk([y,-sin(x)],[x,y],[0,1],[x,0,3.14159,0.1])$ (%i2) sol1:makelist([first(sol[i]),second(sol[i])],i,1,length(sol))$ (%i3) sol2:makelist([first(sol[i]),third(sol[i])],i,1,length(sol))$ (%i4) sol3:makelist([second(sol[i]),third(sol[i])],i,1,length(sol))$ (%i5) wxplot2d([discrete,sol1],[style,lines],[y,0,1.2], [gnuplot_preamble, "set grid;"]);

16 / 24

(%t5)

(%o5) (%i6) wxplot2d([discrete,sol2],[style,lines],[y,0,1.2], [gnuplot_preamble, "set grid;"]);

(%t6)

(%o6)

9 - 53

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm (%i7) wxplot2d([discrete,sol3],[style,lines],[y,0,1.2], [gnuplot_preamble, "set grid;"]);

17 / 24

(%t7)

(%o7) (%i8) kill(all); (%o0) done

Example 11 - The Van Der Pol Equation


dx/dt = y-x*(x^2-1), dy/dt = -x+cos(t) (%i1) sol:rk([y-x*(x^2-1),-x+cos(t)],[x,y],[0,0],[t,0,10,0.1])$ (%i2) sol1:makelist([first(sol[i]),second(sol[i])],i,1,length(sol))$ (%i3) sol2:makelist([first(sol[i]),third(sol[i])],i,1,length(sol))$ (%i4) sol3:makelist([second(sol[i]),third(sol[i])],i,1,length(sol))$

9 - 54

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm (%i5) wxplot2d([[discrete,sol1],[discrete,sol2]],[style,lines],[y,-2,2], [gnuplot_preamble, "set grid;"]);

18 / 24

(%t5)

(%o5) (%i6) wxplot2d([discrete,sol3],[style,lines],[y,-3,3], [gnuplot_preamble, "set grid;"]);

(%t6)

(%o6)

Example 12 - Single ODE


dx/dt = sin(t*x)+sin(t+x) (%i7) kill(all); (%o0) done
9 - 55 (c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm (%i1) sol:rk([sin(t*x)+sin(t+x)],x,0.5,[t,-1,1,0.1])$ (%i2) wxplot2d([discrete,sol],[style,lines],[y,0,1], [gnuplot_preamble, "set grid;"]);

19 / 24

(%t2)

(%o2) (%i3) kill(all); (%o0) done (%i1) sol:rk([y,-5*y-6*x],[x,y],[1,0],[t,0,5,0.1])$ (%i2) sol1:makelist([first(sol[i]),second(sol[i])],i,1,length(sol))$ (%i3) sol2:makelist([first(sol[i]),third(sol[i])],i,1,length(sol))$ (%i4) sol3:makelist([second(sol[i]),third(sol[i])],i,1,length(sol))$

9 - 56

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm

20 / 24

(%i5) wxplot2d([[discrete,sol1],[discrete,sol2],[discrete,sol3]],[style,[points,1]],[y,-1,1], [gnuplot_preamble, "set grid;"]);

(%t5)

(%o5)

Example 13 - The Lokta-Volterra equation


dx/dt = x*(1-y), dy/dt = -y*(1-x) (%i6) kill(all); (%o0) done (%i1) sol:rk([x*(1-y),-y*(1-x)],[x,y],[2,2],[t,0,10,0.01])$ (%i2) sol1:makelist([first(sol[i]),second(sol[i])],i,1,length(sol))$ (%i3) sol2:makelist([first(sol[i]),third(sol[i])],i,1,length(sol))$

9 - 57

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm (%i4) wxplot2d([[discrete,sol1],[discrete,sol2]],[style,[points,1]],[y,0,3], [gnuplot_preamble, "set grid;"]);

21 / 24

(%t4)

(%o4) (%i5) sol3:makelist([second(sol[i]),third(sol[i])],i,1,length(sol))$ (%i6) wxplot2d([discrete,sol3],[style,[points,1]],[y,0,3], [gnuplot_preamble, "set grid;"]);

(%t6)

(%o6)

Example 14 - An LCR electric circuit


LCR circuit-equation L=0.1 Henry; C=10^(-3) Farad ; R=10 ohm ;v=5 volt dx/dt = y, dy/dt = -(L+R*C^2)/(L*C*R)*y - 2/(L*C)*x+v/(L*C) Here x = electric current, y = electric charge (y = dx/dt)
9 - 58 (c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm (%i7) kill(all); (%o0) done (%i1) L:0.1;C:10^(-3);R:10;v:5; (%o1) 0.1


1

22 / 24

(%o2)
1000

(%o3) 10 (%o4) 5 (%i5) sol:rk([y,-(L+R*C^2)/(L*C*R)*y - 2/(L*C)*x+v/(L*C)], [x,y],[0,0],[t,0,0.1,0.001])$ (%i6) sol1:makelist([first(sol[i]),second(sol[i])],i,1,length(sol))$ (%i7) wxplot2d([discrete,sol1],[style,[points,1]],[y,0,3.4], [gnuplot_preamble, "set grid;"]);

(%t7)

(%o7) (%i8) sol2:makelist([first(sol[i]),third(sol[i])],i,1,length(sol))$

9 - 59

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm (%i9) wxplot2d([discrete,sol2],[style,[lines,1,6]],[y,-100,250], [gnuplot_preamble, "set grid;"]);

23 / 24

(%t9)

(%o9) (%i10) wxdraw2d( color=magenta,point_size=0,points_joined=true, points(sol2))$

(%t10)

9 - 60

(c) Gilberto Urroz / Luigi Marino - 2009

05Modified_usage_rk_numeric_ode.wxm (%i11) wxdraw2d( color=green,point_size=0,points_joined=true, points(sol1))$

24 / 24

(%t11)

>>

9 - 61

(c) Gilberto Urroz / Luigi Marino - 2009

04Focault_pendulumNUmerical.wxm

1 / 3

Focault pendulum - 2nd-order system


Solve the following system of two 2nd-order ODEs: x''=-2*omega*sin(psi)*y'+k2*x y''=+2*omega*cos(psi)*x'+k2*y by converting it to the 1st-order ODE system: x'=v y'=z z'=2*omega*sin(psi)-k2*x v'=-2*omega*sin(psi)*v-k2*y init:x(0)=1,y(0)=0,z(0)=0,v(0)=0 k2=9.8/20 psi=pi/4 omega=0.0000729 (%i15) kill(all); (%o0) done (%i1) eq1:[v,z,2*omega*sin(psi)-k2*x,-2*omega*sin(psi)*v-k2*y]; (%o1) [ v , z , 2 omega sin( psi )- k2 x , - k2 y - 2 omega sin( psi )v ] (%i2) omega:0.0000729; (%o2) 7.2899999999999997 10 -5 (%i3) k2:9.8/20; (%o3) 0.49 (%i4) psi:float(%pi/4); (%o4) 0.78539816339745 (%i5) t_range:[t,0,100,0.03]; (%o5) [ t , 0 , 100 , 0.03 ] (%i6) init1:[1,0,0,0]; (%o6) [ 1 , 0 , 0 , 0 ] (%i7) eq2:[v,z,2*0.0000729*sin( 0.78539816339745 )-0.49*x, -2*0.0000729*sin( 0.78539816339745 )*v-0.49*y ]; (%o7) [ v , z , 1.0309616869699881 10 -4 - 0.49 x , - 0.49 y - 1.0309616869699881 10 -4 v ] (%i8) sol: rk(eq2, [x,y,v,z],[1,0,0,0],[t,0,100,0.03])$ (%i9) sol1:makelist([first(sol[i]),second(sol[i])],i,1,length(sol))$

9 - 62

(c) Gilberto Urroz / Luigi Marino - 2009

04Focault_pendulumNUmerical.wxm

2 / 3

(%i10) wxdraw2d( color=green,title="x vs t", point_size=0,points_joined=true, points(sol1))$

(%t10)

(%i11) sol2:makelist([first(sol[i]),third(sol[i])],i,1,length(sol))$ (%i12) sol3: makelist( rest(sol[i]),i,1,length(sol))$ (%i13) wxdraw2d( color=blue,title="y vs t",point_size=0,points_joined=true, points(sol2))$

(%t13)

9 - 63

(c) Gilberto Urroz / Luigi Marino - 2009

04Focault_pendulumNUmerical.wxm

3 / 3

(%i14) wxdraw2d( color=magenta,title="Phase Plane", point_size=0,points_joined=true, points(so

(%t14)

9 - 64

(c) Gilberto Urroz / Luigi Marino - 2009

Vous aimerez peut-être aussi