Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.30 KB | None | 0 0
  1. clear all
  2. close all
  3.  
  4. D = 1/6;
  5. i = 10;  % mesh points
  6. x = linspace(0,1,i);
  7. dx = x(2)-x(1);  % mesh increment in meters
  8. dt = 0.02;  % time step in seconds
  9. n = 200; % number of time steps
  10.  
  11. f_n_minus_1_i = sin(2*pi*x);  % wave function starting condition t=0
  12. % f_n_minus_1_i(end) = 0;
  13. f_n_i = exp(-4*pi^2*D*dt)*sin(2*pi*x);  % wave function starting condition t=1*dt
  14. % f_n_i(end) = 0;
  15. f_n_i_right = [f_n_i(2:end) 0];  % wave function starting condition t=1*dt
  16. f_n_i_left = [0 f_n_i(1:end-1)];  % wave function starting condition t=1*dt
  17.  
  18.  
  19. % figure
  20. % plot(f_n_minus_1_i)
  21. % hold
  22. % plot(f_n_i,'r')
  23. % plot(f_n_i_right,'g')
  24. % plot(f_n_i_left,'y')
  25.  
  26. for l = 2:(50/dt)
  27.        
  28.     f_n_plus_1_i = f_n_minus_1_i +D*2*dt/(dx^2)*(-2*f_n_i + f_n_i_right + f_n_i_left);
  29.     f_n_plus_1_i(1) = 0;  % implementing BC
  30.     f_n_plus_1_i(end) = 0;
  31.     f_n_minus_1_i = f_n_i; %sets f old = f new
  32.     f_n_i = f_n_plus_1_i;
  33.     f_n_i_right = [f_n_plus_1_i(2:end) 0];
  34.     f_n_i_left = [0 f_n_plus_1_i(1:end-1)];
  35.    
  36.     if (l ==0.06/dt)
  37.         exact_sol_006 = exp(-4*pi^2*D*3*dt)*sin(2*pi*x);
  38.         figure
  39.         plot(f_n_i)
  40.         hold
  41.         plot(exact_sol_006,'r');
  42.         LEGEND('computed','exact solution');
  43.         title(['f as a function of x at t = ',num2str(0.06) ' seconds'])
  44.         f_n_i_006 = f_n_i;
  45.  
  46.     end
  47.    
  48.     if (l ==0.1/dt)
  49.         exact_sol_01 = exp(-4*pi^2*D*5*dt)*sin(2*pi*x);
  50.         figure
  51.         plot(f_n_i)
  52.         hold
  53.         plot(exact_sol_01,'r');
  54.         LEGEND('computed','exact solution');
  55.         title(['f as a function of x at t = ',num2str(0.1) ' seconds'])
  56.         f_n_i_01 = f_n_i;
  57.        
  58.     end
  59.    
  60.     if (l ==0.9/dt)
  61.         exact_sol_09 = exp(-4*pi^2*D*45*dt)*sin(2*pi*x);
  62.         figure
  63.         plot(f_n_i)
  64.         hold
  65.         plot(exact_sol_09,'r');
  66.         LEGEND('computed','exact solution');
  67.         title(['f as a function of x at t = ',num2str(0.9) ' seconds'])
  68.         f_n_i_09 = f_n_i;
  69.        
  70.     end
  71.    
  72.     if (l ==50/dt)
  73.         exact_sol_50 = exp(-4*pi^2*D*2500*dt)*sin(2*pi*x);
  74.         figure
  75.         plot(f_n_i)
  76.         hold
  77.         plot(exact_sol_50,'r');
  78.         LEGEND('computed','exact solution');
  79.         title(['f as a function of x at t = ',num2str(50) ' seconds'])
  80.         f_n_i_50 = f_n_i;
  81.        
  82.     end
  83.    
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement