# Exercices


# Exercise 1: Gradient of a Simple Function

# Task

  1. Define the function
f(x) = 3x^3 - 5x^2 + 2x
  1. Compute the gradient of f(x) at ( x = 4 ).

The gradient (derivative) of f(x) is given by:

\frac{d}{dx} \left( 3x^3 - 5x^2 + 2x \right) = 9x^2 - 10x + 2

For ( x = 4 ):

9 \cdot 4^2 - 10 \cdot 4 + 2 = 576 - 40 + 2 = 538

# Code Template

import torch

# Define the function f(x) = 3x^3 - 5x^2 + 2x
def function(x):
    return 3 * x**3 - 5 * x**2 + 2 * x

# Initialize the variable
x = torch.tensor(4.0, requires_grad=True)

# Compute the function value
y = function(x)

# Compute the gradient
y.backward()

# Print the gradient
print(f'The gradient of the function at x = 4 is {x.grad.item()}')

# Exercise 2: Partial Derivatives of a Simple Multivariable Function

# Task

  1. Define the function : f(x, y) = x^2 + 3xy + y^2

  2. Compute the partial derivatives of \frac{\partial f}{\partial x} and \frac{\partial f}{\partial y} at the point (x, y) = (2, 3).


1. Compute

\frac{\partial f}{\partial x}

The partial derivative of ( f ) with respect to ( x ) is:

\frac{\partial f}{\partial x} = \frac{\partial}{\partial x} \left( x^2 + 3xy + y^2 \right)

Since

\frac{\partial}{\partial x} \left( x^2 \right) = 2x
\frac{\partial}{\partial x} \left( 3xy \right) = 3y

and

\frac{\partial}{\partial x} \left( y^2 \right) = 0

we get:

\frac{\partial f}{\partial x} = 2x + 3y

2. Compute

\frac{\partial f}{\partial y}

The partial derivative of ( f ) with respect to ( y ) is:

\frac{\partial f}{\partial y} = \frac{\partial}{\partial y} \left( x^2 + 3xy + y^2 \right)

Since

\frac{\partial}{\partial y} \left( x^2 \right) = 0
\frac{\partial}{\partial y} \left( 3xy \right) = 3x

and

\frac{\partial}{\partial y} \left( y^2 \right) = 2y

we get:

\frac{\partial f}{\partial y} = 3x + 2y

At the point

(x, y) = (2, 3)

we have:

\frac{\partial f}{\partial x} \bigg|_{(2, 3)} = 2 \cdot 2 + 3 \cdot 3 = 4 + 9 = 13
\frac{\partial f}{\partial y} \bigg|_{(2, 3)} = 3 \cdot 2 + 2 \cdot 3 = 6 + 6 = 12

# Code Template

import torch

# Define the function f(x, y) = x^2 + 3xy + y^2
def function(x, y):
    return x**2 + 3 * x * y + y**2

# Initialize the variables
x = torch.tensor(2.0, requires_grad=True)
y = torch.tensor(3.0, requires_grad=True)

# Compute the function value
z = function(x, y)

# Compute the partial derivatives
z.backward()

# Print the partial derivatives
print(f'The partial derivative with respect to x at (2, 3) is {x.grad.item()}')
print(f'The partial derivative with respect to y at (2, 3) is {y.grad.item()}')