Professional Documents
Culture Documents
Solving Differential Equations Using Computer With SymPy 1st Sem 2021 2022 Topic Presentation
Solving Differential Equations Using Computer With SymPy 1st Sem 2021 2022 Topic Presentation
joseemalyn.wordpress.com 2
Types of Computer Calculations
2. Symbolic Solution
➢ Computers represent values using both symbols and numbers.
➢ Software that solve symbolically are called “computer algebra systems” (CAS).
➢ To solve expressions and equations, computers need to manipulate the symbols
in addition to running operations on the numbers.
➢ Functions and irrational constants are generally stored as symbols.
➢ CAS software can determine the general solution of differential equations directly.
➢ However, not all equations can be solved symbolically. A computer can only solve
equations for which it has been programmed to solve.
➢ In this discussion, symbolic solutions are used to solve differential equations.
joseemalyn.wordpress.com 3
Python…
joseemalyn.wordpress.com 4
SymPy Library
joseemalyn.wordpress.com 5
Google Colab
joseemalyn.wordpress.com 6
Link to Google Colab Notebook
Try it yourself: (Note: you need a Google account to run the code snippets)
https://colab.research.google.com/drive/195B2TEApFjTC7CLotjSkzXhpnRK5AEcp?usp=sharing
joseemalyn.wordpress.com 7
Import SymPy Library
• The SymPy library and its functions and classes must be imported at
the start of the program.
joseemalyn.wordpress.com 8
Define Variables and Functions
and the Differential Equation
• In our first example, the differential
equation can be defined using x and y,
where y is a function of x.
• We define the variable x as a SymPy
symbol, and define y as a SymPy function.
• To define a differential equation, we use
the Eq() function – this has two parameters
separated by a comma:
✓ expression at the left side of the equation
✓ expression at the right side of the equation
joseemalyn.wordpress.com 9
Define Variables and Functions
and the Differential Equation cont.
• Since we defined y as a function of x, we
need to always put y(x) instead of just y.
• Addition/subtraction and grouping is
formatted similar to math, by using the ‘+’,
‘-’, ‘(‘, and ‘)’ symbols.
• Multiplication and division is represented
using the ‘*’ and ‘/’ symbols.
• Exponentiation is represented by ‘**’ (two
asterisks).
• Differentiation is done by adding “.diff()” to
an expression. For example, y(x).diff(x, 2)
𝑑2𝑦
means
𝑑𝑥 2
joseemalyn.wordpress.com 10
Classify the Type of ODE
• Based on the form of the equation, SymPy can classify the differential equation into
different types.
• For each type, SymPy already has a programmed set of steps for the solution.
joseemalyn.wordpress.com 11
Solve the Differential Equation
• We can then run dsolve() on the differential equation to get its general solution.
joseemalyn.wordpress.com 12
Tips…
• To run individual cells, click the “Play” button beside the cell, or press
“Ctrl+Enter”.
• The output should appear below the code cell.
• Make sure all SymPy variables are defined.
joseemalyn.wordpress.com 13