Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. close all; clear; clc;
  2. alpha = 1
  3. N = 100;
  4. Lx = 1;
  5. dx = Lx/(N-1);
  6. x = 0:dx:Lx;
  7. M = 1000;
  8. tf = 1;
  9. dt = tf/(M-1);
  10. t = 0:dt:tf;
  11. lambda = alpha*dt/(dx^2);
  12. f = @(x) sin(pi*x/Lx);
  13. fanal = @(x,t) sin(pi*x/Lx)*(exp(1))^(-[pi^2]*alpha*t/Lx);
  14.  
  15. u = zeros(N,M);
  16. u_anal = zeros(N,M);
  17.  
  18. u(:,1) = f(x);
  19. u_anal(:,1)= fanal(x,0);
  20.  
  21. for k= 1:M-1
  22. for i= 2:N-1
  23. u(i,k+1) = (1-2*lambda)*u(i,k) + lambda*[(u(i-1,k))+ u(i+1,k)];
  24. u_anal(i,k+1)=fanal(x(i),t(k));
  25. end
  26. end
  27.  
  28.  
  29. for i = 1:6
  30. figure
  31. plot(x,u_anal(:,i),'-r',x,u(:,i),'--b');
  32. a = ylabel('Temperature');
  33. set(a,'Fontsize',14);
  34. a = xlabel('x');
  35. set(a,'Fontsize',14);
  36. a=title(['Temperature distribution at t = ' num2str(i-1)])
  37. legend('Analytic distribution','Estimated distribution')
  38. set(a,'Fontsize',16);
  39. xlim([0 1]);
  40. grid;
  41. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement