Skip to Content

JACOBIAN_MATRIX

Overview

The JACOBIAN_MATRIX matrix is a fundamental concept in multivariable calculus and mathematical optimization. It generalizes the derivative of a function to higher dimensions. For a vector-valued function f:RnRm\mathbf{f}: \mathbb{R}^n \to \mathbb{R}^m, the JACOBIAN_MATRIX matrix JJ is an m×nm \times n matrix where each entry is the partial derivative of one component of f\mathbf{f} with respect to one variable:

J=[fixj],Jij=fixjJ = \left[ \frac{\partial f_i}{\partial x_j} \right], \quad J_{ij} = \frac{\partial f_i}{\partial x_j}

Each row corresponds to the gradient of one output function with respect to all input variables. The JACOBIAN_MATRIX describes how small changes in the input variables affect the outputs. It is widely used in:

  • Sensitivity analysis
  • Nonlinear optimization
  • Solving systems of nonlinear equations
  • Machine learning (e.g., backpropagation)
  • Robotics and control systems

See the CasADi documentation for more details on the JACOBIAN_MATRIX and its applications.

This example function is provided as-is without any representation of accuracy.

Usage

To use the function in Excel:

=JACOBIAN_MATRIX(expressions, var_values)
  • expressions (string or 2D list of strings, required): A single mathematical expression as a string, or a 2D list of expressions (vector-valued function). Example: "x**2 * y" or { "x**2 * y"; "sin(x) + y**3" }
  • var_values (list[list], required): A 2D list where the first row contains variable names and the second row contains their values. Example: { "x","y"; 2.0,3.0 }

The function returns a 2D list of floats representing the JACOBIAN_MATRIX matrix, or a string error message if calculation fails.

Examples

Example 1: Single Expression

=JACOBIAN_MATRIX("x**2 * y", {"x","y";2.0,3.0})

Expected output:

∂f/∂x∂f/∂y
12.04.0

Example 2: Vector-Valued Function

=JACOBIAN_MATRIX({"x**2 * y"; "sin(x) + y**3"}, {"x","y";2.0,3.0})

Expected output:

∂/∂x∂/∂y
f₁12.04.0
f₂-0.416127.0

Example 3: Single Expression with Functions

=JACOBIAN_MATRIX("sin(x) + y**3", {"x","y";1.0,2.0})

Expected output:

∂f/∂x∂f/∂y
0.540312.0

CasADi Function Names

When writing expressions, you may use standard function names (e.g., sin(x), exp(x)) or CasADi equivalents (e.g., ca.sin(x), ca.exp(x)). Standard names will be automatically converted to CasADi equivalents. See table below:

Standard NameCasADi Equivalent
sin(x)ca.sin(x)
cos(x)ca.cos(x)
tan(x)ca.tan(x)
exp(x)ca.exp(x)
log(x)ca.log(x)
sqrt(x)ca.sqrt(x)
atan(x)ca.atan(x)
asin(x)ca.asin(x)
acos(x)ca.acos(x)
sinh(x)ca.sinh(x)
cosh(x)ca.cosh(x)
tanh(x)ca.tanh(x)
fabs(x)ca.fabs(x)
floor(x)ca.floor(x)
ceil(x)ca.ceil(x)

If you use a standard function name, it will be automatically converted to the CasADi equivalent.

Python Code

import casadi as ca import re def jacobian_matrix(expressions, var_values): """ Calculate the JACOBIAN_MATRIX matrix of a mathematical expression with respect to specified variables. Args: expressions (str or list[list[str]]): The mathematical expression(s). Can be a single string for a single function, or a 2D list of strings for a vector-valued function. var_values (list[list]): A 2D list, where the first inner list contains the names of the variables (e.g., ['x', 'y']) and the second inner list contains their corresponding values at which to evaluate the JACOBIAN_MATRIX (e.g., [1.0, 2.0]). Returns: list[list[float]]: On success, a 2D list of floats representing the JACOBIAN_MATRIX matrix. str: Error message if calculation fails. This example function is provided as-is without any representation of accuracy. """ casadi_func_map = { r'(?<![\w.])sin\s*\(': 'ca.sin(', r'(?<![\w.])cos\s*\(': 'ca.cos(', r'(?<![\w.])tan\s*\(': 'ca.tan(', r'(?<![\w.])exp\s*\(': 'ca.exp(', r'(?<![\w.])log\s*\(': 'ca.log(', r'(?<![\w.])sqrt\s*\(': 'ca.sqrt(', r'(?<![\w.])atan\s*\(': 'ca.atan(', r'(?<![\w.])asin\s*\(': 'ca.asin(', r'(?<![\w.])acos\s*\(': 'ca.acos(', r'(?<![\w.])sinh\s*\(': 'ca.sinh(', r'(?<![\w.])cosh\s*\(': 'ca.cosh(', r'(?<![\w.])tanh\s*\(': 'ca.tanh(', r'(?<![\w.])fabs\s*\(': 'ca.fabs(', r'(?<![\w.])floor\s*\(': 'ca.floor(', r'(?<![\w.])ceil\s*\(': 'ca.ceil(', } if isinstance(expressions, str): expr_list = [expressions] elif isinstance(expressions, list): expr_list = [item for sublist in expressions for item in sublist if isinstance(item, str)] else: return "expressions must be a string or a 2D list of strings." if not expr_list: return "Expression list is empty or contains non-string elements." if not isinstance(var_values, list) or len(var_values) != 2: return "var_values should be a list of two lists: [[variable_names], [values]]" var_names = var_values[0] var_point_values = var_values[1] if not all(isinstance(name, str) for name in var_names): return "Variable names must be strings." if not all(isinstance(val, (int, float)) for val in var_point_values): return "Variable values must be numbers." if len(var_names) != len(var_point_values): return "Mismatch in the number of variable names and their values." sym_vars = [ca.MX.sym(name) for name in var_names] eval_context = {'ca': ca} for i, name in enumerate(var_names): eval_context[name] = sym_vars[i] casadi_exprs = [] for expr_str in expr_list: for pat, repl in casadi_func_map.items(): expr_str = re.sub(pat, repl, expr_str) try: casadi_expr = eval(expr_str, eval_context) casadi_exprs.append(casadi_expr) except NameError as e: undefined_var = str(e).split("'")[1] if undefined_var not in var_names: return f"Mismatch between variables in expression and provided values. Undefined variable: {undefined_var}" return f"Invalid expression string. Error: {e}" except Exception as e: return f"Invalid expression string. Error: {e}" if not casadi_exprs: return "Failed to parse any expressions." casadi_expr_vector = ca.vertcat(*casadi_exprs) J = ca.jacobian(casadi_expr_vector, ca.vertcat(*sym_vars)) jacobian_matrix_func = ca.Function('jacobian_matrix_func', sym_vars, [J]) result_matrix = jacobian_matrix_func(*var_point_values) if isinstance(result_matrix, ca.DM): return result_matrix.full().tolist() else: return "Error during CasADi calculation: Unexpected result type."

Live Notebook

Edit this function in a live notebook.

Live Demo

Last updated on