dsolve_ode
Solve ordinary differential equations (ODEs) using SymPy’s dsolve function. Input an expression and function name to compute and return the solution in LaTeX format. Supports optional solving hints for specific methods.
Instructions
Solves an ordinary differential equation using SymPy's dsolve function.
Args:
expr_key: The key of the expression (previously introduced) containing the differential equation.
func_name: The name of the function (previously introduced) to solve for.
hint: Optional solving method from ODEHint enum. If None, SymPy will try to determine the best method.
Example:
# First introduce a variable and a function
intro("x", [Assumption.REAL], [])
introduce_function("f")
# Create a second-order ODE: f''(x) + 9*f(x) = 0
expr_key = introduce_expression("Derivative(f(x), x, x) + 9*f(x)")
# Solve the ODE
result = dsolve_ode(expr_key, "f")
# Returns solution with sin(3*x) and cos(3*x) terms
Returns:
A LaTeX string representing the solution. Returns an error message string if issues occur.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expr_key | Yes | ||
| func_name | Yes | ||
| hint | No |
Implementation Reference
- server.py:515-568 (handler)The main handler function ('dsolve_ode') for the MCP tool. It implements the core logic: validates inputs, constructs the ODE equation, invokes sympy.dsolve (with optional hint), and formats the solution as LaTeX.def dsolve_ode(expr_key: str, func_name: str, hint: Optional[ODEHint] = None) -> str: """Solves an ordinary differential equation using SymPy's dsolve function. Args: expr_key: The key of the expression (previously introduced) containing the differential equation. func_name: The name of the function (previously introduced) to solve for. hint: Optional solving method from ODEHint enum. If None, SymPy will try to determine the best method. Example: # First introduce a variable and a function intro("x", [Assumption.REAL], []) introduce_function("f") # Create a second-order ODE: f''(x) + 9*f(x) = 0 expr_key = introduce_expression("Derivative(f(x), x, x) + 9*f(x)") # Solve the ODE result = dsolve_ode(expr_key, "f") # Returns solution with sin(3*x) and cos(3*x) terms Returns: A LaTeX string representing the solution. Returns an error message string if issues occur. """ if expr_key not in expressions: return f"Error: Expression with key '{expr_key}' not found." if func_name not in functions: return f"Error: Function '{func_name}' not found. Please introduce it first using introduce_function." expression = expressions[expr_key] try: # Convert to equation form if it's not already if isinstance(expression, sympy.Eq): eq = expression else: eq = sympy.Eq(expression, 0) # Let SymPy handle function detection and apply the specified hint if provided if hint is not None: solution = dsolve(eq, hint=hint.value) else: solution = dsolve(eq) # Convert the solution to LaTeX format latex_output = sympy.latex(solution) return latex_output except ValueError as e: return f"Error: {str(e)}. This might be due to an invalid hint or an unsupported equation type." except NotImplementedError as e: return f"Error: Method not implemented: {str(e)}. Try a different hint or equation type." except Exception as e: return f"An unexpected error occurred: {str(e)}"
- vars.py:39-107 (schema)Enum class ODEHint providing the allowed values for the optional 'hint' parameter in dsolve_ode, defining the SymPy dsolve hint strategies for input validation.class ODEHint(Enum): FACTORABLE = "factorable" NTH_ALGEBRAIC = "nth_algebraic" SEPARABLE = "separable" FIRST_EXACT = "1st_exact" FIRST_LINEAR = "1st_linear" BERNOULLI = "Bernoulli" FIRST_RATIONAL_RICCATI = "1st_rational_riccati" RICCATI_SPECIAL_MINUS2 = "Riccati_special_minus2" FIRST_HOMOGENEOUS_COEFF_BEST = "1st_homogeneous_coeff_best" FIRST_HOMOGENEOUS_COEFF_SUBS_INDEP_DIV_DEP = ( "1st_homogeneous_coeff_subs_indep_div_dep" ) FIRST_HOMOGENEOUS_COEFF_SUBS_DEP_DIV_INDEP = ( "1st_homogeneous_coeff_subs_dep_div_indep" ) ALMOST_LINEAR = "almost_linear" LINEAR_COEFFICIENTS = "linear_coefficients" SEPARABLE_REDUCED = "separable_reduced" FIRST_POWER_SERIES = "1st_power_series" LIE_GROUP = "lie_group" NTH_LINEAR_CONSTANT_COEFF_HOMOGENEOUS = "nth_linear_constant_coeff_homogeneous" NTH_LINEAR_EULER_EQ_HOMOGENEOUS = "nth_linear_euler_eq_homogeneous" NTH_LINEAR_CONSTANT_COEFF_UNDETERMINED_COEFFICIENTS = ( "nth_linear_constant_coeff_undetermined_coefficients" ) NTH_LINEAR_EULER_EQ_NONHOMOGENEOUS_UNDETERMINED_COEFFICIENTS = ( "nth_linear_euler_eq_nonhomogeneous_undetermined_coefficients" ) NTH_LINEAR_CONSTANT_COEFF_VARIATION_OF_PARAMETERS = ( "nth_linear_constant_coeff_variation_of_parameters" ) NTH_LINEAR_EULER_EQ_NONHOMOGENEOUS_VARIATION_OF_PARAMETERS = ( "nth_linear_euler_eq_nonhomogeneous_variation_of_parameters" ) LIOUVILLE = "Liouville" SECOND_LINEAR_AIRY = "2nd_linear_airy" SECOND_LINEAR_BESSEL = "2nd_linear_bessel" SECOND_HYPERGEOMETRIC = "2nd_hypergeometric" SECOND_HYPERGEOMETRIC_INTEGRAL = "2nd_hypergeometric_Integral" NTH_ORDER_REDUCIBLE = "nth_order_reducible" SECOND_POWER_SERIES_ORDINARY = "2nd_power_series_ordinary" SECOND_POWER_SERIES_REGULAR = "2nd_power_series_regular" NTH_ALGEBRAIC_INTEGRAL = "nth_algebraic_Integral" SEPARABLE_INTEGRAL = "separable_Integral" FIRST_EXACT_INTEGRAL = "1st_exact_Integral" FIRST_LINEAR_INTEGRAL = "1st_linear_Integral" BERNOULLI_INTEGRAL = "Bernoulli_Integral" FIRST_HOMOGENEOUS_COEFF_SUBS_INDEP_DIV_DEP_INTEGRAL = ( "1st_homogeneous_coeff_subs_indep_div_dep_Integral" ) FIRST_HOMOGENEOUS_COEFF_SUBS_DEP_DIV_INDEP_INTEGRAL = ( "1st_homogeneous_coeff_subs_dep_div_indep_Integral" ) ALMOST_LINEAR_INTEGRAL = "almost_linear_Integral" LINEAR_COEFFICIENTS_INTEGRAL = "linear_coefficients_Integral" SEPARABLE_REDUCED_INTEGRAL = "separable_reduced_Integral" NTH_LINEAR_CONSTANT_COEFF_VARIATION_OF_PARAMETERS_INTEGRAL = ( "nth_linear_constant_coeff_variation_of_parameters_Integral" ) NTH_LINEAR_EULER_EQ_NONHOMOGENEOUS_VARIATION_OF_PARAMETERS_INTEGRAL = ( "nth_linear_euler_eq_nonhomogeneous_variation_of_parameters_Integral" ) LIOUVILLE_INTEGRAL = "Liouville_Integral" SECOND_NONLINEAR_AUTONOMOUS_CONSERVED = "2nd_nonlinear_autonomous_conserved" SECOND_NONLINEAR_AUTONOMOUS_CONSERVED_INTEGRAL = ( "2nd_nonlinear_autonomous_conserved_Integral" )