viernes, 29 de septiembre de 2023

sympy odes, ivp python - Resolver ODES Ecuaciones diferenciales y PVI con python

 sympy odes, ivp python - Resolver ODES Ecuaciones diferenciales y PVI con python

https://12000.org/my_notes/faq/sympy_python/index.htm

  1. HOME
  2. PDF (letter size)

my sympy and python cheat sheet


September 8, 2023   Compiled on September 8, 2023 at 6:05pm

Contents

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'
 

How to solve a first order ODE?

Solve ()=1+2 for ()

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')
 

How to solve a first oder ODE with initial condition?

Solve ()=1+2 for () with (0)=3

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)
 

How to solve a second order ODE?

Solve 9()+22()=0

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

()=1sin(3)+2cos(3)

How to solve and ODE and convert the result to latex string?

Solve ()=1+2 for () with (0)=3

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)
 

()=2++3

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)
 

(,)=(+)

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
 

How to find function name and its arguments in a proc?

Suppose one passes () to a function, and the function wants to find the name of this function and its argument. Here is an example

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