SYM_SOLVE
This function solves algebraic equations symbolically using SymPy. The input equation can be provided either as a single expression interpreted as equal to zero, or as an equality with =.
If an equation is written as f(x) = g(x), the solver rewrites it as:
f(x) - g(x) = 0
and computes symbolic roots for the requested variable.
Excel Usage
=SYM_SOLVE(equation, variable)
equation(str, required): Equation or expression to solve. If no equals sign is present, expression is set equal to zero.variable(str, optional, default: “x”): Variable name to solve for.
Returns (str): String representation of the symbolic solution set.
Example 1: Solve quadratic with two roots
Inputs:
| equation | variable |
|---|---|
| x**2 - 4 | x |
Excel formula:
=SYM_SOLVE("x**2 - 4", "x")
Expected output:
"[-2, 2]"
Example 2: Solve linear equation written with equals sign
Inputs:
| equation | variable |
|---|---|
| 2*x + 3 = 7 | x |
Excel formula:
=SYM_SOLVE("2*x + 3 = 7", "x")
Expected output:
"[2]"
Example 3: Solve cubic polynomial equation
Inputs:
| equation | variable |
|---|---|
| x**3 - x | x |
Excel formula:
=SYM_SOLVE("x**3 - x", "x")
Expected output:
"[-1, 0, 1]"
Example 4: Solve using non-default variable
Inputs:
| equation | variable |
|---|---|
| t**2 - 9 | t |
Excel formula:
=SYM_SOLVE("t**2 - 9", "t")
Expected output:
"[-3, 3]"
Python Code
import sympy as sp
from sympy import Symbol
from sympy import solve as sympy_solve
def sym_solve(equation, variable='x'):
"""
Solve an algebraic equation for a variable.
See: https://docs.sympy.org/latest/modules/solvers/solvers.html#sympy.solvers.solvers.solve
This example function is provided as-is without any representation of accuracy.
Args:
equation (str): Equation or expression to solve. If no equals sign is present, expression is set equal to zero.
variable (str, optional): Variable name to solve for. Default is 'x'.
Returns:
str: String representation of the symbolic solution set.
"""
try:
variable_symbol = Symbol(variable)
normalized_equation = equation.replace("^", "**")
if "=" in normalized_equation:
lhs, rhs = normalized_equation.split("=", 1)
expr = sp.sympify(lhs) - sp.sympify(rhs)
else:
expr = sp.sympify(normalized_equation)
result = sympy_solve(expr, variable_symbol)
return str(result)
except Exception as e:
return f"Error: {str(e)}"Online Calculator
Equation or expression to solve. If no equals sign is present, expression is set equal to zero.
Variable name to solve for.