Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

MATLAB Equation Solver: Solve Equations in Code

Learn how to use the MATLAB equation solver, including solve, vpasolve, and fzero. Step-by-step code for single, 3×3, and nonlinear systems in research.

Written and maintained by CASRAI Editorial Board

Last updated

The MATLAB equation solver refers to built-in functions in MATLAB and the Symbolic Math Toolbox—principally solve, vpasolve, and fzero—used to calculate exact analytical roots, variable-precision numerical approximations, and continuous function zeros. Researchers employ these solvers across engineering, computational biology, and physics to resolve algebraic equations, systems of nonlinear equations, and constrained mathematical models without manual derivation.

What it is used for in research

In scientific research and quantitative modeling, algebraic and transcendental equations arise routinely when balancing thermodynamic equilibria, solving kinematic loop closures, analyzing circuit nodal voltages, or determining steady-state concentrations in biochemical pathways. Manually isolating variables in nonlinear systems is often mathematically impossible or prone to transcription errors. MATLAB provides both symbolic and numeric equation-solving engines to automate this process.

Depending on whether a research problem demands an exact symbolic expression, high-precision numerical root, or fast double-precision iteration, researchers select among several dedicated solvers. Symbolic variables are defined using symbolic environment declarations, as detailed in the guide on MATLAB syms.

Research Task Recommended Solver Required Toolbox Computation Engine Primary Output Type
Exact algebraic and polynomial roots solve Symbolic Math Toolbox Analytical symbolic algebra Exact symbolic expression or vector (sym)
High-precision roots for non-elementary equations vpasolve Symbolic Math Toolbox Variable-precision arithmetic Arbitrary-precision floating point (sym)
Single-variable continuous function zero fzero MATLAB (Base) Interpolation and bisection Double-precision floating point (double)
Multivariate systems of linear equations (mldivide) or linsolve MATLAB (Base) Gaussian elimination / LAPACK Matrix column vector (double)
Multivariate nonlinear numerical systems fsolve Optimization Toolbox Trust-region dogleg / Levenberg-Marquardt Numerical coordinate vector (double)

For dynamic models governed by rates of change rather than static algebraic balances, researchers transition from algebraic solvers to differential equation solvers such as ode45 in MATLAB.

Install or access (official source only)

MATLAB is commercial scientific computing software developed and distributed by MathWorks. Access and installation proceed through official MathWorks channels:

  1. Institutional Portal Access: Most academic researchers, university faculty, and graduate students access MATLAB under an institutional campus-wide software license agreement. Navigate to your university’s MathWorks portal, sign in using single sign-on (SSO) credentials, and link your university email to a MathWorks Account.
  2. Desktop Installation: Download the official MathWorks Installer from MathWorks (mathworks.com). During installation, select core MATLAB (which includes fzero, roots, and linear algebra solvers) and the Symbolic Math Toolbox (required for solve, vpasolve, and symbolic syntax).
  3. Verification of Toolboxes: Open MATLAB and verify installed toolboxes by typing ver in the Command Window, or check the Symbolic Math Toolbox specifically by running ver('symbolic').
  4. MATLAB Online: Researchers without local administrative privileges can access the full computational environment via a modern web browser at MATLAB Online (matlab.mathworks.com). Cloud compute resources and cloud drive storage synchronize directly with your MathWorks Account.

Step-by-step task (numbered, with menu names or commands exactly as in the current docs)

The following procedures illustrate the primary equation-solving workflows in MATLAB, starting with a single symbolic equation and progressing to multivariable systems and numeric root-finding.

Task 1: Solving a Single Algebraic Equation Symbolically

  1. Define the symbolic variable: Open the MATLAB Editor or Command Window and declare the independent variable using syms:
    syms x
  2. Formulate the equation: Define the equation using the relational equality operator ==. For example, to set up the quadratic relation x² – 5x + 6 = 0:
    eqn = x^2 - 5*x + 6 == 0;
  3. Execute the solve command: Pass the equation and target variable to solve:
    S = solve(eqn, x);
  4. Filter real solutions: If an equation yields complex roots but your physical domain requires real values, specify the Real name-value argument:
    syms y
    eqn_cubic = y^3 - 8 == 0;
    S_real = solve(eqn_cubic, y, 'Real', true);

Task 2: Solving a 3×3 System of Simultaneous Equations

Researchers frequently model three-dimensional equilibrium, multi-loop electrical circuits, or three-species stoichiometric balances requiring a 3×3 equation solver.

  1. Declare all variables:
    syms x y z
  2. Construct the system array: Store the three equations into an array of symbolic relations:
    eqns = [
        x + 2*y - z == 4,
        2*x - y + 3*z == 13,
        3*x + y + z == 11
    ];
  3. Call solve with multiple variables:
    S = solve(eqns, [x, y, z]);
  4. Extract variable solutions from the output structure:
    sol_x = double(S.x);
    sol_y = double(S.y);
    sol_z = double(S.z);

Task 3: Finding Numerical Roots with fzero (Base MATLAB)

When an equation involves transcendental functions that lack analytical closed forms and the Symbolic Math Toolbox is not required, use fzero to find where a continuous function crosses zero.

  1. Define an anonymous function: Represent the function f(x) = cos(x) – x where f(x) = 0:
    fun = @(x) cos(x) - x;
  2. Select an initial point or bracket: Provide a scalar starting guess (e.g., x0 = 0.5) or a two-element bracket where the function values differ in sign (e.g., [0, 1]):
    x0 = [0, 1];
  3. Compute the root and inspect solver diagnostics:
    [x_root, fval, exitflag, output] = fzero(fun, x0);

Output and how to read it

Interpreting MATLAB solver outputs correctly depends on the solver function invoked and the number of variables analyzed:

  • Single-Variable solve Output: Returns a symbolic column vector containing all analytical solutions. For a quadratic equation, S contains two symbolic numbers or expressions. Use double(S) to convert exact radical or rational fractions into standard floating-point numbers.
  • Multivariate solve Output Structure: When solving for multiple variables, solve returns a scalar structure array where each field corresponds to a variable name (e.g., S.x, S.y, S.z). If a nonlinear system possesses multiple discrete solution sets, each field contains a vector of equal length representing corresponding coordinates.
  • Parametric Solutions with ReturnConditions: For periodic or underdetermined systems (such as sin(x) = 0), calling [sol, params, conds] = solve(eqn, x, 'ReturnConditions', true) outputs the solution set alongside generated parameter symbols (such as k representing arbitrary integers) and validity conditions.
  • fzero Diagnostic Outputs:
    • x_root: The calculated zero location.
    • fval: The value of fun(x_root), which should be within machine precision of zero.
    • exitflag: An integer signaling convergence status. An exitflag of 1 confirms that fzero found a zero where the function changes sign. A negative value indicates failure or termination.
    • output: A structure reporting computational metrics, including output.iterations (number of iterations) and output.funcCount (total function evaluations).

Troubleshooting

When running equation-solving routines in MATLAB, researchers commonly encounter several predictable syntax and mathematical roadblocks:

  • Issue: Solver returns Empty sym: 0-by-1.

    Cause: The equation possesses no solution, or solve cannot compute an exact analytical closed form under default settings.

    Resolution: Verify equation syntax. If an analytical solution does not exist, switch to numerical symbolic solving with vpasolve(eqn, var), which computes variable-precision approximations for nonlinear expressions.
  • Issue: Error “Invalid expression” or unexpected variable reassignment.

    Cause: Using a single equals sign (=) instead of a double equals sign (==) when defining the equation. A single = performs variable assignment, overwriting your symbolic variable with a logical evaluation or numeric expression.

    Resolution: Always construct symbolic equations using the double equals comparison operator: eqn = a*x + b == 0;.
  • Issue: fzero error: “Function values at interval endpoints must be of opposite signs.”

    Cause: When passing a two-element bracket [a, b] to fzero, fun(a) and fun(b) must have opposite signs so that the Intermediate Value Theorem guarantees a root. Furthermore, fzero cannot find roots where a function touches zero without crossing the axis (such as f(x) = x²).

    Resolution: Plot the function across the candidate domain using fplot(fun, [x_min, x_max]) to locate a valid sign-crossing bracket, or pass a single scalar guess x0 rather than an interval.
  • Issue: Complex or extraneous roots returned for physical quantities.

    Cause: Polynomial and transcendental equations often have mathematical solutions in the complex plane that have no physical validity in experimental systems.

    Resolution: Add assumptions to your symbolic variables before calling the solver (e.g., assume(x > 0)), or set the name-value argument 'Real', true in solve. Clear assumptions after computation using assume(x, 'clear').

