Symbolic solution of linear differential equations and systems using the Laplace transform method using SymPy

3r31228. 3r3-31.

3r31228. The implementation of algorithms in Python using symbolic calculations is very convenient when solving problems of mathematical modeling of objects defined by differential equations. To solve such equations, the Laplace transformations are widely used, which, to put it simply, allow us to reduce the problem to the solution of the simplest algebraic equations.

3r31228.

3r31228. In this publication, I propose to consider the functions of the direct and inverse Laplace transform from the SymPy library, which allow using the Laplace method to solve differential equations and systems using Python tools.

3r31228.

3r31214.

3r31228. The Laplace method itself and its advantages in solving linear differential equations and systems are widely covered in the literature, for example, in the popular[1]edition. . In the book, the Laplace method is given for implementation in licensed software packages Mathematica, Maple and MATLAB (which implies the acquisition of this software by an educational institution) on selected examples by the author.

3r31228.

3r31228. Let us try today to consider not a separate example of solving a learning problem using Python, but a general method for solving linear differential equations and systems using the functions of the direct and inverse Laplace transform. In this case, we keep the learning moment: the left side of a linear differential equation with Cauchy conditions will be formed by the student himself, and the routine part of the problem, consisting in the direct Laplace transform of the right side of the equation, will be performed using the function 3r312101. laplace_transform () [/b] .

3r31228.

3r31228. 3r31193. The history of the authorship of the Laplace transformations

3r31228. Laplace transformations (images by Laplace) have an interesting history. For the first time the integral in the definition of the Laplace transform appeared in one of the works of L. Euler. However, in mathematics it is customary to call a technique or theorem the name of the mathematician who discovered it after Euler. Otherwise, there would be several hundred different Euler theorems.

3r31228.

3r31228. In this case, the following after Euler was the French mathematician Pierre Simon de Laplace (Pierre Simon de Laplace (1749-1827)). It was he who used such integrals in his work on probability theory. Laplace itself did not use so-called “operational methods” to find solutions to differential equations based on Laplace transforms (Laplace images). These methods were actually discovered and promoted by practical engineers, especially the English electrical engineer Oliver Heaviside (1850-1925). Long before the validity of these methods was rigorously proved, operational calculus was successfully and widely applied, although its legality was largely questioned even at the beginning of the 20th century, and very heated debates were held on this topic.

3r31228.

3r31228. 3r31193. The functions of the direct and inverse Laplace transform

3r31228.

3r31228. 3r31212. The direct Laplace transform function:

3r31228. sympy.integrals.transforms. 3r31201. laplace_transform [/b] (t, s, ** hints).

3r31228. Function ** laplace_transform () ** performs the Laplace transform function * f (t) * real variable to function * F (s) * complex variable, so:

3r31228.

3r3402.

3r33939.

3r3402.

3r31228. This function returns 3r31201. (F, a, cond) [/b] where * F (s) * There is a Laplace transform of the * function. f (t) * , 3r31201. a

3r3402.

3r33939. 3r3118.

3r3402.

3r31228. If the integral cannot be computed in a closed form, this function returns ** uncomputed. InverseLaplaceTransform ** object.

3r31228. 3r31215. 3r31228. 3r31217.

3r31228. 3r31193. The inverse Laplace transform on the example of determining the transition characteristics of regulators 3r31194.

3r31228. The transfer function of the PID controller is[2]:

3r31228.

3r3402.

3r33939. 3r3142.

3r3402.

3r31228. We write a program for obtaining equations for the transient characteristics of the PID and PI controllers for the reduced transfer function, and additionally derive the time taken to perform the inverse visual Laplace transform.

3r31228.

3r31228.

The text of the program [/b]

3r31186. 3r31187. # Downloading required modules

import time

start = time.time ()

from sympy import *

import matplotlib.pyplot as plt

import numpy as np

var ('s Kp Ti Kd Td') # Declare character variables

var ('t', positive = True) # A restriction on a character time variable

Kp = 2; Ti = 2; Kd = 4; Td = Rational (?2) # Representation of a fractional character variable

fp = (1+ (Kd * Td * s) /(1 + Td * s)) * Kp * (1 + 1 /(Ti * s)) * (1 /s) # The transfer function of the PID controller with the operator s 3r31228. ht = inverse_laplace_transform (fp, s, t) # Transient response of the PID controller, obtained by the inverse Laplace transform method 3r31228. Kd = 0

