not_a_red_panda

Numerical Fourier analysis for random heat distribution

Jun 30th, 2019
1,458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. %Delete all stored variables and clear the command window.
  2. clear
  3. clc
  4. %PARAMETERS:
  5. %Number of terms in Fourier series
  6. N=500;
  7. %Thermal diffusivity
  8. diffusivity=0.000111;
  9. % x axis.
  10. x=linspace(0,1,100);
  11. % Placeholder for graph data
  12. y=zeros(1,length(x));
  13. %These will be used to fix the graph
  14. X=linspace(-0.15,1.15,10);
  15. Y=linspace(0,120,12);
  16. %Time axis.
  17. t=linspace(0,60,60);
  18. %Solution matrix placeholder. Each row will represent T(x,t) for a value of t.
  19. soln=zeros(length(t), length(y));
  20. %Fourier coefficients placeholder.
  21. coeffs = zeros(1,N);
  22. %Temperature data
  23. data=[88 33 70 11 3 75 55 45 90 60];
  24.  
  25. %COMPUTATION:
  26.  
  27. %Compute the coefficients.
  28. for n=1:N
  29. for k=1:10
  30. zmin=0.1*k-0.1;
  31. zmax=0.1*k;
  32. A=data(k);
  33. coeffs(n) = coeffs(n)+integral(@(z)2*A*sin(n*pi*z),zmin,zmax);
  34. end
  35. y = y+coeffs(n)*sin(n*pi*x);
  36. end
  37.  
  38. % Compute the solution.
  39. for a=1:length(t)
  40. T=t(a);
  41. y=zeros(1,length(x));
  42. for n=1:N
  43. y = y+coeffs(n)*sin(n*pi*x)*exp(-1*diffusivity*(n^2)*(pi^2)*T);
  44. end
  45. %The temperature should be greater than or equal to zero and less than or equal to 100, this fixes
  46. %any parts of the data that are slightly out of range due to approximation
  47. %error.
  48. for k = 1:length(y)
  49. if (y(k)<0);
  50. y(k) = 0;
  51. elseif (y(k)>100);
  52. y(k)=100;
  53. else
  54. y(k)=y(k);
  55. end
  56. end
  57. %Row number a of solution matrix is equal to the y array.
  58. soln(a,:)=flip(y);
  59. end
  60. mesh(x,t,soln)
  61. xlabel('x');
  62. ylabel('t');
  63. zlabel('T(x,t) in Celsius');
  64. xticks([0, 0.2, 0.4, 0.6, 0.8, 1.0])
  65. xticklabels({'1', '0.8', '0.6', '0.4', '0.2', '0'});
  66. %This part produces the .gif animation. I don't know for sure if this part will work in
  67. %Scilab without modification.
  68. % fig=figure;
  69. % elapsed=0;
  70. % nImages=length(t);
  71. % for T = 1:nImages
  72. % clf
  73. % if (t(T)-(elapsed+1))>=0
  74. % elapsed = elapsed+1;
  75. % end
  76. % FF=zeros(1,length(Y))-0.125;
  77. % hold on
  78. % plot(x,soln(T,:))
  79. % plot(X,zeros(1,length(X)), 'black');
  80. % plot(FF,Y, 'white');
  81. % hold off
  82. % title(['Elapsed time = ' num2str(elapsed) ' seconds'])
  83. % frame = getframe(fig);
  84. % im{T} = frame2im(frame);
  85. % end
  86. % close
  87. % filename = 'RandomHeatSim1.gif';
  88. % for idx = 1:nImages
  89. % [A,map] = rgb2ind(im{idx},256);
  90. % if idx == 1;
  91. % imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',1.5);
  92. % elseif (idx > 1) && (idx < nImages);
  93. % if (idx>200) && (mod(idx,3)==0) && (idx <= 401) %After 200 frames, 3x frameskip.
  94. % imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0.02);
  95. % elseif (idx<=200)
  96. % imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0.02);
  97. % elseif (idx>401) && (mod(idx,5)==0) && (idx <900) %After 401 frames, 5x frameskip.
  98. % imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0.02);
  99. % elseif (idx>=901) && (mod(idx,10)==0) %After 900 frames, 10x frameskip.
  100. % imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0.02);
  101. % end
  102. % else
  103. % imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',1.5);
  104. % end
  105. % end
Advertisement
Add Comment
Please, Sign In to add comment