1

I am trying to use sympy to solve a simple 2nd order ODE: gamma*y''(x) + 4*y(x)=0. I keep getting an error "name x is not defined". And everytime I try to correct one error, another error pops up. Please could someone tell me what is wrong with my code. Also, how do I plot the solution?

Originally I have issues with:

from sympy import* 
from sympy.solvers import dsolve
import sympy as sp
from pylab import*
dsolve?
f = sp.symbols("f", cls=sp.Function)
f(x)
ode1 = gamma*f(x).diff(x,2) + 4*f(x)
f_init = [1,0]
gamma = 0.01
sol1 = dsolve(ode1, f(x), f_init)
print(ode1, sol1)


UPDATE: this made it was what I was looking for to make it work for me:

from sympy import* 
from sympy.solvers import dsolve
import sympy as sp
from pylab import*
import matplotlib.pyplot as plt

x = sp.symbols('x')
f = sp.Function("f")

gamma = 0.01
ode1 = sp.Eq(gamma*f(x).diff(x,2)+4*x)
sol1 = sp.dsolve(ode1, ics={f(0): 0, sp.diff(f(x), x).subs(x,0): 1})

print(ode1, sol1)
denis_lor
  • 6,212
  • 4
  • 31
  • 55
John Rambo
  • 25
  • 6
  • Remember, `sympy` runs in Python. Variables have be defined before they are used. You define `f`, but not `x`. Usually `sympy` has a line like `x = symbols('x')`, which defines the Python variable as a sympy symbol (with name 'x'). Don't skip over the `sympy` tutorial too fast. – hpaulj Feb 07 '20 at 23:09
  • `import *` is discouraged. Always share the entire error message. – AMC Feb 08 '20 at 00:52

1 Answers1

1

At least this should be a start, but then not sure what would you like to achieve further, take a look at:

from sympy.solvers import dsolve
import sympy as sp

x = sp.symbols('x')
f = sp.Function("f")
gamma = 0.01
ode1 = sp.Eq(gamma*f(x).diff(x,2)+4*x)
sol1 = sp.dsolve(ode1, ics={f(0): 0, sp.diff(f(x), x).subs(x,0): 1})

print(ode1, sol1)

Prints out:

Eq(4*x + 0.01*Derivative(f(x), (x, 2)), 0) Eq(f(x), -200*x**3/3 + x)

denis_lor
  • 6,212
  • 4
  • 31
  • 55
  • 1
    Denis, thank you. Your code works. I can now build on it. – John Rambo Feb 07 '20 at 23:28
  • Just one more question, if I may. When I modify your dsolve so that it reads sol1 = dsolve(ode1, f, ics={x(0): 0, sp.diff(f(x), x).subs(x,0): 3}), I get the error TypeError: 'f' object is not callable. Any idea why? – John Rambo Feb 08 '20 at 00:28
  • Is the problem with `f(x)`? `f` is a sympy `Function` object. It is not a python function. – hpaulj Feb 08 '20 at 00:46
  • Hi. I've just had a look at https://stackoverflow.com/questions/58034695/sympy-solving-differential-equation-with-initial-conditions-error. Based on that, I modified the line f = Function("f")(x) to f = Function("f"). However, I still get the error: 'f' object is not callable. If I delete the initial condition parameter from dsolve, there is no error and I get the solution in terms of constants of integration. I have no idea how to overcome this. – John Rambo Feb 08 '20 at 02:02
  • @JohnRambo What about `from sympy.solvers import dsolve import sympy as sp x = sp.symbols('x') f = sp.Function("f") gamma = 0.01 ode1 = sp.Eq(f(x).diff(x,2)*gamma + 4*x) sol1 = sp.dsolve(ode1) print(ode1, sol1)` – denis_lor Feb 08 '20 at 08:53
  • `dsolve` will automatically figure out the function for which to solve so that is ok. If you pass the function it should be the `f(x)` not the `f`. – smichr Feb 08 '20 at 13:21
  • 1
    Everyone, thanks for your replies. I finally got the code to run, please see edited code. – John Rambo Feb 08 '20 at 19:31
  • I think it should say `4*f(x)` not `4*x`. not that you despair looking for that one later. – Silly Freak Feb 08 '20 at 20:49