Processing math: 100%

Trinh @ Bath

This is an old revision of the document!


MA20223 Lecture 22

The 1D heat equation with zero Dirichlet conditions

We will complete our determination of the Fourier series solution of the heat equation as presented in the previous lecture. This was the problem of: ut=κuxx,x[0,π],t0u(0,t)=0=u(π,t)u(x,0)=f(x)=1.

u(x,t)1[bnsin(nπxL)].

where bn will be the Fourier sine coefficients of the odd 2π extension of f(x) on [0,π].

Solution of 1D heat equation with zero Dirichlet

  1. % Written for MA20223 Vectors & PDEs
  2. clear % Clear all variables
  3. close all % Close all windows
  4.  
  5. N = 20; % How many Fourier modes to include?
  6.  
  7. % Define an in-line function that takes in three inputs:
  8. % Input 1: n value [scalar]
  9. % Input 2: x value [vector]
  10. % Input 3: t value [scalar]
  11. R = @(n, t, x) -2/(n*pi)*((-1)^n - 1)*exp(-n^2*t)*sin(n*x);
  12.  
  13. % Create a mesh of points between two limits
  14. x0 = pi;
  15. x = linspace(0, x0, 1000);
  16.  
  17. % Create a mesh of points in time
  18. t = linspace(0, 5, 200);
  19.  
  20. figure(1); % Open the figure
  21. plot([0, pi], [1, 1], 'b', 'LineWidth', 2); % Plot the base function
  22. ylim([-0.2,1.2]); % Set the y limits
  23. xlim([0, x0]); % Set the x limits
  24. xlabel('x'); ylabel('u(x,t)');
  25. hold on
  26. for j = 1:length(t)
  27. tj = t(j);
  28. u = 0;
  29. for n = 1:N
  30. u = u + R(n, tj, x);
  31. end
  32. % Plotting commands
  33. if j == 1
  34. p = plot(x, u, 'r');
  35. else
  36. set(p, 'YData', u);
  37. end
  38. drawnow
  39. title(['t = ', num2str(tj)]);
  40. pause(0.1);
  41. if j == 1
  42. pause
  43. end
  44. end