Derivative of a function defined on a circle: numerical evaluation

97 Views Asked by At

Let's consider a function $f(x,y)$ defined on a circle of radius one in a two dimensional space. I want to obtain the gradient of the function tangent to the curve.

I know that this could be simply writing $f(x,y)$ in polar coordinates $f(\theta)$ and take the gradient along the theta axis $\frac{1}{r} \frac{\partial f}{\partial \theta}$, which is tangent to the unit circle.

However, I wanted to perform this derivative numerically but i am finding some trouble:

what i did is:

discretize the unit circle in points $(x_i,y_i)$ with $i$ going from 1 to $nelem$. The coordinates are obtained as $x_i= \cos(\theta_i)$ and $y_i=\sin(\theta_i)$ with $\theta_i$ going from 0 to $2 \pi$ as i goes from 1 to $nelem$.

I defined the x component of the gradient at the point i as $(f(x_{i+1},y_{i+1})-f(x_i,y_i))/(x_{i+1}-x_i)$ and the y componentof the gradient as $(f(x_{i+1},y_{i+1})-f(x_i,y_i))/(y_{i+1}-y_i)$.

Then I calculate the x component of the unit tangent vector at point i as $(x_{i+1}-x_i)/((x_{i+1}-x_i)^2+(y_{i+1}-y_i)^2)^{1/2}$ and the y component of the unit tangent vector as $(y_{i+1}-y_i)/((x_{i+1}-x_i)^2+(y_{i+1}-y_i)^2)^{1/2}$.

To obtain the tangential component of the gradient, I perform the scalar product between the gradient and the unit tangent vector at each point.

However, by checking my calculations for a simple function like $f(\theta)=\cos{\theta}$ should give on the unit circle $\frac{1}{r} \frac{\partial f}{\partial \theta}=-\sin{\theta}$. Instead, my numerical approximation yields $\approx -2 \sin{\theta}$. There is a factor 2 that i cannot explain. I also tried more complicated functions and i always get this factor 2 more than i should. It doesn't matter how many elements i am using, so i am quite sure it is not a numerical issue but some deeper mistake.

Do you have any suggestion about what i might be doing wrong?

1

There are 1 best solutions below

0
On BEST ANSWER

Your formula for gradient components is wrong. Taking one “measurement” towards some direction like $f(x_1,y_1)-f(x_0,y_0)$ is not enough to get the gradient that has two components. (For example, if functions stays the same in one direction $f(x_1,y_1)=f(x_0,y_0)$, your formula assumes that gradient is zero) If you want to get the gradient, you need to take “measurement” in two non-aligning directions.

For example, a classic formula for gradient is: $$ f'_x = \frac{f(x_1,y_0)-f(x_0,y_0)}{x_1-x_0},\qquad f'_y = \frac{f(x_0,y_1)-f(x_0,y_0)}{y_1-y_0} $$

Here we first “measure” the slope of the function in the direction of $x$ ($x_0,y_0\to x_1,y_0$), and then measure the slope in the direction of $y$ ($x_0,y_0\to x_0,y_1$).

Having said that, you don't need a full gradient to calculate a directional derivative. What you are interested in is how quick the function grows relative to the distance covered. Thus, you can use a formula:

$$ f'_{\text{dir}} = \frac{f(x_1,y_1)-f(x_0,y_0)}{\sqrt{(x_1-x_0)^2+(y_1-y_0)^2}}. $$

Let's check it for $f(\theta)=\cos\theta$: $$ f'_{\text{dir}} = \frac{\cos(\theta+\Delta\theta)-\cos\theta}{\sqrt{(\cos(\theta+\Delta\theta)-\cos\theta)^2+(\sin(\theta+\Delta\theta)-\sin\theta)^2}} =\\ \frac{-2\sin(\theta+\frac{\Delta\theta}2)\sin\frac{\Delta\theta}2}{2|\sin\frac{\Delta\theta}2|\sqrt{\sin(\theta+\frac{\Delta\theta}2)^2+\cos(\theta+\frac{\Delta\theta}2)^2}} = -\sin(\theta+\frac{\Delta\theta}2) \approx -\sin(\theta) $$