Free alternatives (table)

For open-science reproducibility or environments without commercial MATLAB licenses, several validated open-source software libraries provide comparable analytical and numerical equation-solving capabilities.

Software / Library Equation Solver Function Primary Capability License Key Syntax Difference from MATLAB
GNU Octave fsolve Numerical systems of nonlinear equations (MINPACK algorithm) GNU GPL v3+ High command-level script compatibility; does not require proprietary toolboxes for numeric solving.
GNU Octave roots Polynomial root calculation via companion matrix eigenvalues GNU GPL v3+ Identical vector-coefficient syntax: roots([a, b, c]).
Python (SciPy) scipy.optimize.root_scalar Single-variable scalar root-finding (Brent, Newton, Halley) BSD-3-Clause Requires explicit method parameter (e.g. method='brentq') and Python function callable.
Python (SciPy) scipy.optimize.fsolve Multivariate nonlinear numerical equation solver BSD-3-Clause Vectorized callback architecture; requires NumPy array inputs and outputs.
Python (SymPy) sympy.solvers.solve Exact analytical symbolic equation solving BSD-3-Clause Symbolic variables declared via symbols('x'); equations defined as Eq(lhs, rhs).

Specialized research domains also integrate these mathematical solvers with kinetics packages such as Cantera to solve multicomponent chemical equilibrium and reaction network equations.

FAQ

How do I solve a 3×3 system of equations in MATLAB?

To solve a 3×3 system symbolically, declare your variables with syms x y z, group the three equations in a vector eqns = [eqn1, eqn2, eqn3], and call S = solve(eqns, [x, y, z]). The output S is a structure with fields S.x, S.y, and S.z containing the exact solutions. For purely linear systems, formulate the matrix equation Ax = b and compute x = A b in base MATLAB.

What is the difference between solve and fzero in MATLAB?

The solve function belongs to the Symbolic Math Toolbox and finds exact analytical closed-form expressions or variable-precision roots for algebraic equations. In contrast, fzero is a base MATLAB function that uses numerical interpolation and bisection to find a single point where a continuous scalar function changes sign, requiring no symbolic toolbox.

How do I restrict the MATLAB equation solver to real solutions?

When using solve, include the name-value argument 'Real', true in the function call: S = solve(eqn, var, 'Real', true). Alternatively, assign a symbolic assumption prior to solving using assume(var, 'real') or assume(var > 0), and clear the assumption afterward with assume(var, 'clear').

Can I solve equations in MATLAB Online without a desktop install?

Yes. MATLAB Online provides access to the complete MATLAB environment and licensed toolboxes, including the Symbolic Math Toolbox, through standard web browsers. It runs on MathWorks cloud infrastructure and supports identical solve, vpasolve, and fzero workflows without local software installation.

Why does solve return an empty symbolic object?

An empty output (Empty sym: 0-by-1) indicates that no analytical solution exists or that MATLAB cannot express the root in elementary closed-form functions under given assumptions. In such cases, use vpasolve to calculate a numerical approximation or define initial search intervals.

Sources (official docs)

  • MathWorks. (2026). solve: Equations and systems solver. MathWorks Help Center. https://www.mathworks.com/help/symbolic/solve.html (Reviewed 2026-11-07).
  • MathWorks. (2026). fzero: Root of nonlinear function. MathWorks Help Center. https://www.mathworks.com/help/matlab/ref/fzero.html (Reviewed 2026-11-07).
  • MathWorks. (2026). Choose an Approach for Solving Equations Using solve Function. MathWorks Help Center. https://www.mathworks.com/help/symbolic/choose-an-approach-for-solving-equations-using-solve-function.html (Reviewed 2026-11-07).
  • MathWorks. (2026). vpasolve: Solve equations numerically using variable-precision arithmetic. MathWorks Help Center. https://www.mathworks.com/help/symbolic/vpasolve.html (Reviewed 2026-11-07).
  • GNU Octave. (2026). Nonlinear Equations. GNU Octave Documentation. https://docs.octave.org/latest/Nonlinear-Equations.html (Reviewed 2026-11-07).
  • SciPy Community. (2026). scipy.optimize.root_scalar. SciPy Reference Guide. https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.root_scalar.html (Reviewed 2026-11-07).

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about MATLAB Equation Solver: Solve Equations in Code

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →