I have made a code for the following exercise:
Use Romberg integration to calculate the following approximations $\displaystyle\int_{1}^{48}\sqrt[ ]{1+(cosx)^2}dx$
1 [Note: The results of this exercise are interesting if you know arithmeticbetween seven and nine digits].
(a) Determine Ri,1 for$ i = 1,2,3,4 and 5$. Use these approximations to predict the value of thecomprehensive.
(b) Determine Ri,i for i = $1,2,3,4 and 5$.Modify your prediction.
(c) Determine Ri,i for $i = 7,8,9 and 10$. Make a final prediction.
the code is the following:import numpy as np
Función a integrar
def f(x):return np.sqrt(1 + np.cos(x)**2)
Regla del trapecio compuesta
def trapezoid_rule(f, a, b, n):h = (b - a) / nx = np.linspace(a, b, n+1)y = f(x)return h/2 * (y[0] + 2*np.sum(y[1:n]) + y[n])
Integración de Romberg
def romberg_integration(f, a, b, n_max):R = np.zeros((n_max+1,n_max+1))
for i in range(1, n_max+1): R[i][1] = trapezoid_rule(f, a, b, 2**(i-1)) for j in range(2,i+1): R[i][j] = (4**(j-1) * R[i][j-1] - R[i-1][j-1]) / (4**(j-1) - 1)return R
Calcular aproximaciones Ri,1 para i=1 a 5
R_approximations = romberg_integration(f, 1, 48, 5)
print("Aproximaciones Ri,1:")for i in range(1,6):print("R{},1: {:.9f}".format(i,np.around(R_approximations[i][1], 9)))
Predicción del valor de la integral con la última aproximación Ri,i
integral_prediction = R_approximations[5][5]print("\nPredicción del valor de la integral con la última aproximación Ri,i: {:.9f}".format(np.around(integral_prediction, 9)))
Calcular aproximaciones Ri,i para i=1 a 5
R_diagonals = [R_approximations[i][i] for i in range(6)]
print("\nAproximaciones Ri,i:")for i in range(6):print("R{},{}: {:.9f}".format(i,i,np.around(R_diagonals[i], 9)))
Calcular aproximaciones Ri,i para i=7 a 10
R_diagonals_extended = [trapezoid_rule(f, 1, 48 ,2**i) for i in range(7,11)]
print("\nAproximaciones extendidas Ri,i:")for i in range(7,11):print("R{},{}: {:.9f}".format(i,i,np.around(R_diagonals_extended[i-7], 9)))
Predicción final con la última aproximación extendida Ri,i
integral_prediction_final = R_diagonals_extended[-1]print("\nPredicción final del valor de la integral con la última aproximación extendida Ri,i: {:.9f}".format(np.around(integral_prediction_final, 9))) ,
Can you tell me if these results are correct? The code returns these results:Ri,1 approximations:$R1,1: 54.613366966//R2.1: 57.495690027//R3.1: 57.062006029//R4.1: 57.058461760//R5.1: 57.056432956//$Prediction of the value of the integral with the last approximation Ri,i: [tex]57.055422056[/tex]
Ri,i approximations:$R0,0: 0.000000000//R1,1: 54.613366966//R2,2: 58.456464381//R3,3: 56.814843384//R4.4: 57.070598893//R5.5: 57.055422056$
Extended approximations Ri,i:$R7,7: 57.158871884//R8.8: 57.158987051//R9.9: 57.159016703//R10,10: 57.159024165$
Final prediction of the value of the integral with the last extended approximation Ri,i: $57.159024165$