Advertisement
asidesi123

Untitled

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