DSOLVE
This function solves ordinary differential equations (ODEs) symbolically using SymPy. The equation is provided as a string, and a dependent function with an independent variable is specified.
A first-order ODE has the general form:
F\left(x, y(x), y'(x)\right) = 0
and the function returns a symbolic closed-form solution when available.
Excel Usage
=DSOLVE(equation, independent, dependent)
equation(str, required): Differential equation string, typically using Eq(…) and Derivative(…).independent(str, optional, default: “x”): Independent variable name.dependent(str, optional, default: “y”): Dependent function name.
Returns (str): String representation of the symbolic ODE solution.
Example 1: Solve first-order exponential growth ODE
Inputs:
| equation | independent | dependent |
|---|---|---|
| Eq(Derivative(y(x), x), y(x)) | x | y |
Excel formula:
=DSOLVE("Eq(Derivative(y(x), x), y(x))", "x", "y")
Expected output:
"Eq(y(x), C1*exp(x))"
Example 2: Solve first-order linear ODE with constant forcing
Inputs:
| equation | independent | dependent |
|---|---|---|
| Eq(Derivative(y(x), x), 2) | x | y |
Excel formula:
=DSOLVE("Eq(Derivative(y(x), x), 2)", "x", "y")
Expected output:
"Eq(y(x), C1 + 2*x)"
Example 3: Solve second-order harmonic oscillator ODE
Inputs:
| equation | independent | dependent |
|---|---|---|
| Eq(Derivative(y(x), x, x) + y(x), 0) | x | y |
Excel formula:
=DSOLVE("Eq(Derivative(y(x), x, x) + y(x), 0)", "x", "y")
Expected output:
"Eq(y(x), C1*sin(x) + C2*cos(x))"
Example 4: Solve ODE with custom variable and function names
Inputs:
| equation | independent | dependent |
|---|---|---|
| Eq(Derivative(f(t), t), f(t)) | t | f |
Excel formula:
=DSOLVE("Eq(Derivative(f(t), t), f(t))", "t", "f")
Expected output:
"Eq(f(t), C1*exp(t))"
Python Code
import sympy as sp
from sympy import Symbol
from sympy import Function
from sympy import dsolve as sympy_dsolve
def dsolve(equation, independent='x', dependent='y'):
"""
Solve an ordinary differential equation symbolically.
See: https://docs.sympy.org/latest/modules/solvers/ode.html#sympy.solvers.ode.dsolve
This example function is provided as-is without any representation of accuracy.
Args:
equation (str): Differential equation string, typically using Eq(...) and Derivative(...).
independent (str, optional): Independent variable name. Default is 'x'.
dependent (str, optional): Dependent function name. Default is 'y'.
Returns:
str: String representation of the symbolic ODE solution.
"""
try:
independent_symbol = Symbol(independent)
dependent_function = Function(dependent)
local_scope = {
independent: independent_symbol,
dependent: dependent_function,
"Eq": sp.Eq,
"Derivative": sp.Derivative,
}
normalized_equation = equation.replace("^", "**")
expr = sp.sympify(normalized_equation, locals=local_scope)
result = sympy_dsolve(expr, dependent_function(independent_symbol))
return str(result)
except Exception as e:
return f"Error: {str(e)}"Online Calculator
Differential equation string, typically using Eq(...) and Derivative(...).
Independent variable name.
Dependent function name.