fpp = (1+ (Kd * Td * s) /(1 + Td * s)) * Kp * (1 + 1 /(Ti * s)) * (1 /s) # The transfer function of the PI controller (Kd = 0) with the operator s

htt = inverse_laplace_transform (fpp, s, t) # Transient response of the PI controller, obtained by the inverse Laplace transform method 3r31228. stop = time.time () 3r3r122. print ('Time to the inverse visual Laplace transform:% s s'% N ((stop-start), 3))

# Move from the symbolic region to the numeric 3r31228. f = lambdify (t, ht, 'numpy') 3r3r12228. F = lambdify (t, htt, 'numpy') 3r3r12228. tt = np.arange (0.0?2????) r3r31228. # Plotting

plt.title ('Transient characteristics of regulators n with transfer functions: n PID - W (s) =% s n PI - W (s) =% s'% (fp, fpp)) 3r31228. plt.plot (tt, f (tt), color = 'r', linewidth = ? label = 'PID controller: h (t) =% s'% ht)

plt.plot (tt, F (tt), color = 'b', linewidth = ? label = 'PI controller: h (t) =% s'% htt)

plt.grid (true)

plt.legend (loc = 'best')

plt.show ()

3r31189. 3r31190.

3r31228. 3r31224. 3r31224.

3r31228. 3r31201. We get: [/b]

3r31228.

3r31228. Time for the inverse visual Laplace transform: ??? s

3r31228.

3r31228. 3r3202.

3r31228.

3r31228. The inverse Laplace transform is often used in the synthesis of ACS, where Python can replace expensive software “monsters” such as MathCAD, therefore the use of the inverse transform is of practical importance.

3r31228.

3r31228. 3r31193. The Laplace transform of higher order derivatives for solving the Cauchy problem 3r31194.

3r31228. In continuation of our discussion there will be an application of Laplace transformations (images according to Laplace) to the search for solutions of a linear differential equation with constant coefficients of the form: 3r-31205. 3r31228.

3r3402.

3r33939.

3r3402.

3r31228.

3r31228. If * a * and 3r31168. b [/i] - constants, then

3r31228.

3r3402.

3r33939.

3r3402.

3r31228. for all 3r31168. s [/i] , such that there are both Laplace transforms (Laplace images) of the functions 3r31168. f (t) [/i] and 3r31168. q (t) [/i] .

3r31228.

3r31228. We verify the linearity of the direct and inverse Laplace transforms using the previously considered functions 3r31201. laplace_transform () [/b] and 3r31201. inverse_laplace_transform () [/b] . For this, we take * as an example. f (t) = sin (3t) * , 3r31168. q (t) = cos (7t) [/i] , 3r31168. a = 5 [/i] , 3r31168. b = 7 r3r31169. and use the following program.

3r31228.

3r31228.

The text of the program [/b]

3r31186. 3r31187. from sympy import *

var ('s a b')

var ('t', positive = True)

a = 5 3r31228. f = sin (3 * t)

b = 7 3r31228. q = cos (7 * t)

L1 = a * laplace_transform (f, t, s, noconds = True) # Forward transformation a * L {f (t)} 3r31228. L2 = b * laplace_transform (q, t, s, noconds = True) # Forward transformation b * L {q (t)} 3r31228. L = factor (L1 + L2) # Amount a * L {f (t)} + b * L {q (t)}

print (L) 3r31228. LS = factor (laplace_transform (a * f + b * q, t, s, noconds = True)) # Forward transformation L {a * f (t) + b * q (t)} 3r31228. print (LS) 3r31228. print (LS == L) 3r3r122. L_1 = a * inverse_laplace_transform (L1 /a, s, t) # Inverse transform a * L ^ -1 {f (t)} 3r31228. L_2 = b * inverse_laplace_transform (L2 /b, s, t) # Inverse transform b * L ^ -1 {q (t)} 3r31228. L_S = L_1 + L_2 # a * L ^ -1 {f (t)} + b * L ^ -1 {q (t)} 3r31228. print (L_S) 3r31228. L_1_2 = inverse_laplace_transform (L1 + L? s, t) # Inverse transform L ^ -1 {a * f (t) + b * q (t)} 3r31228. print (L_1_2) 3r31228. print (L_1_2 == L_S)

3r31189. 3r31190.

3r31228. 3r31224. 3r31224.

3r31228. 3r31201. We get: [/b]

3r31228.

3r31228. (7 * s ** 3 + 15 * s ** 2 + 63 * s + 735) /((s ** 2 + 9) * (s ** 2 + 49)) r3r31205. 3r31228. (7 * s ** 3 + 15 * s ** 2 + 63 * s + 735) /((s ** 2 + 9) * (s ** 2 + 49)) r3r31205. 3r31228. True

3r31228. 5 * sin (3 * t) + 7 * cos (7 * t)

3r31228. 5 * sin (3 * t) + 7 * cos (7 * t)

3r31228.

3r31228. The code also demonstrates the uniqueness of the inverse Laplace transform.

3r31228.

3r31228. Assuming that

satisfies the conditions of the first theorem, then from this theorem it will follow that: 3r3-31205. 3r31228.

3r3402.

3r33939. 3r33948. 3r3402.

3r31228. and thus r3r31205. 3r31228.

3r3402.

3r33939. 3r33333.

3r3402.

3r31228. Repeating this calculation gives r3r31205. 3r31228.

3r3402.

3r33939. 3r33357.

3r3402.

3r31228. After a finite number of such steps, we obtain the following generalization of the first theorem: 3r3-31205. 3r31228.

3r3402.

3r33939.

3r3402.

3r31228.

3r3402.

3r33939.

3r3402.

3r31228.

3r31228. Applying relation (3), which contains the Laplace-derived derivatives of the sought-for function with initial conditions, to equation (1), one can obtain its solution by the method specially developed in our department with the active support of 3r-3384. Scorobey

for the sympy library.

3r31228.

3r31228. 3r31193. A method for solving linear differential equations and systems of equations based on Laplace transforms using the library SymPy

3r31228. To demonstrate the method, we use a simple differential equation that describes the motion of a system consisting of a material point of a given mass, fixed on a spring, to which an external force is applied. The differential equation and initial conditions for such a system are of the form:

3r31228.

3r3402.

3r33939. 3r33400.

3r3402.

3r31228. where

3r3406.

- the reduced initial position of the mass,

3r3409.

- reduced initial mass velocity.

3r31228.

3r31228. A simplified physical model defined by equation (4) with nonzero initial conditions[1]:

3r31228.

3r31228. 3r33434.

3r31228.

3r31228. A system consisting of a material point of a given mass fixed on a spring satisfies the Cauchy problem (the problem with initial conditions). The material point of a given mass is initially at rest in the position of its equilibrium.

3r31228.

3r31228. To solve this and other linear differential equations using the Laplace transform method, it is convenient to use the following system, derived from relations (3):

3r31228. 3r33939.

3r31228. 3r33939.

3r33946.

3r31228. 3r33939.

3r31228. 3r33939.

3r31228.

3r31228. The sequence of the SymPy solution is as follows:

3r31228.

3r31228.

3r31228. 3r31212. load the necessary modules and explicitly define character variables:

3r31228.

3r31228. 3r31186. 3r31187. from sympy import *

import numpy as np

import matplotlib.pyplot as plt

var ('s')

var ('t', positive = True)

var ('X', cls = Function)

3r31189. 3r31190.

3r31228. 3r31215. 3r31228. 3r31212. specify the version of the sympy library to take into account its features. To do this, enter the following lines:

3r31228.

3r31228. 3r31186. 3r31187. import SymPy

print ('The version of the sympy library is%'% (sympy._version_))

3r31189. 3r31190.

3r31228. 3r31215. 3r31228. 3r31212. According to the physical meaning of the problem, the time variable is determined for the region including zero and positive numbers. We set the initial conditions and the function in the right part of equation (4) with its subsequent Laplace transform. For initial conditions, it is necessary to use the Rational function, since the use of decimal rounding leads to an error.

3r31228.

3r31228. 3r31186. 3r31187. x0 = Rational (6.5) # The given initial mass position is 3r31228. x01 = Rational (?1) # The given initial speed is

g = sin (3 * t)

Lg = laplace_transform (g, t, s, noconds = True)

3r31189. 3r31190.

3r31228. 3r31215. 3r31228. 3r31212. using (5), we rewrite the Laplace-transformed derivatives that appear in the left-hand side of equation (4), form the left-hand side of this equation, and compare the result with its right-hand side: 3r31205. 3r31228.

3r31228. 3r31186. 3r31187. d2 = s ** 2 * X (s) -s * x0-x01

d0 = X (s)

d = d2 + 4 * d0

de = Eq (d, Lg) 3r31228. 3r31189. 3r31190.

3r31228. 3r31215. 3r31228. 3r31212. solve the obtained algebraic equation for the X (s) transformation and perform the inverse Laplace transformation:

3r31228.

3r31228. 3r31186. 3r31187. rez = solve (de, X (s))[0]3r31228. soln = inverse_laplace_transform (rez, s, t)

3r31189. 3r31190.

3r31228. 3r31215. 3r31228. 3r31212. we are moving from work in the SymPy library to the NumPy library: 3r3-31205. 3r31228.

3r31228. 3r31186. 3r31187. f = lambdify (t, soln, 'numpy')

3r31189. 3r31190.

3r31228. 3r31215. 3r31228. 3r31212. We build the graph in the usual way for Python: 3r3-31205. 3r31228.

3r31228. 3r31186. 3r31187. x = np.linspace (0.6 * np.pi, 100)

plt.title ('The function giving the position of the material point n of a given mass: n x (t) =% s'% soln)

plt.grid (true)

plt.xlabel ('t', fontsize = 12)

plt.ylabel ('x (t)', fontsize = 12)

plt.plot (x, f (x), 'g', linewidth = 2)

plt.show ()

3r31189. 3r31190.

3r31228. 3r31215. 3r31228. 3r31217.

3r31228.

Full text of the program: [/b]

3r31186. 3r31187. from sympy import *

import numpy as np

import matplotlib.pyplot as plt

var ('s')

var ('t', positive = True)

var ('X', cls = Function)

import sympy 3r31228. print ("The version of the sympy library is% s"% (sympy .__ version__)) 3r3-31228. x0 = Rational (? 5) # Given the initial position of the material point of a given mass 3r31228. x01 = Rational (?1) # The reduced initial velocity of the material point of a given mass

g = sin (3 * t)

Lg = laplace_transform (g, t, s, noconds = True) # The direct Laplace transform

d2 = s ** 2 * X (s) -s * x0-x01

d0 = X (s)

d = d2 + 4 * d0

de = Eq (d, Lg) 3r31228. rez = solve (de, X (s))[0]# Solving an algebraic equation

soln = inverse_laplace_transform (rez, s, t) # Inverse Laplace transform

f = lambdify (t, soln, "numpy") 3r3r12228. x = np.linspace (0.6 * np.pi, 100)

plt.title ('The function giving the position of the material point n of a given mass: n x (t) =% s'% soln)

plt.grid (true)

plt.xlabel ('t', fontsize = 12)

plt.ylabel ('x (t)', fontsize = 12)

plt.plot (x, f (x), 'g', linewidth = 2)

plt.show ()

3r31189. 3r31190.

3r31228. 3r31224. 3r31224.

3r31228. 3r31201. We get: [/b]

3r31228. The sympy library version is 1.3 r3r31205. 3r31228.

3r31228. 3r3633.

3r31228.

3r31228. The graph of the periodic function, giving the position of the material point of a given mass. The Laplace transform method using the SymPy library gives a solution not only without the need to first find the general solution of a homogeneous equation and a particular solution to the original inhomogeneous differential equation, but also without the need to use the method of unit fractions and Laplace tables.

3r31228.

3r31228. At the same time, the educational value of the solution method is preserved due to the need to use system (5) and transition to NumPy to study the solution with more productive methods.

3r31228.

3r31228. To further demonstrate the method, we solve the system of differential equations: 3r3-31205. 3r31228. 3r33939.

3r31228. with initial conditions 3r33946.

3r31228.

3r31228. A simplified physical model defined by the system of equations (6) with zero initial conditions: 3r3-31205. 3r31228.

3r31228. 3r3662.

3r31228.

3r31228. Thus, the power of * f (t) * it is suddenly applied to the second material point of a given mass at time * t = 0 * when the system is at rest in its equilibrium position.

3r31228.

3r31228. The solution of the system of equations is identical to the previously considered solution of the differential equation (4), therefore, I quote the program text without explanation.

3r31228.

3r31228.

The text of the program [/b]

3r31186. 3r31187. from sympy import *

import numpy as np

import matplotlib.pyplot as plt

var ('s')

var ('t', positive = True)

var ('X Y', cls = Function)

x0 = 0

x01 = 0

y0 = 0

y01 = 0

g = 40 * sin (3 * t)

Lg = laplace_transform (g, t, s, noconds = True)

de1 = Eq (2 * (s ** 2 * X (s) -s * x0-x01) + 6 * X (s) -2 * Y (s)) 3r31228. de2 = Eq (s ** 2 * Y (s) -s * y0-y01-2 * X (s) + 2 * Y (s) -Lg) 3r31228. rez = solve ([de1,de2], X (s), Y (s))

rezX = expand (rez[X(s)]) 3r31228. solnX = inverse_laplace_transform (rezX, s, t)

rezY = expand (rez[Y(s)]) 3r31228. solnY = inverse_laplace_transform (rezY, s, t)

f = lambdify (t, solnX, "numpy")

F = lambdify (t, solnY, "numpy")

x = np.linspace (0.4 * np.pi, 100) 3r3r12228. plt.title ('Functions of the position of the material points of given masses: n x (t) =% sn y (t) =% s'% (solnX, solnY)) 3r312228. plt.grid (true)

plt.xlabel ('t', fontsize = 12)

plt.ylabel ('x (t), y (t)', fontsize = 12)

plt.plot (x, f (x), 'g', linewidth = ? label = 'x (t)')

plt.plot (x, F (x), 'b', linewidth = ? label = 'y (t)')

plt.legend (loc = 'best')

plt.show ()

3r31189. 3r31190.

3r31228. 3r31224. 3r31224.

3r31228. 3r31201. We get: [/b]

3r31228.

3r31228. 3r31228.

3r31228. For nonzero initial conditions, the program text and the graph of functions will take the form: 3r3-31205 3r31228.

3r31228.

The text of the program [/b]

3r31186. 3r31187. from sympy import *

import numpy as np

import matplotlib.pyplot as plt

var ('s')

var ('t', positive = True)

var ('X Y', cls = Function)

x0 = 0

x01 = -1

y0 = 0

y01 = -1

g = 40 * sin (t)

Lg = laplace_transform (g, t, s, noconds = True)

de1 = Eq (2 * (s ** 2 * X (s) -s * x0-x01) + 6 * X (s) -2 * Y (s)) 3r31228. de2 = Eq (s ** 2 * Y (s) -s * y0-y01-2 * X (s) + 2 * Y (s) -Lg) 3r31228. rez = solve ([de1,de2], X (s), Y (s))

rezX = expand (rez[X(s)]) 3r31228. solnX = (inverse_laplace_transform (rezX, s, t)). evalf (). n (3) 3r31228. rezY = expand (rez[Y(s)]) 3r31228. solnY = (inverse_laplace_transform (rezY, s, t)). evalf (). n (3) 3r31228. f = lambdify (t, solnX, "numpy")

F = lambdify (t, solnY, "numpy")

x = np.linspace (0.4 * np.pi, 100) 3r3r12228. plt.title ('Functions of the position of material points of given masses: n x (t) =% s n y (t) =% s'% (solnX, solnY))

plt.grid (true)

plt.xlabel ('t', fontsize = 12)

plt.ylabel ('x (t), y (t)', fontsize = 12)

plt.plot (x, f (x), 'g', linewidth = ? label = 'x (t)')

plt.plot (x, F (x), 'b', linewidth = ? label = 'y (t)')

plt.legend (loc = 'best')

plt.show ()

3r31189. 3r31190.

3r31228. 3r31224. 3r31224.

3r31228. 3r33782.

3r31228.

3r31228. Consider solving a fourth-order linear differential equation with zero initial conditions: 3r3-31205. 3r31228. 3r33939. 3r3790.

3r31228. 3r33939. 3r33795.

3r31228.

3r31228.

Text of the program: [/b]

3r31186. 3r31187. from sympy import *

import numpy as np

import matplotlib.pyplot as plt

var ('s')

var ('t', positive = True)

var ('X', cls = Function)

# Initial conditions 3r31228. x0 = 0

x01 = 0

x02 = 0

x03 = 0

g = 4 * t * exp (t)

Lg = laplace_transform (g, t, s, noconds = True) # The direct Laplace transform

d4 = s ** 4 * X (s) -s ** 3 * x0-s ** 2 * x01-s * x02-x03

d2 = s ** 2 * X (s) -s * x0-x01

d0 = X (s)

d = factor (d4 + 2 * d2 + d0)

de = Eq (d, Lg) 3r31228. rez = solve (de, X (s))[0]# Solving an algebraic equation

soln = inverse_laplace_transform (rez, s, t) # Inverse Laplace transform

f = lambdify (t, soln, "numpy") 3r3r12228. x = np.linspace (0.6 * np.pi, 100)

plt.title ('Solution: n x (t) =% sn'% soln, fontsize = 11)

plt.grid (true)

plt.xlabel ('t', fontsize = 12)

plt.ylabel ('x (t)', fontsize = 12)

plt.plot (x, f (x), 'g', linewidth = 2)

plt.show ()

3r31189. 3r31190.

3r31228. 3r31224. 3r31224.

3r31228. 3r31201. Decision schedule: [/b]

3r31228.

3r31228. 3r33849.

3r31228.

3r31228. Solve a fourth order linear differential equation:

3r31228. 3r33939. 3r33857.

3r31228. with initial conditions 3r33946. 3r33862.

, 3r33946. 3r33865.

, 3r33946. 3r33868.

.

3r31228.

3r31228.

Text of the program: [/b]

3r31186. 3r31187. from sympy import *

import numpy as np

import matplotlib.pyplot as plt

var ('s')

var ('t', positive = True)

var ('X', cls = Function)

# Initial conditions 3r31228. x0 = 0

x01 = 2

x02 = 0

x03 = -13

d4 = s ** 4 * X (s) -s ** 3 * x0-s ** 2 * x01-s * x02-x03

d2 = s ** 2 * X (s) -s * x0-x01

d0 = X (s)

d = factor (d4 + 13 * d2 + 36 * d0)

de = Eq (d, 0) 3r31228. rez = solve (de, X (s))[0]# Solving an algebraic equation

soln = inverse_laplace_transform (rez, s, t) # The inverse Laplace transform

f = lambdify (t, soln, "numpy") 3r3r12228. x = np.linspace (0.6 * np.pi, 100)

plt.title ('Solution: n x (t) =% sn'% soln, fontsize = 11)

plt.grid (true)

plt.xlabel ('t', fontsize = 12)

plt.ylabel ('x (t)', fontsize = 12)

plt.plot (x, f (x), 'g', linewidth = 2)

plt.show ()

3r31189. 3r31190.

3r31228. 3r31224. 3r31224.

3r31228. 3r31201. Decision schedule: [/b]

3r31228.

3r31228. 3r31228.

3r31228. 3r31193. Functions for solving the ODE

3r31228. For those with an analytical solution of ODEs and ODU systems, the function 3r31201 is used. dsolve () [/b] :

3r31228. sympy.solvers.ode. 3r31201. dsolve [/b] (eq, func = None, hint = 'default', simplify = True, ics = None, xi = None, eta = None, x0 = ? n = ? ** kwargs)

3r31228.

3r31228. Let's compare the performance of the dsolve () function with the Laplace method. For example, take the following fourth-degree differential equation with zero initial conditions: 3r3-31205. 3r31228. 3r33939. 3r33942.

3r31228. 3r33939. 3r33947.

3r31228.

3r31228.

Program using the dsolve () function: [/b]

3r31186. 3r31187. import time

start = time.time ()

import numpy as np

from sympy import *

import matplotlib.pyplot as plt

var ('t C1 C2 C3 C4')

u = Function ("u") (t)

de = Eq (u.diff (t, t, t, t) -3 * u.diff (t, t, t) + 3 * u.diff (t, t) -u.diff (t), 4 * t * exp (t)) # Recording a differential equation

des = dsolve (de, u) # Solution of the differential equation 3r31228. # Initial conditions 3r31228. eq1 = des.rhs.subs (t, 0) 3r31228. eq2 = des.rhs.diff (t) .subs (t, 0) 3r3r12228. eq3 = des.rhs.diff (t, t) .subs (t, 0)

eq4 = des.rhs.diff (t, t, t) .subs (t, 0) 3r31228. # Solving a system of algebraic equations for the initial conditions 3r31228. seq = solve ([eq1,eq2-1,eq3-2,eq4-3], C? C? C? C4)

rez = des.rhs.subs ([(C1,seq[C1]), (C? seq[C2]), (C? seq[C3]), (C? seq[C4])])

F = Lambda (t, rez)

f = lambdify (t, rez, 'numpy') 3r3r12228. x = np.linspace (0.6 * np.pi, 100)

stop = time.time () 3r3r122. print ('Decision time for the equation using the dsolve () function:% s s'% round ((stop-start), 3))

plt.title ('Solution using the dsolve () function: nx (t) =% sn'% rez, fontsize = 11)

plt.grid (true)

plt.xlabel ('Time t seconds', fontsize = 12)

plt.ylabel ('f (t)', fontsize = 16)

plt.plot (x, f (x), color = '# 008000', linewidth = 3)

plt.show ()

3r31189. 3r31190.

3r31228. 3r31224. 3r31224.

3r31228. 3r31201. We get: [/b]

3r31228.

3r31228. Time to solve the equation using the dsolve () function: ??? s

3r31228.

3r31228. 3r31228.

3r31228.

The program using the Laplace transform: 3r3-31202.

3r31186. 3r31187. import time

start = time.time ()

from sympy import *

import numpy as np

import matplotlib.pyplot as plt

var ('s')

var ('t', positive = True)

var ('X', cls = Function)

# Initial conditions 3r31228. x0 = 0

x01 = 0

x02 = 0

x03 = 0

g = 4 * t * exp (t) # Write the left side of the differential equation 3r3r12228. Lg = laplace_transform (g, t, s, noconds = True) # The direct Laplace transform

d4 = s ** 4 * X (s) -s ** 3 * x0-s ** 2 * x01-s * x02-x03

d3 = s ** 3 * X (s) -s ** 2 * x0-s * x01-x02

d2 = s ** 2 * X (s) -s * x0-x01

d1 = s * x (s) -x0

d0 = X (s)

d = factor (d4-3 * d3 + 3 * d2- d1) # Write the right side of the differential equation 3r31228. de = Eq (d, Lg) 3r31228. rez = solve (de, X (s))[0]# Solving an algebraic equation

soln = collect (inverse_laplace_transform (rez, s, t), t) # Inverse Laplace transform

f = lambdify (t, soln, 'numpy')

x = np.linspace (0.6 * np.pi, 100)

stop = time.time () 3r3r122. print ('Decision time using the Laplace transform:% s s'% round ((stop-start), 3))

plt.title ('Solution using the Laplace transform: nx (t) =% sn'% soln, fontsize = 11)

plt.grid (true)

plt.xlabel ('t', fontsize = 12)

plt.ylabel ('x (t)', fontsize = 12)

plt.plot (x, f (x), 'g', linewidth = 2)

plt.show ()

3r31189. 3r31190.

3r31228. 3r31224. 3r31224.

3r31228. 3r31201. We get: [/b]

3r31228.

3r31228. Time to solve the equation using the Laplace transform: ??? s

3r31228.

3r31228. 3r31068.

3r31228.

3r31228. So, the dsolve () function (??? s) solves the fourth order equation faster than the solution using the Laplace transform method (??? s) is more than doubled. However, it should be noted that the dsolve () function does not solve the system of second-order differential equations, for example, when solving the system (6) using the dsolve () function, an error occurs: 3r31205. 3r31228.

3r31228. 3r31186. 3r31187. from sympy import *

t = symbols ('t')

x, y = symbols ('x, y', Function = True)

eq = (Eq (Derivative (x (t), t, 2), - 3 * x (t) + y (t)), Eq (Derivative (y (t), t, 2), 2 * x (t ) -2 * y (t) + 40 * sin (3 * t)))

rez = dsolve (eq)

print (list (rez)) 3r31228. 3r31189. 3r31190.

3r31228. 3r31201. We get: [/b]

3r31228.

3r31228. raiseNotImplementedError

3r31228. NotImplementedError

3r31228.

