<jupyter_start><jupyter_text>Lecture 3 - Integration - ExamplesWe can now write a little python program that evaluates the sum for the integral. We do this for the function $f(x)=x^4-2x+1$ from $a=0$ to $b=2$. We know the correct answer for this integral and compare the result of our program to it $$\int_0^2 (x^4-2x+1)dx = \left[\frac{1}{5}x^5-x^2+x\right]_0^2=4.4 .$$<jupyter_code>def f(x):
    return x**4 - 2*x + 1

N = 10
a = 0.0
b = 2.0
h = (b-a)/N

s = 0.0
for k in range(1,N):
    s += f(a+(k-0.5)*h)

print(h*s)<jupyter_output>2.3003400000000007<jupyter_text>With only 10 points, the value of the integral is not very good, because the constant value of the function that we assume in each interval does not reflect the curvature of the function  𝑓(𝑥)  very well.  Exercise 1 - naive integration1. Plot the function $ f(x)=x^4-2x+1 $.2. Change the example program to add a loop over the number of discretisation points. 3. Plot the value of the integral and/or the integration error as a function of integration points. Talking points1. What do you observe?2. How many points do you need for 1% accuracy and how many for 0.1% accuracy? 3. How can we do better?  Exercise 2 - trapezoidal ruleThe trapezoidal rule is $I(a,b) \approx h \left[ \frac{1}{2}f(a)+\frac{1}{2}f(b)+\sum_{k=1}^{N-1} f(a+kh) \right]$.1. Change your integration program to the trapezoidal rule. Loop over the number of discretisation points. 2. Plot the value of the integral and/or the integration error as a function of integration points. Talking points1. What changes with the trapezoidal rule?2. How many points do you need for 1% accuracy and how many for 0.1% accuracy? 3. How could we do even better?  Exercise 3 - Simpon's ruleSimpson's rule leads to the following integral expression: $I(a,b)\approx \frac{h}{3} \left[f(a)+f(b)+4\sum_{k=1}^{N/2} f(a+(2k-1)h)+2\sum_{k=1}^{N/2-1}f(a+2kh) \right]$1. Change your integration program to the Simpson's rule. Loop over the number of discretisation points. 2. Plot the value of the integral and/or the integration error as a function of integration points. Talking points1. What changes with the Simpson's rule?2. How many points do you need for 1% accuracy and how many for 0.1% accuracy? 3. How could we do even better?  Exercise 4The python file gaussxw.py is included in the lecture folder. It contains the routine gaussxw that generates the integration grid points and corresponding weights. It use is shown below.1. Change your integration program to use Gauss-Legendre integration. Use $N=3$ integration points.2. Test what happens when you increase the number of integration points. Talking points1. What changes with Gauss-Legendre integration?2. Do you still need to verify convergence?How to use the gaussxw routine:<jupyter_code># import the routine (make sure that gaussxw.py is in the same directory as your notebook)
from gaussxw import gaussxw

# Calculate the sample points (x) and weights (w) in the interval [-1,1]. x and w are arrays with N entries.
x,w = gaussxw(N)

# Note, if you are not integrating from -1 to 1, you need to map x and w to the required integration domain.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0939873417721519, 'fraction_numerical': 0.03481012658227848, 'mean_word_length': 4.303691275167785, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '584234', 'license': 'MIT', 'n_tokens_mistral': 1050, 'n_tokens_neox': 983, 'n_words': 464, 'path': 'Lecture 2 - Integrals and Derivatives/CompPhys_L2_1_in_class_exercises.ipynb', 'repo_name': 'hlappal/comp-phys', 'size': '3160'}