|
@@ -0,0 +1,35 @@
|
|
|
|
|
+clear all; close all; clc;
|
|
|
|
|
+
|
|
|
|
|
+alpha = 0.01;
|
|
|
|
|
+L = 10;
|
|
|
|
|
+Nx = 200;
|
|
|
|
|
+x = linspace(0, L, Nx);
|
|
|
|
|
+dx = x(2) - x(1);
|
|
|
|
|
+dt = 0.4 * dx^2/(2*alpha);
|
|
|
|
|
+tmax = 2000;
|
|
|
|
|
+Nt = floor(tmax/dt);
|
|
|
|
|
+
|
|
|
|
|
+u = sin(pi*x/L);
|
|
|
|
|
+figure;
|
|
|
|
|
+hold on;
|
|
|
|
|
+xlim([0, 10]);
|
|
|
|
|
+ylim([0, 1]);
|
|
|
|
|
+
|
|
|
|
|
+for i=1:Nt
|
|
|
|
|
+ u_old = u;
|
|
|
|
|
+ laplacian_u = 4*del2(u_old, dx);
|
|
|
|
|
+ u = u_old + alpha*dt*laplacian_u;
|
|
|
|
|
+
|
|
|
|
|
+ % Dirichlet
|
|
|
|
|
+ % u(1) = 2; u(end) = 5;
|
|
|
|
|
+
|
|
|
|
|
+ % Neumann
|
|
|
|
|
+ u(2) = u(1); u(end-1) = u(end);
|
|
|
|
|
+
|
|
|
|
|
+ if mod(i,100) == 0
|
|
|
|
|
+ cla;
|
|
|
|
|
+ plot(x, u, 'LineWidth', 2);
|
|
|
|
|
+ title(sprintf('t = %.3f', i*dt));
|
|
|
|
|
+ pause(0.001);
|
|
|
|
|
+ end
|
|
|
|
|
+end
|