3r31228. This error means that solving a system of differential equations using the function 3r31201. dsolve () [/b] cannot be represented symbolically. Whereas with the help of the Laplace transforms we obtained a symbolic representation of the solution, and this proves the effectiveness of the proposed method.

3r31228.

3r31228. 3r31201. Note. [/b]

3r31228.

3r31228. In order to find the necessary method for solving differential equations using the function 3r31201. dsolve () [/b] , you need to use 3r31201. classify_ode (eq, f (x)) [/b] For example: 3r3-31205. 3r31228.

3r31228. 3r31186. 3r31187. from sympy import *

from IPython.display import *

import matplotlib.pyplot as plt

init_printing (use_latex = True)

x = Symbol ('x')

f = Function ('f')

eq = Eq (f (x) .diff (x, x) + f (x), 0) 3r31228. print (dsolve (eq, f (x))) 3r31228. print (classify_ode (eq, f (x))) 3r31228. eq = sin (x) * cos (f (x)) + cos (x) * sin (f (x)) * f (x) .diff (x) 3r31228. print (classify_ode (eq, f (x))) 3r31228. rez = dsolve (eq, hint = 'almost_linear_Integral')

print (rez) 3r31228. 3r31189. 3r31190.

3r31228. 3r31201. We get: [/b]

3r31228.

3r31228. Eq (f (x), C1 * sin (x) + C2 * cos (x))

3r31228. ('nth_linear_constant_coeff_homogeneous', '2nd_power_series_ordinary')

3r31228. ('separable', '1st_exact', 'almost_linear', '1st_power_series', 'lie_group', 'separable_Integral', '1st_exact_Integral', 'almost_linear_Integral')

3r31228.[Eq(f(x), -acos((C1 + Integral(0, x))*exp(-Integral(-tan(x), x))) + 2*pi), Eq(f(x), acos((C1 + Integral(0,x))*exp(-Integral(-tan(x), x))))]

3r31228.

3r31228. Thus, for the equation * eq = Eq (f (x) .diff (x, x) + f (x), 0) * Any method from the first list works:

3r31228.

3r31228. nth_linear_constant_coeff_homogeneous,

3r31228. 2nd_power_series_ordinary

3r31228.

3r31228. For the equation * eq = sin (x) * cos (f (x)) + cos (x) * sin (f (x)) * f (x) .diff (x) * Any method from the second list works:

3r31228.

3r31228. separable, 1st_exact, almost_linear,

3r31228. 1st_power_series, lie_group, separable_Integral,

3r31228. 1st_exact_Integral, almost_linear_Integral

3r31228.

3r31228. To use the selected method, the dsolve () function entry will look, for example:

3r31228.

3r31228. 3r31186. 3r31187. rez = dsolve (eq, hint = 'almost_linear_Integral')

3r31189. 3r31190.

3r31228. 3r31193. Conclusion:

3r31228. This article aimed to show how to use the SciPy and NumPy libraries using the example of solving linear ODE systems using an operator method. Thus, the methods of symbolic solution of linear differential equations and systems of equations by the Laplace method were considered. The analysis of the performance of this method and the methods implemented in the dsolve () function.

3r31228.

3r31228. 3r31201. References: [/b]

3r31228.

3r31228.

3r31228. 3r31212. Differential equations and boundary value problems: modeling and calculation using Mathematica, Maple and MATLAB. 3rd edition .: Trans. from English - M .: OOO “I.D. Williams ”, 2008. - 1104 pp., Il. - Paral. tit English 3r31215. 3r31228. 3r31212. 3r3r12313. Using the inverse Laplace transform to analyze the dynamic links of control systems 3r31214. 3r31215. 3r31228. 3r31217. 3r31224. 3r31228. 3r31228. 3r31221. ! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d,! 1): e.attachEvent ("onload", d ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () (); 3r31222. 3r31228. 3r31224. 3r31228. 3r31228. 3r31228. 3r31228.

It may be interesting

#### weber

Author**14-10-2018, 07:14**

Publication Date
#### Programming / Mathematics / Python

Category- Comments: 0
- Views: 511

Corvus Health provides medical training services as well as recruiting high quality health workers for you or placing our own best team in your facility. Check Out: Health Workforce Recruitment

I.T HATCH offers a wide range of IT services including remote access setup, small business servers, data storage solutions, IT strategy services, and more. Check Out: IT strategy services