|
| 1 | +# use pytorch : x^3 integral => pytorch like mma! |
| 2 | + |
| 3 | +import torch |
| 4 | + |
| 5 | +# Define the function f(x) = x^3 |
| 6 | +def f(x): |
| 7 | + return x**3 |
| 8 | + |
| 9 | +# Define the interval [a, b] |
| 10 | +a = 0.0 |
| 11 | +b = 2.0 |
| 12 | + |
| 13 | +# Number of points to use in the approximation |
| 14 | +n_points = 1000 |
| 15 | + |
| 16 | +# Create a tensor of x values evenly spaced between a and b |
| 17 | +x = torch.linspace(a, b, n_points) |
| 18 | +#tensor([0.0000, 0.0020, 0.0040, 0.0060, 0.0080, 0.0100, 0.0120, 0.0140, 0.0160, |
| 19 | +# 0.0180, 0.0200, 0.0220, 0.0240, 0.0260, 0.0280, 0.0300, 0.0320, 0.0340, |
| 20 | +# ...) |
| 21 | + |
| 22 | +# Compute the corresponding y values |
| 23 | +y = f(x) |
| 24 | +#tensor([0.0000e+00, 8.0240e-09, 6.4192e-08, 2.1665e-07, 5.1354e-07, 1.0030e-06, |
| 25 | +# 1.7332e-06, 2.7522e-06, 4.1083e-06, 5.8495e-06, 8.0240e-06, 1.0680e-05, |
| 26 | +# 1.3866e-05, 1.7629e-05, 2.2018e-05, 2.7081e-05, 3.2866e-05, 3.9422e-05, |
| 27 | +# 4.6796e-05, 5.5037e-05, 6.4192e-05, 7.4311e-05, 8.5440e-05, 9.7629e-05, |
| 28 | +#....) |
| 29 | + |
| 30 | +# Approximate the integral using the trapezoidal rule |
| 31 | +integral = torch.trapz(y, x) |
| 32 | + |
| 33 | +# Print the result |
| 34 | +print(f"The approximate integral of x^3 from {a} to {b} is: {integral.item()}") |
| 35 | +# => The approximate integral of x^3 from 0.0 to 2.0 is: 4.000003814697266 |
| 36 | + |
| 37 | + |
0 commit comments