Search code examples
pythonnumpymathsympydifferential-equations

Sin function not working with Differential Equation


I am trying to get the differential equation y'=sin(x) however my differential equation will not run any further as I get the error "can't convert expression to float". If I use numpy with np.sin(x), I get another error "loop of ufunc does not support argument 0 of type Symbol which has no callable sin method". Here is the code:

import sympy as sym
from math import *

x = sym.symbols('x')
y = sym.Function('y')
diffeq = sym.Eq(y(x).diff(x), sin(x))

If anyone can show me where I am going wrong that would be a great help as it seems quite simple....


Solution

  • You are calling sin() with an argument that it doesn't support. math.sin() must take a numerical value as its only argument.

    I think to fix the problem, you just need to pass the function's name instead of calling it:

    diffeq = sym.Eq(y(x).diff(x), sin)
    

    Or else, you need to use sym.sin instead of math.sin:

    diffeq = sym.Eq(y(x).diff(x), sym.sin(x))