Advertisement
not_a_red_panda

Inhomogeneous Heat Equation example 3

Jul 9th, 2019
926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. clear
  2. clc
  3. %Terms in Fourier series. 50 is enough. Any accuracy gained from increasing
  4. %this further is probably not worth the extra computation time.
  5. N=50;
  6. %Diffusivity
  7. diff= 2*10^-2;
  8. %Set the number of terms in the x linspace to 100 for generating the gif
  9. %and 10 or 20 for generating the mesh plot.
  10. x=linspace(0,1,100);
  11. y=zeros(1,length(x));
  12. Y=linspace(0,5,5);
  13. %Desired simulation time in seconds. This is ideally set to a multiple of
  14. %10.
  15. simtime=11;
  16. %Desired frames per second. Set this to 1,2, 5, 10, 20, or 50. Set to 1, 2, or 5
  17. %for the mesh plot, higher values for the gif. Framerates higher than 50 do
  18. %not significantly contribute to the quality of the animation and will
  19. %therefore add unnecessary computation time.
  20. FPS=50;
  21. %Number of frames
  22. framenum=simtime*FPS;
  23. %time axis
  24. t=linspace(0,simtime,framenum+1);
  25. %Solution matrix placeholder. Each row will represent T(x,t) for a value of t.
  26. soln=zeros(length(t), length(y));
  27. %Coefficients
  28. forcing_coeffs=zeros(1,N);
  29. phi_coeffs=zeros(1,N);
  30. %Time when heat is turned off.
  31. switchoff=5;
  32. %Magnitude of forcing.
  33. force=5;
  34.  
  35. for n=1:N
  36. forcing_coeffs(n) = 2*integral(@(z)force*sin(n*pi*z),0.45,0.55);
  37. end
  38.  
  39. %Solution while forcin is ON.
  40. for a=1:FPS*switchoff
  41. y = zeros(1,length(x));
  42. for n=1:N
  43. y = y +sin(n*pi*x)*forcing_coeffs(n)*integral(@(s)exp(diff*(n^2)*(pi^2)*(s-t(a))),0,t(a));
  44. end
  45. %Row 'a' of solution matrix is equal to the y array. Absolute
  46. %value to eliminate error causing y to be slightly less than 0 when it
  47. %should just be 0.
  48. soln(a,:) = abs(y);
  49. end
  50.  
  51. %To find the 'initial' condition for the time period when the forcing is
  52. %off, we need a functional representation of the temperature at the moment
  53. %when the forcing is turned off. If you are on MATLAB then you will need
  54. %the Symbolic Toolkit. SciLab has limited symbolic abilities so I do not
  55. %know if this will work for SciLab.
  56. syms p TXSWITCHOFF(p)
  57. TXSWITCHOFF(p) = 0;
  58. for n=1:N
  59. TXSWITCHOFF(p) = TXSWITCHOFF(p) + sin(n*pi*p)*forcing_coeffs(n)*integral(@(s)exp(diff*(n^2)*(pi^2)*(s-switchoff)),0,switchoff);
  60. end
  61.  
  62. %Now we use this to compute the Fourier coefficients for the 'initial' conditions.
  63. phi=matlabFunction(TXSWITCHOFF(p));
  64. for n=1:N
  65. phi_coeffs(n) = 2*integral(@(z)(phi(z).*sin(n*pi*z)),0,1);
  66. end
  67.  
  68. %Solution for the temperature after the source has been turned off. We use
  69. %the solution for the temperature at the instant of the switchoff as the
  70. %initial condition, so we have to make the transformation T >> (T-switchoff).
  71. for a=(FPS*switchoff+1):1:length(t)
  72. y=zeros(1,length(x));
  73. for n=1:N
  74. y=y+phi_coeffs(n)*exp(-1*diff*(n^2)*(pi^2)*(t(a)-switchoff))*sin(n*pi*x);
  75. end
  76. soln(a,:)=abs(y);
  77. end
  78. %Uncomment the next two lines if you want to store the solution array.
  79. % filename='forcing_solution1.mat';
  80. % save(filename, 'soln')
  81.  
  82. %Uncomment the following block to generate the frames to be used in the
  83. %animation.
  84.  
  85. nImages = simtime*FPS;
  86. fig=figure;
  87. FF=zeros(1,length(Y));
  88. for idx = 1:nImages
  89. clf
  90. hold on
  91. plot(x,soln(idx,:))
  92. plot(FF,Y,'white')
  93. hold off
  94. elapsed = (idx/framenum)*simtime;
  95. if idx < switchoff*FPS
  96. title(['Elapsed time: ' num2str(elapsed-0.02, '%.2f') ' seconds. Forcing is ON.'])
  97. else
  98. title(['Elapsed time: ' num2str(elapsed-0.02, '%.2f') ' seconds. Forcing is OFF.'])
  99. end
  100. frame = getframe(fig);
  101. im{idx} = frame2im(frame);
  102. end
  103. close;
  104.  
  105. %Uncomment the next two lines if you wish to save a backup of the images you
  106. %that were generated by the last section.
  107. % filename='ForcingSpike1FramesBackup.mat';
  108. % save(filename,im);
  109.  
  110. %Uncomment the following block to save a gif animation of the solution.
  111.  
  112. % filename='Forcing_Spike1.gif';
  113. % %Set this to 1 to include every frame, 2 to include every other frame, 4 to
  114. % %include every fourth frame, etc.
  115. % frameskip=1;
  116. % %Animation length in seconds, up to simtime.
  117. % animation_length=10;
  118. % for idx = 1:nImages
  119. % map = im{idx};
  120. % A = rgb2gray(im{idx});
  121. % if idx == 1
  122. % imwrite(A,filename,'gif','LoopCount',Inf,'DelayTime',1.5);
  123. % elseif (idx>1) && (idx<=animation_length*FPS) && (mod(idx,frameskip)==0);
  124. % imwrite(A,filename,'gif','WriteMode','append','DelayTime',(1/FPS)*frameskip);
  125. % elseif idx==animation_length*FPS+1
  126. % imwrite(A,filename,'gif','WriteMode','append','DelayTime',1.5);
  127. % end
  128. % end
  129.  
  130. %Uncomment the following code to produce a 3D mesh plot of the solution.
  131. % mesh(x,t,soln)
  132. % xlabel('x');
  133. % ylabel('t');
  134. % zlabel('T(x,t) in Celsius');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement