Taylor Expansion for Multivariate Functions
Deslize para mostrar o 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}")
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo