Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Implementing Partial Derivatives in Python | Mathematical Analysis
Mathematics for Data Science

bookImplementing Partial Derivatives in Python

In this video, we'll learn how to compute partial derivatives of functions with multiple variables using Python. Partial derivatives are essential in optimization problems, machine learning, and data science to analyze how a function changes with respect to one variable while keeping the others constant.

1. Defining a Multivariable Function

x, y = sp.symbols('x y')
f = 4*x**3*y + 5*y**2
  • Here, we define xx and yy as symbolic variables;
  • We then define the function f(x,y)=4x3y+5y2f(x, y) = 4x³y + 5y².

2. Computing Partial Derivatives

df_dx = sp.diff(f, x)  
df_dy = sp.diff(f, y)  
  • sp.diff(f, x) computes fx\frac{∂f}{∂x} while treating yy as a constant;
  • sp.diff(f, y) computes fy\frac{∂f}{∂y} while treating xx as a constant.

3. Evaluating Partial Derivatives at (x=1, y=2)

df_dx_val = df_dx.subs({x: 1, y: 2})  
df_dy_val = df_dy.subs({x: 1, y: 2})
  • The .subs({x: 1, y: 2}) function substitutes x=1x=1 and $$y=2$4 into the computed derivatives;
  • This allows us to numerically evaluate the derivatives at a specific point.

4. Printing the Results

12345678910111213141516
import sympy x, y = sp.symbols('x y') f = 4*x**3*y + 5*y**2 df_dx = sp.diff(f, x) df_dy = sp.diff(f, y) df_dx_val = df_dx.subs({x: 1, y: 2}) df_dy_val = df_dy.subs({x: 1, y: 2}) print("Function: f(x, y) =", f) print("∂f/∂x =", df_dx) print("∂f/∂y =", df_dy) print("∂f/∂x at (1,2) =", df_dx_val) print("∂f/∂y at (1,2) =", df_dy_val)
copy
  • We print the original function, its partial derivativesw, and their evaluations at (1,2)(1,2).

1. What does the partial derivative of f(x,y)f(x, y) with respect to xx represent?

2. What will sp.diff(f, y) return for given function?

f(x,y)=x2y+3y2f(x, y) = x²y + 3y²

3. If we evaluate fx\frac{∂f}{∂x} at (2,3)(2,3) and the result is 24, what does this mean?

question mark

What does the partial derivative of f(x,y)f(x, y) with respect to xx represent?

Select the correct answer

question mark

What will sp.diff(f, y) return for given function?

f(x,y)=x2y+3y2f(x, y) = x²y + 3y²

Select the correct answer

question mark

If we evaluate fx\frac{∂f}{∂x} at (2,3)(2,3) and the result is 24, what does this mean?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 8

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you explain what partial derivatives are in simple terms?

How do I interpret the results of the partial derivatives at the point (1,2)?

Can you show more examples of using SymPy for partial derivatives?

Awesome!

Completion rate improved to 1.89

bookImplementing Partial Derivatives in Python

Veeg om het menu te tonen

In this video, we'll learn how to compute partial derivatives of functions with multiple variables using Python. Partial derivatives are essential in optimization problems, machine learning, and data science to analyze how a function changes with respect to one variable while keeping the others constant.

1. Defining a Multivariable Function

x, y = sp.symbols('x y')
f = 4*x**3*y + 5*y**2
  • Here, we define xx and yy as symbolic variables;
  • We then define the function f(x,y)=4x3y+5y2f(x, y) = 4x³y + 5y².

2. Computing Partial Derivatives

df_dx = sp.diff(f, x)  
df_dy = sp.diff(f, y)  
  • sp.diff(f, x) computes fx\frac{∂f}{∂x} while treating yy as a constant;
  • sp.diff(f, y) computes fy\frac{∂f}{∂y} while treating xx as a constant.

3. Evaluating Partial Derivatives at (x=1, y=2)

df_dx_val = df_dx.subs({x: 1, y: 2})  
df_dy_val = df_dy.subs({x: 1, y: 2})
  • The .subs({x: 1, y: 2}) function substitutes x=1x=1 and $$y=2$4 into the computed derivatives;
  • This allows us to numerically evaluate the derivatives at a specific point.

4. Printing the Results

12345678910111213141516
import sympy x, y = sp.symbols('x y') f = 4*x**3*y + 5*y**2 df_dx = sp.diff(f, x) df_dy = sp.diff(f, y) df_dx_val = df_dx.subs({x: 1, y: 2}) df_dy_val = df_dy.subs({x: 1, y: 2}) print("Function: f(x, y) =", f) print("∂f/∂x =", df_dx) print("∂f/∂y =", df_dy) print("∂f/∂x at (1,2) =", df_dx_val) print("∂f/∂y at (1,2) =", df_dy_val)
copy
  • We print the original function, its partial derivativesw, and their evaluations at (1,2)(1,2).

1. What does the partial derivative of f(x,y)f(x, y) with respect to xx represent?

2. What will sp.diff(f, y) return for given function?

f(x,y)=x2y+3y2f(x, y) = x²y + 3y²

3. If we evaluate fx\frac{∂f}{∂x} at (2,3)(2,3) and the result is 24, what does this mean?

question mark

What does the partial derivative of f(x,y)f(x, y) with respect to xx represent?

Select the correct answer

question mark

What will sp.diff(f, y) return for given function?

f(x,y)=x2y+3y2f(x, y) = x²y + 3y²

Select the correct answer

question mark

If we evaluate fx\frac{∂f}{∂x} at (2,3)(2,3) and the result is 24, what does this mean?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 8
some-alt