Skip to content
DeepForgeby svx
All problems
MediumCalculusca-163

Gradient in Polar Coordinates

Problem

In polar coordinates the gradient of f has components f_r along the radial direction and (1/r)*f_theta along the angular direction.

For the function given by the coefficient grid coeffs[i][j] * r^i * theta^j, compute the two partials with central differences (h = 1e-6) and return [f_r, f_theta / r] at (r, theta).

Hint

The angular component must be divided by r to get a physical length scale.

Test cases

case 1
in: [[[0,0],[0,0],[1,0]],3,1]
expected: [6,0]
case 2
in: [[[0,1]],2,3]
expected: [0,0.5]
case 3
in: [[[],[0,1]],2,3]
expected: [3,1]
case 4
in: [[[0,0],[0,0],[0,1]],1,2]
expected: [4,1]
Starter codePython
def polar_gradient(coeffs, r, theta, h=1e-6):
    # Return [f_r, f_theta / r]
    # Your code here
    pass

4 test cases · run your code against them in the editor