Guest User

Untitled

a guest
Apr 25th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. function simulation(Tmax, Xmax)
  2. tic
  3. % Run turtle pattern simulation
  4. % if Xmax is not set we use a default of 40
  5. % if even Tmax is not set we use a default of 1800
  6.  
  7. if nargin < 2, Xmax = 40; end; % Default max x value
  8. if nargin < 1, Tmax = 1800; end; % Defalt max t value
  9.  
  10. % Set up constants for periodic pattern:
  11. dt = 0.1; dx = 1; % Step-sizes
  12. au = 0.02; av = 0.03; % Decay constants
  13. bu = 0.001; bv = 0; % Base expression rates
  14. Du = 0.01; Dv = 0.15; % Diffusion constants
  15.  
  16. % Define the domain:
  17. Nt = floor(Tmax/dt) + 1;
  18. Nx = floor(Xmax/dx) + 1;
  19. T = dt*(0:(Nt-1));
  20. X = dx*(0:(Nx-1));
  21.  
  22. % Set up slightly noisy initial conditions:
  23. noise = 0.005;
  24. lu = au*(1 + noise*(2*rand(Nx +2,Nx +2)-1));
  25. u = ones(Nx,Nx);
  26. v = u;
  27.  
  28. % Reaction rules:
  29. f = @(u,v) lu.*(u.^2 + bu)./(v) - au*u;
  30. g = @(u,v) lu.*u.^2 - av*v + bv;
  31.  
  32. % Display initial state:
  33. figure(1); hax1 = gca(); % create window 1 and draw u
  34. draw(hax1, u);
  35. figure(2); hax2 = gca(); % create window 2 and draw v
  36. draw(hax2, v);
  37.  
  38. % Simulation loop:
  39. for t = 1:Nt -1
  40.  
  41. % Expand u and v to use zero-flux boundary conditions
  42. % Note: this just works as our laplace2d applies the matrices
  43. % in the correct order
  44. ue = zeroFlux(u);
  45. ve = zeroFlux(v);
  46.  
  47. % Calculate interdots (rk)s
  48. ue2 = ue + dt/2 * (f(ue, ve) + Du*laplace2d(ue, dx));
  49. ve2 = ve + dt/2 * (g(ue, ve) + Dv*laplace2d(ve, dx));
  50.  
  51. % Calculate new u and v using the interdots
  52. ue = ue + dt * (f(ue2, ve2) + Du*laplace2d(ue2, dx));
  53. ve = ve + dt * (g(ue2, ve2) + Dv*laplace2d(ve2, dx));
  54.  
  55. % Shrink u and v back to real size
  56. u = ue(1:Nx,1:Nx);
  57. v = ve(1:Nx,1:Nx);
  58.  
  59. % Fixed Boundary Condition
  60. % uBound = 0;
  61. % vBound = 1; % vBound must not be 0
  62. % u = fixedBoundary(u, uBound);
  63. % v = fixedBoundary(v, vBound);
  64.  
  65. % Display:
  66. if mod(t,50) == 0
  67. draw(hax1,u); % This is necessary if we want animation (calls drawnow)
  68. draw(hax2,v); % This is necessary if we want animation (calls drawnow)
  69. end;
  70. end;
  71. toc
  72. end
Add Comment
Please, Sign In to add comment