sympy odes, ivp python - Resolver ODES Ecuaciones diferenciales y PVI con python
https://12000.org/my_notes/faq/sympy_python/index.htm
my sympy and python cheat sheet
Contents
2 How to solve a first order ODE?
3 How to solve a first oder ODE with initial condition?
4 How to solve a second order ODE?
5 How to solve and ODE and convert the result to latex string?
6 How to solve a PDE in sympy?
7 How to check if something is derivative?
8 How to find function name and its arguments in a proc?
1 Installing sympy 1.10.1 on Ubuntu
Installing Python 3.10 and sympy 1.10.1 was very tricky. First Ubuntu 20.04 came with Python 3.8 and it was struggle to make it use Python 3.10 instead. After 2 hrs trying many commands, finally it seems to be using 3.10 now. Next did the following
>which python /bin/python >python --version Python 3.10.4
Now
python -m pip install sympy
Gives
Defaulting to user installation because normal site-packages is not writeable Collecting sympy Using cached sympy-1.10.1-py3-none-any.whl (6.4 MB) Collecting mpmath>=0.19 Using cached mpmath-1.2.1-py3-none-any.whl (532 kB) Installing collected packages: mpmath, sympy Successfully installed mpmath-1.2.1 sympy-1.10.1
And now
>python Python 3.10.4 (main, Mar 24 2022, 16:12:56) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sympy >>> sympy.__version__ '1.10.1'
2 How to solve a first order ODE?
Solve
from sympy import * x = symbols('x') y = Function('y') ode = Eq(Derivative(y(x),x),1+2*x) sol = dsolve(ode,y(x)) # Eq(y(x), C1 + x**2 + x) checkodesol(ode,sol) # (True, 0) if checkodesol(ode,sol)[0]==True: print ('verified solution OK')
3 How to solve a first oder ODE with initial condition?
Solve
import sympy x = sympy.symbols('x') y = sympy.Function('y') ode = sympy.Eq(sympy.Derivative(y(x),x),1+2*x) sol = sympy.dsolve(ode,y(x),ics={y(0):3}) # Eq(y(x), x**2 + x + 3) sympy.checkodesol(ode,sol) # (True, 0)
4 How to solve a second order ODE?
Solve
from sympy import Function,dsolve,Derivative,Eq x=sympy.symbols('x') y=sympy.symbols('y', cls=Function) ode=Eq(Derivative(y(x), x, x) + 9*y(x),0) dsolve(ode, y(x))
gives
5 How to solve and ODE and convert the result to latex string?
Solve
import sympy x = sympy.symbols('x') y = sympy.Function('y') ode = sympy.Eq(sympy.Derivative(y(x),x),1+2*x) sol = sympy.dsolve(ode,y(x),ics={y(0):3}) # Eq(y(x), x**2 + x + 3) sympy.latex(sol)
6 How to solve a PDE in sympy?
PDE solving is still limited in sympy. Here is how to solve first order pde
Solve
import sympy as sp x,t = sp.symbols('x t') u = sp.Function('u') pde = sp.Eq( sp.diff(u(x,t),t) , sp.diff(u(x,t),x)) sol = sp.pdsolve(pde) sp.latex(sol)
7 How to check if something is derivative?
import sympy x = sympy.symbols('x') y = sympy.Function('y') expr = sympy.Derivative(y(x),x) type(expr) is sympy.Derivative #True if type(expr) is sympy.Derivative: print("yes") #yes
This also works, which seems to be the more prefered way
isinstance(expr,sympy.Derivative) #True
8 How to find function name and its arguments in a proc?
Suppose one passes
def process(the_function): print("the function argument is ", the_function.args[0]) print("the function name itself is ", the_function.name) import sympy x = sympy.symbols('x') y = sympy.Function('y') process(y(x))
This prints
the function argument is x the function name itself is y
No hay comentarios:
Publicar un comentario