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.
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...
Related questions
Question
PLEASE DO Q4 IN PYTHON
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning