codisinmyvines

Konuhov2zadanie

Nov 13th, 2022
1,328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.72 KB | None | 0 0
  1. %aconst.m
  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. % U_0 = @(x) sqrt(x/X_L)*sqrt(abs(sin(4*x/3)));
  7. % mu = @(t) 1/3*(1-exp(-t/15))*(cos(t)+sin(2*t))^2;
  8. U_0 = @(x) (1-x/X_L)^5*abs(sin(x/2));
  9. mu = @(t) exp(-t/20)*sqrt(abs(sin(t/5)));
  10. Hx = X_L/Nx;
  11. B = 1;
  12. a = 1;
  13. Ht = B*Hx/a;
  14. gm = a*Ht/Hx;
  15. x = zeros(1, Nx+1);
  16. for i=0:Nx
  17.     x(i+1)=i*Hx;
  18. end
  19. t_entered = 5;
  20. Uj = zeros(1, Nx+1);
  21. for i=0:Nx
  22.     Uj(i+1)=U_0(x(i+1));
  23. end
  24. %точное решение для а>0
  25. Uex = zeros(1, Nx+1);
  26. for i=0:Nx
  27.     if(x(i+1)>=a*t_entered)
  28.         Uex(i+1)= U_0(x(i+1)-a*t_entered);
  29.     else
  30.         Uex(i+1)= mu(t_entered-x(i+1)/a);
  31.     end
  32. end
  33. %приближенное решение для a>0
  34. t = 0;
  35. Uj1 = zeros(1, Nx+1);
  36. while(t<=t_entered)
  37.     Uj1(1)=mu(t);
  38.     for i=1:Nx
  39.         Uj1(i+1) = (1-gm)*Uj(i+1)+gm*Uj(i);
  40.     end
  41.     for i=0:Nx
  42.         Uj(i+1)=Uj1(i+1);
  43.     end
  44.     t = t + Ht;
  45. end
  46. % nexttile
  47. f1 = figure;
  48. f1.Position= [0 0 800 350];
  49. plot(x, Uex)
  50. hold on
  51. plot(x, Uj, 'o')
  52. legend('точ. решение', 'приб. решение')
  53. xlabel('x')
  54. ylabel('U(x,t)')
  55.  
  56. %точно решение для a<0
  57. % Uex_flip = zeros(1, Nx+1);
  58. % for i=0:Nx
  59. %     if(X_L-x(i+1)>=abs(a)*t_entered)
  60. %         Uex_flip(i+1)= U_0(X_L-x(i+1)-abs(a)*t_entered);
  61. %     else
  62. %         Uex_flip(i+1)= mu(t_entered-(X_L-x(i+1))/abs(a));
  63. %     end
  64. % end
  65. % приб решение для а<0
  66. % Uj_flip = flip(Uj);
  67. %
  68. % nexttile
  69. % f = figure;
  70. % f.Position = [0 0 800 350];
  71. % plot(x, Uex_flip)
  72. % hold on
  73. % plot(x, Uj_flip, 'o')
  74. % legend('точ. решение', 'приб. решение')
  75. % xlabel('x')
  76. % ylabel('U(x,t)')
  77.  
  78.  
  79. %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  80. %anotconst.m
  81. Nx = 400;
  82. X_L = 10;
  83. % U_0 = @(x) x/X_L*abs(sin(2*x/3));
  84. % mu = @(t) (1-exp(-t/10)+sin(2*t)^2/(1+t/100))/2;
  85. U_0 = @(x) sqrt(x/X_L)*sqrt(abs(sin(4*x/3)));
  86. mu = @(t) 1/3*(1-exp(-t/15))*(cos(t)+sin(2*t))^2;
  87. Hx = X_L/Nx;
  88. B = 1;
  89. x = zeros(1, Nx+1);
  90. for i=0:Nx
  91.     x(i+1)=i*Hx;
  92. end
  93. t_entered = 5;
  94. Uj = zeros(1, Nx+1);
  95. for i=0:Nx
  96.     Uj(i+1)=U_0(x(i+1));
  97. end
  98. Uj1 = zeros(1,Nx+1);
  99. t = 0;
  100. while(t<=t_entered)
  101.     amax = sin(t/4.5-x(1)/2);
  102.     for i=2:Nx+1
  103.         a = sin(t/4.5-x(i)/2);
  104.         if(abs(a)>amax)
  105.             amax = abs(a);
  106.         end
  107.     end
  108.     if(amax~=0)
  109.         Ht=B*Hx/amax;
  110.     else
  111.         Ht=B*Hx;
  112.     end
  113.     Uj1(1)=mu(t);
  114.     for i=0:Nx
  115.         a(i+1) = sin(t/4.5-x(i+1)/2);
  116.         gm = a(i+1)*Ht/Hx;
  117.         nn=1;
  118.         if(i==0)
  119.             nn=0;
  120.         end
  121.         if(i==Nx)
  122.             nn=2;
  123.         end
  124.         switch(nn)
  125.             case 0
  126.                 if(gm<0)
  127.                     Uj1(i+1)=(1+gm)*Uj(i+1)-gm*Uj(i+2);
  128.                 end
  129.             case 1
  130.                 if(gm>=0)
  131.                     Uj1(i+1)=(1-gm)*Uj(i+1)+gm*Uj(i);
  132.                 else
  133.                     Uj1(i+1)=(1+gm)*Uj(i+1)-gm*Uj(i+2);
  134.                 end
  135.             case 2
  136.                 if(gm>0)
  137.                     Uj1(i+1)=(1-gm)*Uj(i+1)+gm*Uj(i);
  138.                 else
  139.                     Uj1(i+1)=Uj(i+1);
  140.                 end
  141.         end
  142.     end
  143.     for l=0:Nx
  144.             Uj(l+1)=Uj1(l+1);
  145.     end
  146.     t = t+Ht;
  147. end
  148. f1 = figure;
  149. plot(x, Uj)
  150. xlabel('x')
  151. ylabel('U(x,t)')
  152.  
  153. f2 = figure;
  154. plot(x, a, 'r')
  155. xlabel('x')
  156. ylabel('a(x,t)')
  157.  
  158. % f1 = figure;
  159. % f1.Position = [0 0 800 350];
  160. % plot(x, Uj)
  161. % hold on
  162.  
  163. % B=1.4;
  164. % t = 0;
  165. % Uj = zeros(1, Nx+1);
  166. % for i=0:Nx
  167. %     Uj(i+1)=U_0(x(i+1));
  168. % end
  169. % Uj1 = zeros(1,Nx+1);
  170. % while(t<=t_entered)
  171. %     amax = sin(t/4.5-x(1)/2);
  172. %     for i=2:Nx+1
  173. %         a = sin(t/4.5-x(i)/2);
  174. %         if(abs(a)>amax)
  175. %             amax = abs(a);
  176. %         end
  177. %     end
  178. %     if(amax~=0)
  179. %         Ht=B*Hx/amax;
  180. %     else
  181. %         Ht=B*Hx;
  182. %     end
  183. %     Uj1(1)=mu(t);
  184. %     for i=0:Nx
  185. %         a(i+1) = sin(t/4.5-x(i+1)/2);
  186. %         gm = a(i+1)*Ht/Hx;
  187. %         nn=1;
  188. %         if(i==0)
  189. %             nn=0;
  190. %         end
  191. %         if(i==Nx)
  192. %             nn=2;
  193. %         end
  194. %         switch(nn)
  195. %             case 0
  196. %                 if(gm<0)
  197. %                     Uj1(i+1)=(1+gm)*Uj(i+1)-gm*Uj(i+2);
  198. %                 end
  199. %             case 1
  200. %                 if(gm>=0)
  201. %                     Uj1(i+1)=(1-gm)*Uj(i+1)+gm*Uj(i);
  202. %                 else
  203. %                     Uj1(i+1)=(1+gm)*Uj(i+1)-gm*Uj(i+2);
  204. %                 end
  205. %             case 2
  206. %                 if(gm>0)
  207. %                     Uj1(i+1)=(1-gm)*Uj(i+1)+gm*Uj(i);
  208. %                 else
  209. %                     Uj1(i+1)=Uj(i+1);
  210. %                 end
  211. %         end
  212. %     end
  213. %     for l=0:Nx
  214. %             Uj(l+1)=Uj1(l+1);
  215. %     end
  216. %     t = t+Ht;
  217. % end
  218. %
  219. % plot(x, Uj, 'o')
  220. % legend('b=1','b=1.4')
Advertisement
Add Comment
Please, Sign In to add comment