声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 5909|回复: 23

[共享资源] [转帖]matlab编写的流体计算和传热程序

 关闭 [复制链接]
发表于 2006-6-18 07:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
  1. %Temperature distribution in a rod
  2. %See example 9
  3. %Discretization method: Finite difference
  4. % Solution method: SOR
  5. clear all;
  6. a=[];b=[];c=[];d=[];x=[];T=[];analytical=[];
  7. nn = input('Number of increment = ')
  8. n = nn+1;% number of  grid points
  9. L = 0.6;
  10. dx = L/nn; % size of increment
  11. Qprim = 50000; % heat source
  12. lambda = 20; % thermal conductivity
  13. for k = 1:n % Set coefficients
  14.    a(k)=2.0;
  15.    b(k)=1.0;
  16.    c(k)=1.0;
  17.    d(k)=Qprim*dx^2/lambda;
  18.    T(k) = 30.0; % start value
  19.    x(k) = (k-1)*dx;
  20. end; %
  21. a(1) = 3;
  22. b(1) = 4;
  23. c(1) = 0;
  24. % Solver SOR if omega = 1 Gauss-Siedel
  25. omega=2/(1+sin(pi/nn));
  26. counter = 0;
  27. maxit = 200;
  28. sumres = 1.;
  29. maxres = 1.0e-5;
  30. while ((sumres > maxres)&(counter < maxit) )
  31.    for k = 2:n-1 % SOR
  32.       T(k)=omega*(b(k)*T(k+1)+c(k)*T(k-1)+d(k))/a(k)+(1-omega)*T(k);
  33.    end; % SOR
  34.    d(1) =-T(3);
  35.    T(1)=(b(1)*T(2)+d(1))/a(1);% Insulated end
  36.    sumres = 0.0
  37.    for k = 2:n-1 % residual
  38.       res=abs(a(k)*T(k)-(b(k)*T(k+1)+c(k)*T(k-1)+d(k)));
  39.       sumres = sumres+res;
  40.    end % residual
  41.    summa = sumres
  42.    counter = counter +1 % counts number of iterations   
  43. end; %while   
  44. %Analytical solution
  45. for k = 1:n
  46.    analytical(k)=Qprim*(L^2-x(k)^2)/(lambda*2)+T(n);
  47. end;
  48. % Plot
  49. hold on;
  50. plot(x,T,'*');
  51. plot(x,analytical,'o');
  52. hold off;
  53. legend('Numerical','Analytical',0);
  54. title('Temperature distribution');
复制代码

[ 本帖最后由 suffer 于 2006-10-9 19:49 编辑 ]

评分

1

查看全部评分

回复
分享到:

使用道具 举报

发表于 2006-10-9 19:50 | 显示全部楼层
  1. %Temperature distribution in a rod
  2. %See example 9
  3. %Discretization method: Finite difference
  4. % Solution method: SOR

  5. clear all;
  6. T = [];x=[];P=[];Q=[];
  7. nn = input('Number of increment = ')
  8. n = nn+1;% number of  grid points
  9. L = 0.6;
  10. dx = L/nn; % size of increment
  11. Qprim = 50000; % heat source
  12. lambda = 20; % thermal conductivity
  13. x = 0:dx:L;
  14. T(n) = 30;
  15. % Solver TDMA
  16. % first order appr. at boundary to get a tri-diagonal matrix
  17. % dT/dx = 0 gives  T(1)=T(2) results in P(1)=1
  18. P(1) = 1;
  19. Q(1) = 0;
  20. a = 2; b = 1; c = 1;
  21. d = Qprim*dx^2/lambda;
  22. for k = 2:n-1
  23.    P(k) = b/(a-c*P(k-1));
  24.    Q(k) = (c*Q(k-1)+d)/(a-c*P(k-1));
  25. end;
  26. for k = n-1:-1:2
  27.    T(k) = P(k)*T(k+1)+Q(k);
  28. end;
  29. T(1)=T(2);% Insulated end
  30. % Analytical solution
  31. for k = 1:n
  32.    analytical(k)=Qprim*(L^2-x(k)^2)/(lambda*2)+T(n);
  33. end;   
  34. % Plot
  35. hold on;
  36. plot(x,T,'*');
  37. plot(x,analytical,'o');
  38. hold off;
  39. legend('Numerical','Analytical',0);
  40. title('Temperature distribution');
复制代码
发表于 2006-10-9 19:50 | 显示全部楼层
  1. %Temperature distribution in a rod
  2. %See example 9
  3. %Finite difference methof
  4. %TDMA
  5. clear all;
  6. T = [];P=[];Q=[];analytical=[];
  7. nn = input('Number of increment = ')
  8. n = nn+1;% number of  grid points
  9. L = 0.6;
  10. dx = L/nn; % size of increment
  11. Qprim = 50000; % heat source
  12. lambda = 20; % thermal conductivity
  13. x = 0:dx:L;
  14. T(n) = 30;
  15. for k =1:n
  16.    T(k)=30;
  17. end;   
  18. % Solver TDMA
  19. % second order appr. dT/dx = 0 gives  3T(1)=4T(2)-T(3)
  20. % to get a tri-diagonal matrix the eqn. T(1) = T(1) is used
  21. % For the results in P(1)=0 and Q(1)=T(1)
  22. a = 2; b = 1; c = 1;
  23. d = Qprim*dx^2/lambda;
  24. counter = 0;
  25. maxit = 200;
  26. sumres = 1.;
  27. maxres = 1.0e-5;
  28. P(1) = 0;
  29. while ((sumres > maxres)&(counter < maxit) )
  30.    Q(1) = T(1);
  31.    sumres = 0;
  32.    for k = 2:n-1
  33.       P(k) = b/(a-c*P(k-1));
  34.       Q(k) = (c*Q(k-1)+d)/(a-c*P(k-1));
  35.       sumres=sumres+abs(a*T(k)-(b*T(k+1)+c*T(k-1)+d));
  36.    end;
  37.    for k = n-1:-1:1
  38.       T(k) = P(k)*T(k+1)+Q(k);
  39.    end;
  40.    T(1)=(4*T(2)-T(3))/3;
  41.    counter=counter +1
  42. end;
  43. for k = 1:n
  44.    analytical(k)=Qprim*(L^2-x(k)^2)/(lambda*2)+T(n);
  45. end;   
  46. hold on;
  47. plot(x,T,'*');
  48. plot(x,analytical,'o');
  49. hold off;
  50. legend('Numerical','Analytical',0);
  51. title('Temperature distribution');
复制代码
发表于 2006-10-9 19:51 | 显示全部楼层
  1. %Temperature distribution in a rod
  2. %See example 9
  3. %Finite volume method
  4. %TDMA
  5. clear all;
  6. T = [];P=[];Q=[];ae=[];aw=[];ap=[];analytical=[];
  7. nn = input('Number of control volumes = ');
  8. n = nn+2;% number of  grid points
  9. L = 0.6;
  10. dx = L/nn; % size of increment
  11. Qprim = 50000; % heat source
  12. lambda = 20; % thermal conductivity
  13. x(1)=0;x(2)=dx/2;
  14. for k = 3:n-1
  15.     x(k)=x(k-1)+dx;
  16. end
  17. x(n)=L;
  18. T(n) = 30;
  19. for k =1:n
  20.    T(k)=30;
  21. end;   
  22. % Solver TDMA
  23. % P(1)=0 and Q(1)=T(1)
  24. for k = 2:n-1
  25.   d(k)=Qprim*dx;
  26.   ae(k)=lambda/dx;
  27.   aw(k)=lambda/dx;
  28.   if (k==2);aw(k)=0;end;
  29.   if (k==n-1);ae(k)=2*lambda/dx;end;
  30.   ap(k)=ae(k)+aw(k);
  31. end;  
  32. P(1) = 0;
  33. Q(1) = T(1);
  34. sumres = 0;
  35. for k = 2:n-1
  36.     P(k) = ae(k)/(ap(k)-aw(k)*P(k-1));
  37.     Q(k) = (aw(k)*Q(k-1)+d(k))/(ap(k)-aw(k)*P(k-1));
  38. end;
  39. for k = n-1:-1:1
  40.     T(k) = P(k)*T(k+1)+Q(k);
  41. end;
  42. T(1)=T(2);
  43. for k = 1:n
  44.    analytical(k)=Qprim*(L^2-x(k)^2)/(lambda*2)+T(n);
  45. end;   
  46. hold on;
  47. plot(x,T,'*');
  48. plot(x,analytical,'o');
  49. hold off;
  50. legend('Numerical','Analytical',0);
  51. title('Temperature distribution');
复制代码
发表于 2006-10-9 19:51 | 显示全部楼层
  1. % Transient temperature distribution in aninfinite plate with the thickness 2L.
  2. % Initially temperature is Tinit when it is exposed to a fluid with
  3. % the temperature Tfluid.
  4. % Explicit method
  5. %
  6. clear all;
  7. n = 20; % number of increments
  8. nk = n+1; % number of node points
  9. L = 0.05; % half thickness
  10. dx = L/n % increment
  11. dt =0.1 % time step
  12. conductivity = 40; % thermal conductivity
  13. density = 8000; % density
  14. cp = 500 % Heat capacity
  15. alfa = 1600; % heat transfer coefficient
  16. diffusivity = conductivity/(density*cp); % thermal diffusivity
  17. maxtime = 100; % total time
  18. Tinit = 1; % initial temperature (dimensionless)
  19. Tfluid = 0; % fluid temperature (dimensionless)
  20. for k = 1:nk
  21.    x(k) = (k-1)*dx;
  22. end;
  23. % Explicit formulation
  24. for k = 1:nk % coefficients
  25.    t(k) = Tinit;
  26.    told(k) = Tinit;
  27.    a(k) = 1;
  28.    b(k) = diffusivity*dt/dx^2;
  29.    c(k) = diffusivity*dt/dx^2;
  30.    d(k) = 1 - 2*diffusivity*dt/dx^2;
  31. end;
  32. % second order approximation at boundary
  33. c(1) = 0;
  34. b(1) = 4;
  35. a(1) = 3 + 2*alfa*dx/conductivity;
  36. a(nk) = 3;
  37. b(nk) = 0;
  38. c(nk) = 4;
  39. time = 0
  40. while (time < (maxtime+dt/2))
  41.    told = t;
  42.    d(1) = 2*Tfluid*alfa*dx/conductivity - t(3);
  43.    t(1)=(b(1)*t(2)+d(1))/a(1);
  44.    for k = 2:n
  45.       t(k) = (b(k)*told(k+1)+c(k)*told(k-1)+d(k)*told(k))/a(k);
  46.    end;
  47.    d(nk)=-t(nk-2);
  48.    t(nk) =(c(nk)*t(nk-1)+d(nk))/a(nk);
  49.    time = time +dt;
  50. end; %while
  51. %
  52. plot(x,t,'*');ylabel('Temperature');xlabel('x-pos');
  53. title('Transient temperature distribution');
复制代码
发表于 2006-10-9 19:51 | 显示全部楼层
  1. % Transient temperature distribution in aninfinite plate with the thickness 2L.
  2. % Initially temperature is Tinit when it is exposed to a fluid with
  3. % the temperature Tfluid.
  4. % Explicit method
  5. %
  6. clear all;
  7. t=[];ap=[];aw=[];ae=[];d=[];
  8. n_cv = 30; % number of control volumes
  9. ni = n_cv+2; % number of node points
  10. L = 0.05; % half thickness
  11. dx = L/n_cv % increment
  12. dt =0.1 % time step
  13. conductivity = 40; % thermal conductivity
  14. density = 8000; % density
  15. cp = 500 % Heat capacity
  16. alfa = 1600; % heat transfer coefficient
  17. maxtime = 100; % total time
  18. Tinit = 1; % initial temperature (dimensionless)
  19. Tfluid = 0; % fluid temperature (dimensionless)
  20. x(1) = 0;
  21. x(2) = dx/2;
  22. for k = 3:ni-1
  23.    x(k) = (k-1)*dx;
  24. end;
  25. x(ni) = L;
  26. % Explicit formulation
  27. for k = 1:ni % coefficients
  28.    t(k) = Tinit;
  29.    told(k) = Tinit;
  30.    ap(k) = density*cp*dx/dt;
  31.    ae(k) = conductivity/dx;
  32.    aw(k) = conductivity/dx;
  33.    if (k==2);aw(k)=0;end;
  34.    if (k==ni-1);ae(k)=0;end;  
  35.    d(k) = ap(k)- aw(k)-ae(k);
  36. end;
  37. time = 0;
  38. while (time < (maxtime+dt/2))
  39.    told = t;
  40.    for k = 2:ni-1
  41.        dd = d(k)*told(k);
  42.        if k == 2;
  43.          dd = d(k)*told(k)+(Tfluid-told(k))/(1/alfa+2*dx/conductivity);  
  44.        end;  
  45.        t(k) = (ae(k)*told(k+1)+aw(k)*told(k-1)+dd)/ap(k);
  46.    end;
  47.    time = time +dt;
  48. end; %while
  49. % Boundaries
  50. t(ni)=t(ni-1)
  51. t(1)=(alfa*Tfluid+2*conductivity/dx*t(2))/(alfa+2*conductivity/dx);
  52. %
  53. plot(x,t,'*');ylabel('Temperature');xlabel('x-pos');
  54. title('Transient temperature distribution');
复制代码
发表于 2006-10-9 19:52 | 显示全部楼层
  1. % Transient temperature distribution in an
  2. % infinite plate with the thickness 2L.
  3. % Initially the plate has the temperature
  4. % Tinit when it is exposed to a fluid with
  5. % the temperature Tfluid.
  6. % Implicite scheme
  7. % Solver Gauss-Siedel
  8. clear all;
  9. n = 20; % number of increments
  10. nk = n+1; % number of node points
  11. L = 0.05; % half thickness
  12. dx = L/n; % increment
  13. dt = 1.0 % time step
  14. lambda = 40; % thermal conductivity
  15. alfa = 1600; % heat transfer coefficient
  16. diffusivity = 1.0e-6; % thermal diffusivity
  17. maxtime = 100; % total time
  18. Tinit = 1; % initial temperature (dimensionless)
  19. Tfluid = 0; % fluid temperature (dimensionless)
  20. x=[];a=[];b=[];c=[];d=[];t=[];told=[];
  21. x = 0:dx:L;

  22. % Fully Implicit formulation
  23. for k = 1:nk % coefficients
  24.    t(k) = Tinit;
  25.    told(k) = Tinit;
  26.    a(k) = 1+2*diffusivity*dt/dx^2;
  27.    b(k) = diffusivity*dt/dx^2;
  28.    c(k) = diffusivity*dt/dx^2;
  29.    d(k) = 1;
  30. end;
  31. % second order approximation at boundary
  32. c(1) = 0;
  33. b(1) = 4;
  34. a(1) = 3 + 2*alfa*dx/lambda;
  35. a(nk) = 3;
  36. b(nk) = 0;
  37. c(nk) = 4;
  38. time = 0;
  39. maxres = 1.e-5;
  40. maxit = 10;
  41. while (time < (maxtime+dt/2))
  42.    told = t;
  43.    sumres = 1;
  44.    counter = 0;
  45.    while ((sumres > maxres)&(counter < maxit) )
  46.      d(1) = 2*Tfluid*alfa*dx/lambda - t(3);
  47.      t(1)=(b(1)*t(2)+d(1))/a(1);
  48.      for k = 2:n
  49.         d(k)=told(k);
  50.         t(k) = (b(k)*t(k+1)+c(k)*t(k-1)+d(k))/a(k);
  51.      end;
  52.      d(nk)=-t(nk-2);
  53.      t(nk) =(c(nk)*t(nk-1)+d(nk))/a(nk);
  54.      sumres = 0.0;
  55.      for k = 2:n % residual
  56.        res=abs(a(k)*t(k)-(b(k)*t(k+1)+c(k)*t(k-1)+told(k)));
  57.        sumres = sumres+res;
  58.      end % residual
  59.      summa = sumres
  60.      counter = counter +1 % counts number of iterations   
  61.    end; %while convergence loop   
  62.    time = time +dt;
  63. end; %while  time loop
  64. plot(x,t,'*');ylabel('Temperature');xlabel('x-pos');
  65. title('Transient temperature distribution');
复制代码
发表于 2006-10-9 19:52 | 显示全部楼层
  1. % Transient temperature distribution in an infinite plate with the thickness 2L.
  2. % Initially the plate is at Tinit when it is exposed to a fluid with
  3. % the temperature Tfluid.
  4. % Implicit scheme
  5. % Finite volume method
  6. % Solver TDMA
  7. t=[];ap=[];aw=[];ae=[];d=[];Q=[];P=[];
  8. n_cv = 30; % number of control volumes
  9. ni = n_cv+2; % number of node points
  10. L = 0.05; % half thickness
  11. dx = L/n_cv % increment
  12. dt =1 % time step
  13. conductivity = 40; % thermal conductivity
  14. density = 8000; % density
  15. cp = 500 % Heat capacity
  16. alfa = 1600; % heat transfer coefficient
  17. maxtime = 100; % total time
  18. Tinit = 1; % initial temperature (dimensionless)
  19. Tfluid = 0; % fluid temperature (dimensionless)
  20. x(1) = 0;
  21. x(2) = dx/2;
  22. for k = 3:ni-1
  23.    x(k) = (k-1)*dx;
  24. end;
  25. x(ni) = L;
  26. % Implicit formulation
  27. for k = 1:ni % coefficients
  28.    t(k) = Tinit;
  29.    told(k) = Tinit;
  30.    ae(k) = conductivity/dx;
  31.    aw(k) = conductivity/dx;
  32.    if (k==2);aw(k)=0;end;
  33.    if (k==ni-1);ae(k)=0;end;  
  34.    d(k) = density*cp*dx/dt;
  35.    ap(k)=aw(k)+ae(k)+density*cp*dx/dt;
  36. end;
  37. time = 0;
  38. maxres = 1.e-5;
  39. maxit = 10;
  40. while (time < (maxtime+dt/2))
  41.    told = t;
  42.    P(1) = 0;
  43.    Q(1) = t(1);
  44.    for k = 2:ni-1
  45.       dd = d(k)*told(k);
  46.       a = ap(k);
  47.       if k == 2;
  48.         a = ap(k)+1/(1/alfa+2*dx/conductivity);  
  49.         dd = d(k)*told(k)+Tfluid/(1/alfa+2*dx/conductivity);        
  50.       end;
  51.       P(k) = ae(k)/(a-aw(k)*P(k-1));
  52.       Q(k) = (aw(k)*Q(k-1)+dd)/(a-aw(k)*P(k-1));
  53.    end;
  54.    for k = ni-1:-1:2
  55.      t(k) = P(k)*t(k+1)+Q(k);
  56.    end;
  57.    time = time +dt;
  58. end; %while  time loop
  59. % Boundaries
  60. t(ni)=t(ni-1)
  61. t(1)=(alfa*Tfluid+2*conductivity/dx*t(2))/(alfa+2*conductivity/dx);
  62. %
  63. plot(x,t,'*');ylabel('Temperature');xlabel('x-pos');
  64. title('Transient temperature distribution');
复制代码
发表于 2006-10-9 19:52 | 显示全部楼层
  1. % Assignment 6 MMV042A 2003
  2. % Boundary layer eqn.
  3. % Tangential flow along a flat plate
  4. % Marching procedure in x-direction
  5. % using TDMA in y-direction
  6. clear all;
  7. x=[];y=[];P=[];Q=[];U=[];V=[];T=[];
  8. viscosity = 18.1e-6; % viscosity
  9. density = 1.19 ;
  10. kvisc = viscosity/density; % kinematic viscosity
  11. Pr = 0.72; % Prandtl number
  12. Umax = 5; % free stream velocity
  13. maxres = 1e-5; % max residual
  14. maxit = 3;
  15. nx = input('Number of increment in x-dir= ')
  16. ny = input('Number of increment in y-dir= ')
  17. ni = nx +1;
  18. nj = ny +1;
  19. % Increment length in y-dir 0.05 - 0.2 mm
  20. % Increment length in x-dir 0.1 - 1.0 mm
  21. dx = input('Increment length in x-dir (mm)= ')
  22. dy = input('Increment length in y-dir (mm)=  ')
  23. dx = dx/1000;
  24. dy = dy/1000;
  25. x=0:dx:((ni-1)*dx)
  26. y=0:dy:(nj-1)*dy
  27. % Boundary and initial values
  28. for i=1:ni
  29.    for j=1:nj
  30.       U(i,j)=Umax;
  31.       if j==1; U(i,j)=0; end;
  32.       V(i,j) = 0;
  33.       T(i,j) = 0;
  34.       if j==1; T(i,j)=1.0; end;
  35.    end;
  36. end;  
  37. counter = 0;
  38. for i = 2:ni
  39.    counter = 0;
  40.    sumres = 1;
  41.    while ((sumres > maxres)&(counter < maxit) )
  42.       P(1)=0;Q(1)=0;
  43.       sumres=0;
  44. %% momentuum eqn      
  45.       for j = 2:nj-1
  46.          a = U(i,j)/dx+2*kvisc/dy^2;
  47.          b = kvisc/dy^2-V(i,j)/(2*dy);
  48.          c = kvisc/dy^2+V(i,j)/(2*dy);
  49.          d = U(i,j)*U(i-1,j)/dx;
  50.          P(j)=b/(a-c*P(j-1));
  51.          Q(j)=(c*Q(j-1)+d)/(a-c*P(j-1));
  52.          sumres=sumres+abs(a*U(i,j)-(b*U(i,j+1)+c*U(i,j-1)+d));
  53.       end;
  54.       for j=nj-1:-1:2
  55.          U(i,j) = P(j)*U(i,j+1)+Q(j);
  56.       end;
  57.       P(1)=0;Q(1)=0;
  58. %%continuity eqn.
  59.       for j = 2:nj-1
  60.          a = 1/dy;
  61.          b = 0;
  62.          c = 1/dy;
  63.          d = -((U(i,j)-U(i-1,j))+(U(i,j-1)-U(i-1,j-1)))/(2*dx);
  64.          P(j)=b/(a-c*P(j-1));
  65.          Q(j)=(c*Q(j-1)+d)/(a-c*P(j-1));
  66.          sumres=sumres+abs(a*V(i,j)-(b*V(i,j+1)+c*V(i,j-1)+d));
  67.       end;
  68.       for j=nj-1:-1:2
  69.          V(i,j) = P(j)*V(i,j+1)+Q(j);
  70.       end;
  71.       counter = counter +1; % counts number of iterations   
  72.    end;
  73.    TotalResidual = sumres
  74. end;
  75. for i = 2:ni % Temperature field
  76.    P(1)=0;Q(1)=T(i,1);  % TDMA
  77. %   for j = 2:nj-1
  78. %      a =???
  79. %      b =???
  80. %      c =???
  81. %      d =???
  82. %      P(j)=b/(a-c*P(j-1));
  83. %      Q(j)=(c*Q(j-1)+d)/(a-c*P(j-1));
  84. %    end;
  85. %    for j=nj-1:-1:2
  86. %         T(i,j) = P(j)*T(i,j+1)+Q(j);
  87. %    end;
  88. end;
  89. for i=2:ni
  90.    % calc. Nusselt number and friction factor
  91. end;   
  92. subplot(2,1,1);pcolor(x,y,U');shading interp;title('U-velocity');colorbar;
  93. % subplot(2,1,2);pcolor(x,y,T');shading interp;title('Temperature');colorbar;
复制代码
发表于 2006-10-9 19:53 | 显示全部楼层
  1. % Twodimensional heat conduction
  2. % Finite Volume Method
  3. % SOR
  4. clear all;
  5. x=[];y=[];T=[];Told=[];Su=[];Sp=[];ap=[];ae=[];aw=[];as=[];an=[];
  6. great = 1.e20;
  7. lambda = 10; % thermal conductivity
  8. alfa = 10; % heat transfer coefficient
  9. dt = great; % Time step. If great stedy state
  10. density = 6000;% density
  11. cp = 500;% heat capacity
  12. Lx = 0.12; % length x-direction
  13. Ly = 0.12; % length y -direction
  14. Tfluid = 20; % Fluid temperature
  15. Tinit = 50; % Initial guess and top- and bottom tempearature
  16. %cv_x = input('Number of CVs in x-direction = ')
  17. %cv_y = input('Number of CVs in y-direction = ')
  18. cv_x=10;cv_y=10;
  19. ni = cv_x+2; % grid points x-direction
  20. nj = cv_y+2; % grid points y-direction
  21. dx = Lx/cv_x;
  22. dy = Ly/cv_y;
  23. x(1) = 0;
  24. x(2)=dx/2;
  25. for i = 3:ni-1
  26.    x(i)=x(i-1)+dx;
  27. end;
  28. x(ni)=Lx;
  29. y(1) = 0;
  30. y(2)=dy/2;
  31. for j = 3:nj-1
  32.    y(j)=y(j-1)+dy;
  33. end
  34. y(nj)=Ly;
  35. % Initial values and coefficients
  36. for i = 1:ni
  37.   for j = 1:nj
  38.     T(i,j) = Tinit;  %Initial temperature
  39.     Told(i,j) = Tinit;
  40.     T(i,1) = 50;
  41.     T(i,nj) = 50;
  42.     Su(i,j)=0;       %Initial indendendent source term
  43.     Sp(i,j)=0;       %Initial dependent source term
  44.     ae(i,j) = lambda*dy/dx;
  45.     aw(i,j) = lambda*dy/dx;
  46.     an(i,j) = lambda*dx/dy;
  47.     as(i,j) = lambda*dx/dy;
  48.     dV = dx*dy;
  49.     ap0 = density*cp*dV/dt;
  50.     if i==2  % convective heat transfer boundary
  51.        Su(i,j) = Tfluid/(1/alfa+dx/(2*lambda))*dy/dV;
  52.        Sp(i,j) = -1/(1/alfa+dx/(2*lambda))*dy/dV;
  53.        aw(i,j) = 0;
  54.     end;
  55.     if i==ni-1 % insulated boundary
  56.        ae(i,j) = 0;
  57.     end
  58.     if j==2 % bottom boundary, given temperature
  59.        as(i,j)=2*lambda*dx/dy;
  60.     end   
  61.     if j==nj-1 % top boundary, given temperature
  62.        an(i,j)=2*lambda*dx/dy;
  63.     end   
  64.     ap(i,j) = ae(i,j)+aw(i,j)+an(i,j)+as(i,j)-Sp(i,j)*dV+ap0;
  65.   end;
  66. end;
  67. %%%%%%%%%%%
  68. maxres = 1.0e-6;
  69. maxit = 100;
  70. time=0;
  71. maxtime=100;
  72. s=(cos(pi/cv_x)+(dx/dy)^2*cos(pi/cv_y))/(1+(dx/dy)^2);
  73. omega =2/(1+sqrt(1-s^2));omega=1;
  74. while (time < (maxtime+dt/2))
  75. Told=T;  
  76. sumres = 1;
  77. counter = 0;
  78. while (sumres>maxres&counter<maxit)
  79.   sumres = 0;
  80.   for i = 2:ni-1
  81.    for j = 2:nj-1
  82.     T(i,j)=omega*(ae(i,j)*T(i+1,j)+aw(i,j)*T(i-1,j)+an(i,j)*T(i,j+1)...
  83.        +as(i,j)*T(i,j-1)+Su(i,j)*dV+ap0*Told(i,j))/ap(i,j)+(1-omega)*T(i,j);
  84.     res =  abs(ap(i,j)*T(i,j)-(ae(i,j)*T(i+1,j)+aw(i,j)*T(i-1,j)+...
  85.       an(i,j)*T(i,j+1)+as(i,j)*T(i,j-1)+Su(i,j)*dV+ap0*Told(i,j)));
  86.     sumres=sumres+res;
  87.    end;
  88.   end;
  89.   for i = 2:ni-1
  90.    for j = 2:nj-1
  91.     res =  abs(ap(i,j)*T(i,j)-(ae(i,j)*T(i+1,j)+aw(i,j)*T(i-1,j)+...
  92.       an(i,j)*T(i,j+1)+as(i,j)*T(i,j-1)+Su(i,j)*dV+ap0*Told(i,j)));
  93.     sumres=sumres+res;
  94.    end;
  95.   end

  96.   sumerr=sumres
  97.   counter = counter + 1
  98. end;
  99. time = time +dt;
  100. end;
  101. % Calculate boundary values
  102. for j = 2:nj-1
  103.    T(1,j)=(alfa*Tfluid+lambda/(dx/2)*T(2,j))/(alfa+lambda/(dx/2));
  104.    T(ni,j) = T(ni-1,j);
  105. end;   
  106. %
  107. pcolor(x,y,T');shading interp;xlabel('x');ylabel('y');title('Temperature distribution');colorbar;
  108. %
复制代码
发表于 2006-10-9 19:53 | 显示全部楼层
  1. % Twodimensional heat conduction
  2. % Finite Volume Method
  3. % Line by Line TDMA with SOR
  4. clear all;
  5. x=[];y=[];T=[];Told=[];Su=[];Sp=[];ap=[];ae=[];aw=[];as=[];an=[];
  6. Q=[];P=[];
  7. great = 1e20;
  8. lambda = 10; % thermal conductivity
  9. alfa = 10; % heat transfer coefficient
  10. dt = great; % Time step. If great steady state
  11. density = 6000;% density
  12. cp = 500;% heat capacity
  13. Lx = 0.12; % length x-direction
  14. Ly = 0.12; % length y -direction
  15. Tfluid = 20; % Fluid temperature
  16. Tinit = 50; % Initial guess and top- and bottom tempearature
  17. %cv_x = input('Number of CVs in x-direction = ')
  18. %cv_y = input('Number of CVs in y-direction = ')
  19. cv_x=10;cv_y=10;
  20. ni = cv_x+2; % grid points x-direction
  21. nj = cv_y+2; % grid points y-direction
  22. dx = Lx/cv_x;
  23. dy = Ly/cv_y;
  24. x(1) = 0;
  25. x(2)=dx/2;
  26. for i = 3:ni-1
  27.    x(i)=x(i-1)+dx;
  28. end;
  29. x(ni)=Lx;
  30. y(1) = 0;
  31. y(2)=dy/2;
  32. for j = 3:nj-1
  33.    y(j)=y(j-1)+dy;
  34. end
  35. y(nj)=Ly;
  36. % Initial values and coefficients
  37. for i = 1:ni
  38.   for j = 1:nj
  39.     T(i,j) = Tinit;  %Initial temperature
  40.     Told(i,j) = Tinit;
  41.     T(i,1) = 50;
  42.     T(i,nj) = 50
  43.     Su(i,j)=0;       %Initial indendendent source term
  44.     Sp(i,j)=0;       %Initial dependent source term
  45.     ae(i,j) = lambda*dy/dx;
  46.     aw(i,j) = lambda*dy/dx;
  47.     an(i,j) = lambda*dx/dy;
  48.     as(i,j) = lambda*dx/dy;
  49.     dV = dx*dy;
  50.     ap0 = density*cp*dV/dt;
  51.     if i==2  % convective heat transfer boundary
  52.        Su(i,j) = Tfluid/(1/alfa+dx/(2*lambda))*dy/dV;
  53.        Sp(i,j) = -1/(1/alfa+dx/(2*lambda))*dy/dV;
  54.        aw(i,j) = 0;
  55.     end;
  56.     if i==ni-1 % insulated boundary
  57.        ae(i,j) = 0;
  58.     end
  59.     if j==2 % bottom boundary, given temperature
  60.        as(i,j)=2*lambda*dx/dy;
  61.     end   
  62.     if j==nj-1 % top boundary, given temperature
  63.        an(i,j)=2*lambda*dx/dy;
  64.     end   
  65.     ap(i,j) = ae(i,j)+aw(i,j)+an(i,j)+as(i,j)-Sp(i,j)*dV+ap0;
  66.   end;
  67. end;
  68. %%%%%%%%%%%
  69. maxres = 1.0e-6;
  70. maxit = 100;
  71. time=0;
  72. maxtime = 100;
  73. omega=1;
  74. while (time < (maxtime+dt/2))
  75. Told=T;  
  76. sumres = 1;
  77. counter = 0;
  78. while (sumres>maxres&counter<maxit)
  79.   %Sweep in j-direction  
  80.   for i = 2:ni-1
  81.     P(1) = 0;
  82.     Q(1) = T(1);
  83.     for j = 2:nj-1
  84.       a=ap(i,j)/omega;b=an(i,j);c=as(i,j);
  85.       d=ae(i,j)*T(i+1,j)+aw(i,j)*T(i-1,j)+Su(i,j)*dV +ap0*Told(i,j)...
  86.           +(1-omega)*a*T(i,j);
  87.       P(j) = b/(a-c*P(j-1));
  88.       Q(j) = (c*Q(j-1)+d)/(a-c*P(j-1));
  89.     end;
  90.     for j = nj-1:-1:2
  91.       T(i,j) = P(j)*T(i,j+1)+Q(j);
  92.     end;
  93.   end;
  94.   %Sweep in i-direction
  95.   for j = 2:nj-1  
  96.     P(1) = 0;
  97.     Q(1) = T(1);
  98.     for i = 2:ni-1
  99.       a=ap(i,j)/omega;b=ae(i,j);c=aw(i,j);
  100.       d=an(i,j)*T(i,j+1)+as(i,j)*T(i,j-1)+Su(i,j)*dV+ap0*Told(i,j)...
  101.            +(1-omega)*a*T(i,j);
  102.       P(i) = b/(a-c*P(i-1));
  103.       Q(i) = (c*Q(i-1)+d)/(a-c*P(i-1));
  104.     end;
  105.     for i = ni-1:-1:2
  106.       T(i,j) = P(i)*T(i+1,j)+Q(i);
  107.     end;
  108.   end;
  109.   sumres = 0;
  110.   % Calculate residual
  111.   for i = 2:ni-1
  112.    for j = 2:nj-1   
  113.     res =  abs(ap(i,j)*T(i,j)-(ae(i,j)*T(i+1,j)+aw(i,j)*T(i-1,j)+...
  114.       an(i,j)*T(i,j+1)+as(i,j)*T(i,j-1)+Su(i,j)*dV+ap0*Told(i,j)));
  115.     sumres=sumres+res;
  116.    end;
  117.   end
  118.   sumerr=sumres
  119.   counter = counter + 1
  120. end;%end while
  121. time = time+dt;
  122. end; %end time loop

  123. % Calculate boundary values
  124. for j = 2:nj-1
  125.    T(1,j)=(alfa*Tfluid+lambda/(dx/2)*T(2,j))/(alfa+lambda/(dx/2));
  126.    T(ni,j) = T(ni-1,j);
  127. end;   
  128. %
  129. pcolor(x,y,T');shading interp;xlabel('x');ylabel('y');title('Temperature distribution');colorbar;
  130. %
复制代码

评分

1

查看全部评分

发表于 2006-10-11 21:55 | 显示全部楼层
这个有些看不明白。
发表于 2006-10-12 09:09 | 显示全部楼层
搂主能否给些简单的说明呢
发表于 2006-10-12 14:35 | 显示全部楼层
原帖由 无水1324 于 2006-10-12 09:09 发表
搂主能否给些简单的说明呢


程序开头的注解都有简单的说明
发表于 2006-10-14 20:28 | 显示全部楼层
太长了

有简单的没有?
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-24 17:12 , Processed in 0.061583 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表