Taylor Expansion for Multivariate Functions
Swipe to show menu
The Taylor expansion is a powerful tool that allows you to approximate a function near a specific point using its derivatives. For functions of a single variable, you have already seen how the Taylor series provides a polynomial that closely matches the function near a point. When you move to functions of several variables, the idea is similar, but the formula involves gradients and higher-order derivatives like the Hessian matrix.
For a function f(x,y) that is sufficiently differentiable, the second-order Taylor expansion about the point (a,b) is:
f(x,y)βBaseΒ valuef(a,b)βββ+LinearΒ approximationΒ (1stΒ order)fxβ(a,b)(xβa)+fyβ(a,b)(yβb)ββ+QuadraticΒ correctionΒ (2ndΒ order)21β[fxxβ(a,b)(xβa)2+2fxyβ(a,b)(xβa)(yβb)+fyyβ(a,b)(yβb)2]βββHere's how you build this expansion step by step:
- Evaluate the function at the expansion point: f(a,b);
- Add the first-order terms, which use the gradient (the vector of partial derivatives): fxβ(a,b)β(xβa) and fyβ(a,b)β(yβb);
- Add the second-order terms, which use the second partial derivatives (entries of the Hessian matrix): fxxβ(a,b)β(xβa)2, fyyβ(a,b)β(yβb)2, and the mixed partial fxyβ(a,b)β(xβa)β(yβb);
- Each second-order term is multiplied by 0.5 to account for the Taylor expansion formula.
This approximation gives you a quadratic surface that matches the function's value, slope, and curvature at the point (a,b). The more derivatives you include, the more accurate your approximation becomes near the expansion point.
1234567891011121314151617181920212223242526272829import sympy as sp import numpy as np # 1. Define symbols and the function x, y = sp.symbols('x y') f = x**2 * y + y**3 # 2. SymPy automatically computes the Gradient and the Hessian matrix grad_f = sp.Matrix([sp.diff(f, x), sp.diff(f, y)]) hessian_f = sp.hessian(f, (x, y)) # 3. Set the coordinates a, b = 1.0, 2.0 x_val, y_val = 1.1, 2.05 # 4. Substitute the base point (a, b) and convert to NumPy arrays subs_dict = {x: a, y: b} f_ab = float(f.subs(subs_dict)) grad_ab = np.array(grad_f.subs(subs_dict), dtype=float).flatten() # Vector (2,) hessian_ab = np.array(hessian_f.subs(subs_dict), dtype=float) # Matrix (2, 2) # 5. Create the difference vector (Delta) delta = np.array([x_val - a, y_val - b]) # 6. Vector-matrix Taylor formula (using @ for matrix multiplication) taylor_approx = f_ab + grad_ab @ delta + 0.5 * (delta @ hessian_ab @ delta) print(f"Second-order Taylor approximation at ({x_val}, {y_val}): {taylor_approx:.4f}")
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat