Let the curve extend along the X-axis from $a$ to $b$. We divide the extent between $a$ and $b$ into small pieces each of length $dx$. Let the corresponding position of the curve along the Y-axis be $y_a$ at $x=a$ and $y_b$ at $x=b$. After dividing the X-axis into small intervals $dx$, we have $$Values(X) = a, a+dx, a+2dx, a+3dx, \dots, a+(N-1)dx$$ Let us call these values $x_i$ where $x_i = a+(i-1)dx$ with $i=1, 2, 3, \ldots, N$. Similarly, the corresponding $y$ values are $y_i, i=1, 2, \ldots, N$.
The curve length is then given by $$ l = \sum_{i=1}^{N-1}\left[(x_{i+1}-x_i)\sqrt{1 + \left( \frac{y_{i+1}-y_i}{x_{i+1}-x_i}\right)^2}\,\right] $$
Write the code below for finding curve lengths.import numpy as np
from math import sqrt
from matplotlib import pyplot as plt
np.set_printoptions(precision=2, suppress=True)
def curve_length(X, Y) :
return length
Use the data from the cell below to test your code.
X = np.arange(0, 5, 0.25)
Y = 2.1 * X ** 2 - 5.7 * X + 9.81
plt.grid(True)
plt.plot(X, Y, 'xkcd:khaki')
plt.show()
### Solve the problem in this cell
length = 0.0
print('Length of the curve is: ', length)
def area_trapezoid(X, Y) :
area = 0.0
return area
def area_simpson(X, Y) :
area = 0.0
return area
Try your code on the same example given for the length of the curve. The correct answer is $\approx 57.3$