Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.23 KB | None | 0 0
  1. public abstract class scheme {
  2. protected int N, M, n;
  3. protected double h, tau, a;
  4. protected double[][] temp;
  5. protected double[] U1;
  6.  
  7. protected scheme(double h, double tau) {
  8. this.h = h;
  9. this.tau = tau;
  10. this.N = (int)(1/h);
  11. this.M = (int)(1/tau);
  12. temp = new double[N+1][M+1];
  13. }
  14. protected abstract void run();
  15. protected void paint() {
  16. if(n == 0) { n = 11;}
  17. double[] U = new double[n];
  18. double[] T = new double[n];
  19. for(int i = 0; i < n; i++) {
  20. U[i] = temp[(int)(N/2)][i*M/(n-1)];
  21. T[i] = (M/(n-1))*tau*i;
  22. System.out.print(U[i]);
  23. System.out.print(' ');
  24. System.out.println(U1[i]);
  25. }
  26. int[] X = new int[n];
  27. for(int i = 0; i < n; i++) {
  28. X[i] = (int) (440*T[i]) + 20;
  29. }
  30. double Umax = U[0];
  31. double Umin = U[0];
  32. for(int i = 0; i < n; i++) {
  33. if(Umax < U[i]) {Umax = U[i];}
  34. if(Umax < U1[i]) {Umax = U1[i];}
  35. if(Umin > U[i]) {Umin = U[i];}
  36. if(Umin > U1[i]) {Umin = U1[i];}
  37. }
  38. myFrame.n = n;
  39. myFrame.y = doubleToInt(U, n, Umin, Umax);
  40. myFrame.y1 = doubleToInt(U1, n, Umin, Umax);
  41. myFrame.x = X;
  42. new myFrame().setVisible(true);
  43. }
  44. private int[] doubleToInt(double[] U, int n, double min, double max) {
  45. int[] Y = new int[n];
  46. for(int i = 0; i < n; i++) {
  47. Y[i] = 340 - (int) (320*(U[i] - min)/(max - min));
  48. }
  49. return Y;
  50. }
  51. protected void error() {
  52. double error = 0;
  53. for(int i = 0; i < N+1; i++) {
  54. for(int j = 0; j < M+1; j++) {
  55. double er = Math.abs(temp[i][j] - solution(j*tau, i*h));
  56. if(er > error) {
  57. error = er;
  58. }
  59. }
  60. }
  61. System.out.println(error);
  62. }
  63. protected abstract double f(double t, double x);
  64. protected abstract double solution(double t, double x);
  65. }
  66.  
  67.  
  68. /////////////////////////////////////////////////////////////
  69.  
  70.  
  71. public class exTerm extends scheme{
  72.  
  73. public exTerm(double h, double tau) {
  74. super(h, tau);
  75. a = 0.037;
  76. n = 11;
  77. run();
  78. paint();
  79. error();
  80. }
  81. @Override
  82. protected void run() {
  83. for(int i = 0; i < N+1; i++) {
  84. temp[i][0] = solution(0, i*h);
  85. }
  86. for(int j = 1; j < M+1; j++) {
  87. temp[0][j] = solution(j*tau, 0);
  88. temp[N][j] = solution(j*tau, 1);
  89. for(int i = 1; i < N; i++) {
  90. temp[i][j] = temp[i][j-1] + a*(temp[i+1][j-1] - 2*temp[i][j-1] + temp[i-1][j-1])*tau/(h*h) + tau*f((j-1)*tau, i*h);
  91. }
  92. }
  93. U1 = new double[n];
  94. for(int i = 0; i < n; i++) { U1[i] = solution(i*(double)1/(n-1), 0.5);}
  95. }
  96.  
  97. @Override
  98. protected double f(double t, double x) {
  99. double result = (-9)*Math.pow(t, 2) + 6*t*x - a*(24*Math.pow(x, 2) - 2*Math.exp(x));
  100. return result;
  101. }
  102. @Override
  103. protected double solution(double t, double x) {
  104. double result = 2*Math.pow(x, 4) + (-3)*Math.pow(t, 3) + 3*Math.pow(t, 2)*x - 2*Math.exp(x);
  105. return result;
  106. }
  107. }
  108.  
  109. //////////////////////////////////////////////////////////////////
  110.  
  111.  
  112. public class imTerm extends scheme{
  113. private double A, B, C;
  114.  
  115. protected imTerm(double h, double tau) {
  116. super(h, tau);
  117. a = 0.034;
  118. n = 11;
  119. A = a*tau/(h*h);
  120. B = (-1)*(1 + 2*a*tau/(h*h));
  121. C = A;
  122. run();
  123. paint();
  124. error();
  125. }
  126. @Override
  127. protected void run() {
  128. for(int i = 0; i < N+1; i++) {
  129. temp[i][0] = solution(0, i*h);
  130. }
  131. for(int j = 1; j < M+1; j++) {
  132. temp[0][j] = solution(j*tau, 0);
  133. temp[N][j] = solution(j*tau, 1);
  134. double[] F = new double[N-1];
  135. for(int i = 0; i < N-1; i++) {
  136. F[i] = (-1)*(temp[i+1][j-1] + tau*f(j*tau, i*h));
  137. }
  138. F[0] -= solution(tau*j, 0)*A;
  139. F[N-2] -= solution(tau*j, 1)*A;
  140. calc(F, j);
  141. }
  142. U1 = new double[n];
  143. for(int i = 0; i < n; i++) { U1[i] = solution(i*(double)1/(n-1), 0.5);}
  144. }
  145. private void calc(double[] F, int j) {
  146. int n = N-1;
  147. double[] p = new double[n-1], q = new double[n-1], X = new double[n];
  148. p[0] = C/B;
  149. q[0] = F[0]/B;
  150. for(int i = 1; i < n-1; i++) {
  151. p[i] = C/(B - p[i-1]*A);
  152. q[i] = (F[i] - q[i-1]*A)/(B - p[i-1]*A);
  153. }
  154. X[n-1] = (F[n-1] - q[n-2]*A)/(B - p[n-2]*A);
  155. for(int i = n-2; i >= 0; i--) {
  156. X[i] = q[i] - p[i]*X[i+1];
  157. }
  158. for(int i = 1; i < N; i++) {
  159. temp[i][j] = X[i-1];
  160. }
  161. }
  162. @Override
  163. protected double f(double t, double x) {
  164. double result = (-9)*Math.pow(t, 2) + 6*t*x - a*(24*Math.pow(x, 2) - 2*Math.exp(x));
  165. return result;
  166. }
  167. @Override
  168. protected double solution(double t, double x) {
  169. double result = 2*Math.pow(x, 4) + (-3)*Math.pow(t, 3) + 3*Math.pow(t, 2)*x - 2*Math.exp(x);
  170. return result;
  171. }
  172.  
  173. }
  174.  
  175.  
  176. /////////////////////////////////////////////////////////////////////
  177.  
  178.  
  179. public class KNTerm extends scheme{
  180. double A, B, C;
  181.  
  182. protected KNTerm(double h, double tau) {
  183. super(h, tau);
  184. a = 0.02;
  185. n = 11;
  186. A = a*tau/(2*h*h);
  187. B = (-1)*(1 + a*tau/(h*h));
  188. C = A;
  189. run();
  190. paint();
  191. error();
  192. }
  193. @Override
  194. protected void run() {
  195. for(int i = 0; i < N+1; i++) {
  196. temp[i][0] = solution(0, i*h);
  197. }
  198. for(int j = 1; j < M+1; j++) {
  199. temp[0][j] = solution(j*tau, 0);
  200. temp[N][j] = solution(j*tau, 1);
  201. double[] F = new double[N+1];
  202. for(int i = 1; i < N-1; i++) {
  203. F[i-1] = (-1)*(temp[i-1][j-1]*(a*tau/(2*h*h)) + temp[i][j-1]*(1 - a*tau/(h*h)) + temp[i+1][j-1]*(a*tau/(2*h*h)) + tau*f(j*tau, i*h));
  204. }
  205. F[0] -= solution(tau*j, 0)*a*tau/(2*h*h); ////1.4268
  206. F[N-2] -= solution(tau*j, 1)*a*tau/(2*h*h);
  207. calc(F, j);
  208. }
  209. U1 = new double[n];
  210. for(int i = 0; i < n; i++) { U1[i] = solution(i*(double)1/(n-1), 0.5);}
  211. }
  212. private void calc(double[] F, int j) {
  213. int n = N-1;
  214. double[] p = new double[n-1], q = new double[n-1], X = new double[n];
  215. p[0] = C/B;
  216. q[0] = F[0]/B;
  217. for(int i = 1; i < n-1; i++) {
  218. p[i] = C/(B - p[i-1]*A);
  219. q[i] = (F[i] - q[i-1]*A)/(B - p[i-1]*A);
  220. }
  221. X[n-1] = (F[n-1] - q[n-2]*A)/(B - p[n-2]*A);
  222. for(int i = n-2; i >= 0; i--) {
  223. X[i] = q[i] - p[i]*X[i+1];
  224. }
  225. for(int i = 1; i < N; i++) {
  226. temp[i][j] = X[i-1];
  227. }
  228. }
  229. @Override
  230. protected double f(double t, double x) {
  231. double result = (-9)*Math.pow(t, 2) + 6*t*x - a*(24*Math.pow(x, 2) - 2*Math.exp(x));
  232. return result;
  233. }
  234. @Override
  235. protected double solution(double t, double x) {
  236. double result = 2*Math.pow(x, 4) + (-3)*Math.pow(t, 3) + 3*Math.pow(t, 2)*x - 2*Math.exp(x);
  237. return result;
  238. }
  239.  
  240. }
  241.  
  242.  
  243. ////////////////////////////////////////////////////////////////////////////////////////////
  244.  
  245.  
  246. public class exDiff extends scheme{
  247.  
  248. public exDiff(double h, double tau) {
  249. super(h, tau);
  250. a = 0.034;
  251. n = 11;
  252. run();
  253. paint();
  254. error();
  255. }
  256. @Override
  257. protected void run() {
  258. for(int i = 0; i < N+1; i++) {
  259. temp[i][0] = solution(0, i*h);
  260. }
  261. for(int j = 1; j < M+1; j++) {
  262. temp[0][j] = solution(j*tau, 0);
  263. temp[N][j] = solution(j*tau, 1);
  264. for(int i = 1; i < N; i++) {
  265. temp[i][j] = temp[i][j-1] - a*(temp[i][j-1] - temp[i-1][j-1])*tau/h + tau*f((j-1)*tau, i*h);
  266. }
  267. }
  268. U1 = new double[n];
  269. for(int i = 0; i < n; i++) { U1[i] = solution(i*(double)1/(n-1), 0.5);}
  270. }
  271. @Override
  272. protected double f(double t, double x) {
  273. double result = a*(2*Math.PI - Math.PI*Math.sin(Math.PI*x)) - Math.PI*Math.cos(2*Math.PI*t) - 3.5;
  274. return result;
  275. }
  276. @Override
  277. protected double solution(double t, double x) {
  278. double result = Math.cos(Math.PI*x) - 0.5*Math.sin(2*Math.PI*t) + 2*Math.PI*x - 3.5*t;
  279. return result;
  280. }
  281. }
  282.  
  283.  
  284. ///////////////////////////////////////////////////////////////
  285.  
  286.  
  287. public class imDiff extends scheme{
  288. private double A, B, C;
  289.  
  290. protected imDiff(double h, double tau) {
  291. super(h, tau);
  292. a = 0.001;
  293. n = 11;
  294. A = -a*tau/h;
  295. B = (1 + a*tau/h);
  296. C = 0;
  297. run();
  298. paint();
  299. error();
  300. }
  301. @Override
  302. protected void run() {
  303. for(int i = 0; i < N+1; i++) {
  304. temp[i][0] = solution(0, i*h);
  305. }
  306. for(int j = 1; j < M+1; j++) {
  307. temp[0][j] = solution(j*tau, 0);
  308. temp[N][j] = solution(j*tau, 1);
  309. double[] F = new double[N+1];
  310. for(int i = 0; i < N-1; i++) {
  311. F[i] = (temp[i][j-1] + tau*f(j*tau, i*h));
  312. }
  313.  
  314. calc(F, j);
  315. }
  316. U1 = new double[n];
  317. for(int i = 0; i < n; i++) { U1[i] = solution(i*(double)1/(n-1), 0.5);}
  318. }
  319. private void calc(double[] F, int j) {
  320. int n = N-1;
  321. double[] p = new double[n-1], q = new double[n-1], X = new double[n];
  322. p[0] = C/B;
  323. q[0] = F[0]/B;
  324. for(int i = 1; i < n-1; i++) {
  325. p[i] = C/(B - p[i-1]*A);
  326. q[i] = (F[i] - q[i-1]*A)/(B - p[i-1]*A);
  327. }
  328. X[n-1] = (F[n-1] - q[n-2]*A)/(B - p[n-2]*A);
  329. for(int i = n-2; i >= 0; i--) {
  330. X[i] = q[i] - p[i]*X[i+1];
  331. }
  332. for(int i = 1; i < N; i++) {
  333. temp[i][j] = X[N-(i+1)];
  334. }
  335. }
  336. @Override
  337. protected double f(double t, double x) {
  338. double result = a*(2*Math.PI - Math.PI*Math.sin(Math.PI*x)) - Math.PI*Math.cos(2*Math.PI*t) - 3.5;
  339. return result;
  340. }
  341.  
  342. @Override
  343. protected double solution(double t, double x) {
  344. double result = Math.cos(Math.PI*x) - 0.5*Math.sin(2*Math.PI*t) + 2*Math.PI*x - 3.5*t;
  345. return result;
  346. }
  347.  
  348. }
  349.  
  350.  
  351. /////////////////////////////////////////////////////////////////////////////////////////////
  352.  
  353.  
  354. public class lacksDiff extends scheme{
  355.  
  356. public lacksDiff(double h, double tau) {
  357. super(h, tau);
  358. a = 0.4;
  359. n = 11;
  360. run();
  361. paint();
  362. error();
  363. }
  364. @Override
  365. protected void run() {
  366. for(int i = 0; i < N+1; i++) {
  367. temp[i][0] = solution(0, i*h);
  368. }
  369. for(int j = 1; j < M+1; j++) {
  370. temp[0][j] = solution(j*tau, 0);
  371. temp[N][j] = solution(j*tau, 1);
  372. for(int i = 1; i < N; i++) {
  373. temp[i][j] = 0.5*(temp[i+1][j-1] + temp[i-1][j-1]) + (temp[i-1][j-1] - temp[i+1][j-1])*(a*tau/(2*h));
  374. }
  375. }
  376. U1 = new double[n];
  377. for(int i = 0; i < n; i++) { U1[i] = solution(i*(double)1/(n-1), 0.5);}
  378. }
  379. @Override
  380. protected double f(double t, double x) {
  381. double result = 0;
  382. return result;
  383. }
  384. @Override
  385. protected double solution(double t, double x) {
  386. double result = Math.cos(2*Math.PI*(x - a*t)) + Math.pow((x - a*t), 3);
  387. return result;
  388. }
  389. }
  390.  
  391.  
  392. /////////////////////////////////////////////////////////////////////////////
  393.  
  394.  
  395. import java.awt.BorderLayout;
  396. import java.awt.Color;
  397.  
  398. import javax.swing.JFrame;
  399. import javax.swing.JPanel;
  400.  
  401. public class myFrame extends JFrame{
  402. public static int y[];
  403. public static int x[];
  404. public static int y1[];
  405. public static int n = 5;
  406.  
  407. public myFrame () {
  408. super("График");
  409. JPanel jcp = new JPanel(new BorderLayout());
  410. setContentPane(jcp);
  411. jcp.add(new drawingComponent (), BorderLayout.CENTER);
  412. jcp.setBackground(Color.gray);
  413. setSize(500, 400);
  414. setLocationRelativeTo(null);
  415. setDefaultCloseOperation(EXIT_ON_CLOSE);
  416. }
  417.  
  418. }
  419.  
  420.  
  421. //////////////////////////////////////////////////////////////////////////////////////
  422.  
  423.  
  424. import java.awt.Color;
  425. import java.awt.Graphics;
  426. import java.awt.Graphics2D;
  427.  
  428. import javax.swing.JPanel;
  429.  
  430. public class drawingComponent extends JPanel{
  431. int xg[] = myFrame.x;
  432. int yg[] = myFrame.y;
  433. int yg1[] = myFrame.y1;
  434. int ng = myFrame.n;
  435.  
  436. @Override
  437. protected void paintComponent(Graphics gh) {
  438. Graphics2D drp = (Graphics2D)gh;
  439. drp.drawLine(20, 340, 20, 20);
  440. drp.drawLine(20, 340, 460, 340);
  441. drp.setColor(Color.RED);
  442. drp.drawPolyline(xg, yg, ng);
  443. drp.setColor(Color.BLACK);
  444. drp.drawPolyline(xg, yg1, ng);
  445. }
  446. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement