Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import scipy.linalg as lal
  4.  
  5. N = 20
  6. a = 0
  7. b = np.pi/2
  8. h = (b - a)/N
  9. x = np.linspace(a,b,N + 1)
  10. t = np.linspace(a,b,N + 1)
  11.  
  12.  
  13. def K(x,t):
  14. K = np.sin(x + t)
  15. return K
  16.  
  17. def f(x):
  18. f = x
  19. return f
  20.  
  21. f_i = f(x)
  22.  
  23.  
  24. K_ij = np.zeros((N + 1,N + 1))
  25.  
  26. for i in range(0,N + 1):
  27. for j in range(0,N + 1):
  28. if j == 0 or j == N:
  29. K_ij[i,j] = h/2*K(x[i],t[j])
  30. else:
  31. K_ij[i,j] = h*K(x[i],t[j])
  32.  
  33.  
  34. #allg: A*x = b hier: A*y = f wobei f = x und y = x bzw t also A*y = t
  35.  
  36.  
  37. Em = np.zeros((N + 1,N + 1))
  38.  
  39. for i in range(0,N + 1):
  40. for j in range(0,N + 1):
  41. while j == i:
  42. Em[i,j] = 1
  43. j += 1
  44.  
  45.  
  46. A_ij = Em - K_ij
  47. y = lal.solve(A_ij,f_i)
  48. print(y)
  49.  
  50. plt.figure
  51. plt.plot(x,y,label = 'N = 20')
  52. plt.legend(loc = 'upper left')
  53. plt.xlabel('x')
  54. plt.ylabel('$y(x_{i})$')
  55. plt.title('Numerische Lösung')
  56. plt.grid()
  57.  
  58. def y_analytic(x):
  59. y_analytic = ((np.pi**2 - 4)*x -2*(4 - 2*np.pi + np.pi**2)*np.cos(x) - 8*(np.pi - 1)*np.sin(x))/(np.pi**2 - 4)
  60. return y_analytic
  61.  
  62. #print(y_analytic(N))
  63.  
  64.  
  65. eps = 10**-4
  66. N = 1
  67. mdiff = 10
  68. while eps < mdiff:
  69. h2 = (b - a)/N
  70. x2 = np.linspace(a,b,N + 1)
  71. t2 = np.linspace(a,b,N + 1)
  72. f_i2 = f(x2)
  73. K_ij2 = np.zeros((N + 1,N + 1))
  74. for i in range(0,N + 1):
  75. for j in range(0,N + 1):
  76. if j == 0 or j == N:
  77. K_ij2[i,j] = h2/2*K(x2[i],t2[j])
  78. else:
  79. K_ij2[i,j] = h2*K(x2[i],t2[j])
  80. Em2 = np.zeros((N + 1,N + 1))
  81. for i in range(0,N + 1):
  82. for j in range(0,N + 1):
  83. while j == i:
  84. Em2[i,j] = 1
  85. j += 1
  86. A_ij2 = Em2 - K_ij2
  87. y2 = lal.solve(A_ij2,f_i2)
  88. mdiff = max(abs(y2 - y_analytic(x2)))
  89. # print(mdiff)
  90. # print(N)
  91. N += 1
  92. print('')
  93. print('Anzahl der Intervalle um einen maximalen Fehler von',eps,'zu machen:',N)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement