Advertisement
codisinmyvines

KonuhovTask2

Nov 1st, 2022
1,280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.63 KB | None | 0 0
  1. %a = const
  2. Nx = 400;
  3. X_L = 10;
  4. U_0 = @(x) x/X_L*abs(sin(2*x/3));
  5. mu = @(t) (1-exp(-t/10)+sin(2*t)^2/(1+t/100))/2;
  6. Hx = X_L/Nx;
  7. B = 1;
  8. a = 5;
  9. Ht = B*Hx/a;
  10. gm = a*Ht/Hx;
  11. x = zeros(1, Nx+1);
  12. for i=0:Nx
  13.     x(i+1)=i*Hx;
  14. end
  15. t_entered = 2;
  16. Uj = zeros(1, Nx+1);
  17. for i=0:Nx
  18.     Uj(i+1)=U_0(x(i+1));
  19. end
  20. %точное решение для а>0
  21. Uex = zeros(1, Nx+1);
  22. for i=0:Nx
  23.     if(x(i+1)>=a*t_entered)
  24.         Uex(i+1)= U_0(x(i+1)-a*t_entered);
  25.     else
  26.         Uex(i+1)= mu(t_entered-x(i+1)/a);
  27.     end
  28. end
  29. %приближенное решение для a>0
  30. t = 0;
  31. Uj1 = zeros(1, Nx+1);
  32. while(t<=t_entered)
  33.     Uj1(1)=mu(t);
  34.     for i=1:Nx
  35.         Uj1(i+1) = (1-gm)*Uj(i+1)+gm*Uj(i);
  36.     end
  37.     for i=0:Nx
  38.         Uj(i+1)=Uj1(i+1);
  39.     end
  40.     t = t + Ht;
  41. end
  42. nexttile
  43. plot(x, Uex)
  44. hold on
  45. plot(x, Uj, 'o')
  46.  
  47. %точно решение для a<0
  48. Uex_flip = zeros(1, Nx+1);
  49. for i=0:Nx
  50.     if(X_L-x(i+1)>=abs(a)*t_entered)
  51.         Uex_flip(i+1)= U_0(X_L-x(i+1)-abs(a)*t_entered);
  52.     else
  53.         Uex_flip(i+1)= mu(t_entered-(X_L-x(i+1))/abs(a));
  54.     end
  55. end
  56. %приб решение для а<0
  57. Uj_flip = flip(Uj);
  58.  
  59. nexttile
  60. plot(x, Uex_flip)
  61. hold on
  62. plot(x, Uj_flip, 'o')
  63.  
  64. % a not const
  65. Nx = 400;
  66. X_L = 10;
  67. U_0 = @(x) x/X_L*abs(sin(2*x/3));
  68. mu = @(t) (1-exp(-t/10)+sin(2*t)^2/(1+t/100))/2;
  69. Hx = X_L/Nx;
  70. B = 1;
  71. x = zeros(1, Nx+1);
  72. for i=0:Nx
  73.     x(i+1)=i*Hx;
  74. end
  75. t_entered = 2;
  76. Uj = zeros(1, Nx+1);
  77. for i=0:Nx
  78.     Uj(i+1)=U_0(x(i+1));
  79. end
  80. Uj1 = zeros(1,Nx+1);
  81. t = 0;
  82. while(t<=t_entered)
  83.     amax = sin(t/4.5-x(1)/2);
  84.     for i=2:Nx+1
  85.         a = sin(t/4.5-x(i)/2);
  86.         if(abs(a)>amax)
  87.             amax = abs(a);
  88.         end
  89.     end
  90.     if(amax~=0)
  91.         Ht=Hx/amax;
  92.     else
  93.         Ht=Hx;
  94.     end
  95.     Uj1(1)=mu(t);
  96.     for i=0:Nx
  97.         a(i+1) = sin(t/4.5-x(i+1)/2);
  98.         gm = a(i+1)*Ht/Hx;
  99.         nn=1;
  100.         if(i==0)
  101.             nn=0;
  102.         end
  103.         if(i==Nx)
  104.             nn=2;
  105.         end
  106.         switch(nn)
  107.             case 0
  108.                 if(gm<0)
  109.                     Uj1(i+1)=(1+gm)*Uj(i+1)-gm*Uj(i+2);
  110.                 end
  111.             case 1
  112.                 if(gm>=0)
  113.                     Uj1(i+1)=(1-gm)*Uj(i+1)+gm*Uj(i);
  114.                 else
  115.                     Uj1(i+1)=(1+gm)*Uj(i+1)-gm*Uj(i+2);
  116.                 end
  117.             case 2
  118.                 if(gm>0)
  119.                     Uj1(i+1)=(1-gm)*Uj(i+1)+gm*Uj(i);
  120.                 else
  121.                     Uj1(i+1)=Uj(i+1);
  122.                 end
  123.         end
  124.     end
  125.     for l=0:Nx
  126.             Uj(l+1)=Uj1(l+1);
  127.     end
  128.     t = t+Ht;
  129. end
  130. nexttile
  131. plot(x, Uj)
  132.  
  133. nexttile
  134. plot(x, a)
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement