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

bookImplementing Partial Derivatives in Python

In this video, you will learn how to compute partial derivatives of multivariable functions using Python. They are essential in optimization, machine learning, and data science for analyzing how a function changes with respect to one variable while keeping 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^3y + 5y^2.

2. Computing Partial Derivatives

df_dx = sp.diff(f, x)  
df_dy = sp.diff(f, y)  
  • sp.diff(f, x) computes βˆ‚fβˆ‚x\frac{\raisebox{1pt}{$\partial f$}}{\raisebox{-1pt}{$\partial x$}} while treating yy as a constant;
  • sp.diff(f, y) computes βˆ‚fβˆ‚y\frac{\raisebox{1pt}{$\partial f$}}{\raisebox{-1pt}{$\partial 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

We print the original function, its partial derivatives, and their evaluations at (1,2)(1,2).

12345678910111213141516
import sympy as sp 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
question mark

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

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 8

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain what a partial derivative is in simple terms?

What is the output of the code when evaluated at (x=1, y=2)?

How can I practice more problems like this?

Awesome!

Completion rate improved to 1.96

bookImplementing Partial Derivatives in Python

Swipe to show menu

In this video, you will learn how to compute partial derivatives of multivariable functions using Python. They are essential in optimization, machine learning, and data science for analyzing how a function changes with respect to one variable while keeping 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^3y + 5y^2.

2. Computing Partial Derivatives

df_dx = sp.diff(f, x)  
df_dy = sp.diff(f, y)  
  • sp.diff(f, x) computes βˆ‚fβˆ‚x\frac{\raisebox{1pt}{$\partial f$}}{\raisebox{-1pt}{$\partial x$}} while treating yy as a constant;
  • sp.diff(f, y) computes βˆ‚fβˆ‚y\frac{\raisebox{1pt}{$\partial f$}}{\raisebox{-1pt}{$\partial 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

We print the original function, its partial derivatives, and their evaluations at (1,2)(1,2).

12345678910111213141516
import sympy as sp 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
question mark

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

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 8
some-alt