This code seems to calculate the integral of the entire function and deems it's…
- User
This code seems to calculate the integral of the entire function and deems it's somewhere around 7000 but for the integral of interpolation and approximation of just one Y value it suddenly jumps to numbers in millions, whats the issue?
import numpy as np import matplotlib.pyplot as plt
dane = np.loadtxt('dane.txt')
X = dane[:, 0] Y = dane[:, 1] Z = dane[:, 2]
def Wykres_2D_wejsciowe(x, y, z, nazwa = 'Wykres 2d'): x_grid, y_grid = np.meshgrid(np.unique(x), np.unique(y)) z_grid = np.zeros_like(x_grid)
for i in range(len(x)): x_idx = np.where(x_grid[0] == x[i]) y_idx = np.where(y_grid[:, 0] == y[i]) z_grid[y_idx, x_idx] = z[i] plt.pcolormesh(x_grid, y_grid, z_grid, cmap='inferno', shading='gouraud') plt.colorbar() plt.xlabel('X') plt.ylabel('Y') plt.title('Mapa 2D') plt.show()def Wykres_3D(x, y, z, nazwa = 'Wykres 3D'): figura = plt.figure() podwykres = figura.add_subplot(111, projection='3d')
podwykres.plot_trisurf(x, y, z, cmap='plasma') podwykres.set_xlabel('X') podwykres.set_ylabel('Y') podwykres.set_zlabel('Z') podwykres.set_title(nazwa) podwykres.set_box_aspect([2, 1, 0.5]) plt.show()def Srednia_Z_W_Przedzialach(y, z): wszystkie_y = np.unique(y)
for wartosc in wszystkie_y: indeksy_do_z = np.where(y == wartosc) srednia_z = Srednia(z[indeksy_do_z]) print(f"Srednia dla y = {wartosc} : {srednia_z}")def Srednia(tablica): suma = sum(tablica) srednia = suma / len(tablica) return srednia
def Mediana(z): n = len(z) zety = list(z) zety.sort()
if n % 2 == 0: mediana1 = zety[n//2] mediana2 = zety[n//2-1] mediana = (mediana1 + mediana2)/2 else: mediana = z[n//2] return medianadef Mediana_Z_W_Przedzialach(y, z): wszystkie_y = np.unique(y)
for wartosc in wszystkie_y: indeksy_do_z = np.where(y == wartosc)[0] mediana_z = Mediana(z[indeksy_do_z]) print(f"Mediana dla y = {wartosc} : {mediana_z}")def Odchylenie_Standardowe_W_Przedzialach(y, z): wszystkie_y = np.unique(y)
for wartosc in wszystkie_y: indeksy_do_z = np.where(y == wartosc) wartosci_z = z[indeksy_do_z] srednia_z = Srednia(wartosci_z) odchylenie = np.sqrt(Srednia(np.square(wartosci_z - srednia_z))) print(f"Odchylenie standardowe dla y = {wartosc} : {odchylenie}")def Interpolacja(x, y, z): indeksy = np.where(y == Y) wartosci_x = x[indeksy] wartosci_z = z[indeksy] interpolowane = Interpolacja_Lagrange(wartosci_x, wartosci_z)
plt.plot(wartosci_x, interpolowane) plt.scatter(wartosci_x, interpolowane, color = 'red') plt.xlabel('x') plt.ylabel('Interpolowane wartości') plt.title('Interpolacja Lagrange\'a') plt.show()def Interpolacja_Lagrange(x, y): n = x.shape[0] a = np.zeros(n) mi = np.ones(n)
for i in range(0, n): for j in range(0, n): if i != j: mi[i] *= x[i] - x[j] if mi[i] != 0: a[i] = y[i] / mi[i] else: a[i] = y[i] return an = X.shape[0]
def suma(X, potega): suma = 0 for i in range(0, n): suma = suma + X[i]**potega return suma
def suma2(X,potega, Y, potega2): suma = 0 for i in range(0, n): suma = suma + (X[i]**potega)*(Y[i]**potega2) return suma
indeksy = np.where(Y == 0.2) wartosci_x = X[indeksy] wartosci_z = Z[indeksy] stopien_aproksymacji = 25
def Aproksymacja_f1(X, Y, stopien): M = np.zeros((stopien+1, stopien+1)) P = np.zeros((stopien+1))
for i in range(stopien+1): for j in range(stopien+1): M[i][j] = np.sum(X ** (i+j)) P[i] = np.sum(Y * X ** i) A = np.linalg.solve(M, P) return Awspolczynniki = Aproksymacja_f1(wartosci_x, wartosci_z, stopien_aproksymacji)
def f1(X, wspolczynniki): stopien = len(wspolczynniki) - 1 Y = np.zeros(len(X))
for i in range(len(X)): for j in range(stopien+1): Y[i] += wspolczynniki[j] * X[i] ** j return Ydef Aproksymacja_wykres(): X_wykres = np.linspace(np.min(wartosci_x), np.max(wartosci_x), 100) Z_przyblizone = f1(X_wykres, wspolczynniki)
plt.scatter(wartosci_x, wartosci_z, color='red', label='Dane (Y=0.2)') plt.plot(X_wykres, Z_przyblizone, color='blue', label='Aproksymacja') plt.xlabel('X') plt.ylabel('F(X, Y)') plt.title('Aproksymacja wielomianem stopnia ' + str(stopien_aproksymacji) + ' (Y=0.2)') plt.legend() plt.show()def Simpson(X, Z): n = len(X) h = X[1] - X[0] pole_segmentu = 0 for i in range(0, n-1, 2): pole_segmentu += h/3 * (Z[i] + 4*Z[i+1] + Z[i+2])
pole_całkowite = pole_segmentu return pole_całkowitedef Calka_Aproksymacji(wezel): indeksy = np.where(Y == wezel) print(Simpson(X[indeksy], Aproksymacja_f1(X[indeksy], Z[indeksy], stopien_aproksymacji)))
def Calka_Interpolacji(wezel): indeksy = np.where(Y == wezel) print(abs(Simpson(X[indeksy], Interpolacja_Lagrange(X[indeksy], Z[indeksy]))))
Wykres_2D_wejsciowe(X, Y, Z, "Wykres danych wejsciowych 2d") Wykres_3D(X, Y, Z, "Wykres danych wejsciowych 3d") Srednia_Z_W_Przedzialach(Y, Z) Mediana_Z_W_Przedzialach(Y, Z) Odchylenie_Standardowe_W_Przedzialach(Y, Z) Interpolacja(X, 0.2, Z) Aproksymacja_wykres() print(Simpson(X, Z)) Calka_Aproksymacji(0.1) Calka_Interpolacji(0.1)
- Assistant
The issue is that in the interpolation function, the indices used to select the values of X, Y, and Z are not correctly defined. The line
indeksy = np.where(Y == Y)should be changed toindeksy = np.where(Y == y), whereyis the input value for which we want to interpolate the function. The same correction should be made in theCalka_Interpolacjifunction.Without this correction, the interpolation function is using all the values of Y for the interpolation, which is not correct for a single input value. As a result, the interpolated function can have large values, leading to large integral values.