Advertisement
asidesi123

Untitled

Apr 8th, 2020
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.85 KB | None | 0 0
  1. clear;
  2. clear all;
  3. clc;
  4. %constants
  5. W=2*pi; %width of mesh grid
  6. L=4*pi; %length of mesh grid
  7. t_final=5; %max time
  8. dx=2*pi/100; % point seperation in x-axis    
  9. dy=dx; %point seperation in y-axis      
  10. dt=0.01; %point seperation in time-axis
  11. alpha=0.1; %thermal diffusivity constant in x-direction      
  12. beta=alpha; %thermal diffusivity constant in y-direction        
  13. n=round((L/dx)+1); %number of points in x-asix
  14. m=round((W/dy)+1); %number of points in y-axis
  15. tt=(t_final/dt)+1  %number of points in z-axis
  16. %%
  17. x=zeros(n,1);   %x vector
  18. for i=1:n
  19.     x(i)=(i-1)*dx;
  20. end
  21. y=zeros(m,1);   %y vector
  22. for j=1:m
  23.     y(j)=(j-1)*dy;
  24. end
  25. t=zeros(tt,1);    %t vector
  26. for k=1:tt
  27.     t(k)=(k-1)*dt;
  28. end
  29. %%
  30. T=zeros(n,m,tt); % realisations
  31. for k=1;
  32.     for j=1:m
  33.         for i = 1:n
  34.         T(i,j,k)=20*cos(x(i))*sin(y(j));
  35.         end
  36.     end
  37. end
  38.  
  39.  
  40. %Setting up velocity
  41. v=zeros(n,tt);
  42. for k=1:tt
  43. for i=1:n
  44.     v(i,k)=5*(1-(((x(i))-2*pi)^2/(4*pi^2)))*cos(pi*t(k))*sin(x(i));
  45. end
  46. end
  47.  
  48. as=zeros(n,tt);
  49. an=zeros(n,tt);
  50. for k =1:tt
  51.     for i=1:n
  52.         as(i,k)=(-v(i,k)*dt/(2*dy))-(beta*dt/(dy^2));
  53.         an(i,k)=(v(i,k)*dt/(2*dy))-(beta*dt/(dy^2));
  54.     end
  55. end
  56.  
  57. aw=(-alpha*dt/(dx^2));
  58. ap=1+(2*alpha*dt/(dx^2))+(2*beta*dt/(dy^2));
  59. ae=-alpha*dt/(dx^2);
  60.  
  61. b=zeros(n*m,tt); %Setting up matrix b
  62. k=1;
  63. for j =1:m
  64.     for i=1:n
  65.         P=(j-1)*n+i;
  66.         b(P,k)=20*cos(x(i))*sin(y(j));
  67.     end
  68. end
  69.  
  70. Temp(:,k)=b(:,k);
  71.  
  72. A=zeros(n*m);  %setting up matrix A
  73.  
  74. %matrix A
  75. for k=2:tt
  76. %T1
  77. for i=2:n-1
  78.     for j=2:m-1
  79.         P=(j-1)*n+i;
  80.         A(P,P)=ap;
  81.         A(P,P+1)=ae;
  82.         A(P,P-1)=aw;
  83.         A(P,P+n)=an(i,k);
  84.         A(P,P-n)=as(i,k);
  85.     end
  86. end
  87. %T2
  88. for i=1
  89.     for j=2:m-1
  90.         P=(j-1)*n+i;
  91.         A(P,P)=ap;
  92.         A(P,P+1)=ae+aw;
  93.         A(P,P-n)=as(i,k);
  94.         A(P,P+n)=an(i,k);
  95.     end
  96. end
  97.  
  98. %T3
  99. for i=n
  100.     for j=2:m-1
  101.         P=(j-1)*n+i;
  102.         A(P,P)=ap;
  103.         A(P,P-1)=ae+aw;
  104.         A(P,P-n)=as(i,k);
  105.         A(P,P+n)=an(i,k);
  106.     end
  107. end
  108.  
  109. %T4
  110. for j=1
  111.     for i=2:n-1
  112.         P=(j-1)*n+i;
  113.         A(P,P)=ap;
  114.         A(P,P+1)=ae;
  115.         A(P,P+(m-2)*n)=as(i,k);
  116.         A(P,P+n)=an(i,k);
  117.         A(P,P-1)=aw;
  118.     end
  119. end
  120.  
  121. %T5
  122. for j=m
  123.     for i=2:n-1
  124.         P=(j-1)*n+i;
  125.         A(P,P)=ap;
  126.         A(P,P+1)=ae;
  127.         A(P,P-n)=as(i,k);
  128.         A(P,P-(m-2)*n)=an(i,k);
  129.         A(P,P-1)=aw;
  130.     end
  131. end
  132.  
  133. %T6
  134. for i=1
  135.     for j=1
  136.         P=(j-1)*n+i;
  137.         A(P,P)=ap;
  138.         A(P,P+1)=aw+ae;
  139.         A(P,P+n)=an(i,k);
  140.         A(P,P+(m-2)*n)=as(i,k);
  141.     end
  142. end
  143.  
  144. %T7
  145. for i=n
  146.     for j=1
  147.         P=(j-1)*n+i;
  148.         A(P,P)=ap;
  149.         A(P,P-1)=aw+ae;
  150.         A(P,P+n)=an(i,k);
  151.         A(P,P+(m-2)*n)=as(i,k);
  152.     end
  153. end
  154.  
  155. %T8
  156. for i=1
  157.     for j=m
  158.         P=(j-1)*n+i;
  159.         A(P,P)=ap;
  160.         A(P,P+1)=aw+ae;
  161.         A(P,P-n)=as(i,k);
  162.         A(P,P-(m-2)*n)=an(i,k);
  163.     end
  164. end
  165.  
  166. %T9
  167. for i=n
  168.     for j=m
  169.         P=(j-1)*n+i;
  170.         A(P,P)=ap;
  171.         A(P,P-1)=aw+ae;
  172.         A(P,P-n)=as(i,k);
  173.         A(P,P-(m-2)*n)=an(i,k);
  174.     end
  175. end
  176. A=sparse(A);
  177. Temp(:,k)=A\b(:,k-1);
  178. b(:,k)=Temp(:,k);
  179. end
  180. %% Graphs
  181.  
  182. %t=0
  183. figure(1)
  184.     T1_init=reshape(b(:,t==0),[n,m]);
  185.     T1=flipud(T1_init');
  186.     contourf(x,y,T1);
  187.     c=colorbar;  
  188.     c.Label.String = ['Temperature in ' char(176) 'C'];
  189.     title('Temperature distribution at 0 seconds');
  190.     xlabel('Length (m)');
  191.     ylabel('Width (m)');
  192.    
  193. %t=0.1
  194. figure(2)
  195.     T2_init=reshape(b(:,t==0.1),[n,m]);
  196.     T2=flipud(T2_init');
  197.     contourf(x,y,T2);
  198.     c=colorbar;
  199.     c.Label.String = ['Temperature in ' char(176) 'C'];
  200.     title('Temperature distribution at 0.1 seconds');
  201.     xlabel('Length (m)');
  202.     ylabel('Width (m)');
  203.    
  204. %t=0.5  
  205. figure(3)
  206.     T3_init=reshape(b(:,t==0.5),[n,m]);
  207.     T3=flipud(T3_init');
  208.     contourf(x,y,T3);
  209.     c=colorbar;
  210.     c.Label.String = ['Temperature in ' char(176) 'C'];
  211.     title('Temperature distribution at 0.5 seconds');
  212.     xlabel('Length (m)');
  213.     ylabel('Width (m)');
  214.  
  215. %t=1
  216. figure(4)
  217.     T4_init=reshape(b(:,t==1),[n,m]);
  218.     T4=flipud(T4_init');
  219.     contourf(x,y,T4);
  220.     c=colorbar;
  221.     c.Label.String = ['Temperature in ' char(176) 'C'];
  222.     title('Temperature distribution at 1 second');
  223.     xlabel('Length (m)');
  224.     ylabel('Width (m)');    
  225.    
  226. %t=2
  227. figure(5)
  228.     T5_init=reshape(b(:,t==2),[n,m]);
  229.     T5=flipud(T5_init');
  230.     contourf(x,y,T5);
  231.     c=colorbar;
  232.     c.Label.String = ['Temperature in ' char(176) 'C'];
  233.     title('Temperature distribution at 2 seconds');
  234.     xlabel('Length (m)');
  235.     ylabel('Width (m)');
  236.  
  237. %t=5
  238. figure(6)
  239.     T6_init=reshape(b(:,t==5),[n,m]);
  240.     T6=flipud(T6_init');
  241.     contourf(x,y,T6);
  242.     c=colorbar;
  243.     c.Label.String = ['Temperature in ' char(176) 'C'];
  244.     title('Temperature distribution at =5 seconds');
  245.     xlabel('Length (m)');
  246.     ylabel('Width (m)');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement