##// END OF EJS Templates
Shaperilio/qtgui fixes (#13957)...
Shaperilio/qtgui fixes (#13957) I started using the released version of my `PySide6`-enabling changes and noted some problems. In this PR, I fix those, and also overall improve the feedback to the user when a GUI event loop is hooked in: - Report which event loop is running when using `%gui <some GUI>`; e.g. `%gui qt` will show `Installed qt6 event loop hook.` - Report when the event loop is disabled; i.e. `%gui` will show `GUI event loop hook disabled.` if an event loop hook was installed, or `No event loop hook running.` if nothing was installed. - Requesting a second event loop will give the message `Shell is already running a gui event loop for <some GUI>. Call with no arguments to disable current loop.` - Requesting a different version of Qt, i.e. `%gui qt6` followed by `%gui` followed by `%gui qt5` will show `Cannot switch Qt versions for this session; will use qt6.` followed by `Installed qt6 event loop hook.` (Fixes / improves #13864)

File last commit:

r20547:8f4e2b41
r28163:88d1fedc merge
Show More
Trapezoid Rule.ipynb
971 lines | 73.7 KiB | text/plain | TextLexer

Basic Numerical Integration: the Trapezoid RuleĀ¶

A simple illustration of the trapezoid rule for definite integration:

$$ \int_{a}^{b} f(x)\, dx \approx \frac{1}{2} \sum_{k=1}^{N} \left( x_{k} - x_{k-1} \right) \left( f(x_{k}) + f(x_{k-1}) \right). $$
First, we define a simple function and sample it between 0 and 10 at 200 points
InĀ [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
InĀ [2]:
def f(x):
    return (x-3)*(x-5)*(x-7)+85

x = np.linspace(0, 10, 200)
y = f(x)

Choose a region to integrate over and take only a few points in that region

InĀ [3]:
a, b = 1, 8 # the left and right boundaries
N = 5 # the number of points
xint = np.linspace(a, b, N)
yint = f(xint)

Plot both the function and the area below it in the trapezoid approximation

InĀ [4]:
plt.plot(x, y, lw=2)
plt.axis([0, 9, 0, 140])
plt.fill_between(xint, 0, yint, facecolor='gray', alpha=0.4)
plt.text(0.5 * (a + b), 30,r"$\int_a^b f(x)dx$", horizontalalignment='center', fontsize=20);
No description has been provided for this image

Compute the integral both at high accuracy and with the trapezoid approximation

InĀ [5]:
from __future__ import print_function
from scipy.integrate import quad
integral, error = quad(f, a, b)
integral_trapezoid = sum( (xint[1:] - xint[:-1]) * (yint[1:] + yint[:-1]) ) / 2
print("The integral is:", integral, "+/-", error)
print("The trapezoid approximation with", len(xint), "points is:", integral_trapezoid)
The integral is: 565.2499999999999 +/- 6.275535646693696e-12
The trapezoid approximation with 5 points is: 559.890625