Reputation: 25
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)
Upvotes: 1
Views: 157
Reputation: 6547
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)
Upvotes: 1