def integrand(x, params): value = 0 for coefficient, power in params: value += coefficient *x** power return value def trapezoidal_rule(f, a, b, n): x = np.linspace (a, b, n+1) # a- Lower Limit, b-upper limit, n= number of trapezoid fx = f(x) weights = np.ones(n+1) weights[0] /= 2 weights [-1] /= 2 h (b a) n = integral h✶ np.sum(weights return integral * fx) def plot_errors (x_values, y_values, labels, linestyles, title): plt.figure(figsize=(10, 6)) for x, y, label, linestyle in zip(x_values, y_values, labels, linestyles): plt.loglog(x, y, linestyle, label-label) plt.xlabel('Number of Trapezoids (log scale)') plt.ylabel('Error (log scale)') plt.title(title) plt.legend() plt.show() def calculate_slope (x_values, y_values): coefficients = np. polyfit (np.log(x_values), np.log(y_values), 1) return coefficients[0] 4. Use Richardson extrapolation for the trapezoid rule to compute the integral for 15 ✓e dx. Compute the 1st, 2nd, 3rd, 4th, and 5th best approximation. This is analagous to finding 121, 122, 123, 124, and 125. Once again, make use of the functions that you defined in 1. There is no graph for this exercise--just output the 5 numbers.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

PLEASE DO Q4 IN PYTHON

def integrand(x, params):
value = 0
for coefficient, power in params:
value += coefficient *x** power
return value
def trapezoidal_rule(f, a, b, n):
x = np.linspace (a, b, n+1) # a- Lower Limit, b-upper limit, n= number of trapezoid
fx = f(x)
weights
=
np.ones(n+1)
weights[0] /= 2
weights [-1] /= 2
h (b a) n
=
integral h✶ np.sum(weights
return integral
*
fx)
def plot_errors (x_values, y_values, labels, linestyles, title):
plt.figure(figsize=(10, 6))
for x, y, label, linestyle in zip(x_values, y_values, labels, linestyles):
plt.loglog(x, y, linestyle, label-label)
plt.xlabel('Number of Trapezoids (log scale)')
plt.ylabel('Error (log scale)')
plt.title(title)
plt.legend()
plt.show()
def calculate_slope (x_values, y_values):
coefficients = np. polyfit (np.log(x_values), np.log(y_values), 1)
return coefficients[0]
Transcribed Image Text:def integrand(x, params): value = 0 for coefficient, power in params: value += coefficient *x** power return value def trapezoidal_rule(f, a, b, n): x = np.linspace (a, b, n+1) # a- Lower Limit, b-upper limit, n= number of trapezoid fx = f(x) weights = np.ones(n+1) weights[0] /= 2 weights [-1] /= 2 h (b a) n = integral h✶ np.sum(weights return integral * fx) def plot_errors (x_values, y_values, labels, linestyles, title): plt.figure(figsize=(10, 6)) for x, y, label, linestyle in zip(x_values, y_values, labels, linestyles): plt.loglog(x, y, linestyle, label-label) plt.xlabel('Number of Trapezoids (log scale)') plt.ylabel('Error (log scale)') plt.title(title) plt.legend() plt.show() def calculate_slope (x_values, y_values): coefficients = np. polyfit (np.log(x_values), np.log(y_values), 1) return coefficients[0]
4. Use Richardson extrapolation for the trapezoid rule to compute the integral for 15 ✓e
dx. Compute the 1st, 2nd, 3rd, 4th, and 5th best
approximation. This is analagous to finding 121, 122, 123, 124, and 125. Once again, make use of the functions that you defined in 1.
There is no graph for this exercise--just output the 5 numbers.
Transcribed Image Text:4. Use Richardson extrapolation for the trapezoid rule to compute the integral for 15 ✓e dx. Compute the 1st, 2nd, 3rd, 4th, and 5th best approximation. This is analagous to finding 121, 122, 123, 124, and 125. Once again, make use of the functions that you defined in 1. There is no graph for this exercise--just output the 5 numbers.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning