Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. clear;
  2.  
  3. %Define what part of the plane to solve the problem in
  4. mini=0;
  5. maxi=1;
  6. side=50;
  7. scale=(maxi-mini)/side;
  8. [X,Y]=meshgrid(linspace(mini,maxi,side));
  9.  
  10. %Define the vector valued function uprim according to pdf
  11. alpha1=0.8;
  12. alpha2=3;
  13. alpha3=1;
  14. epsilon=0;
  15. Beta1=-[0.1;0.1];
  16. Beta2=-[0.1;0.1];
  17. uprim=@(u,gu1,gu2,du1,du2)([
  18. -alpha1*u(1)*u(2)-dot(Beta1,gu1)+epsilon*du1;
  19. alpha2*u(1)*u(2)-alpha3*u(2)-dot(Beta2,gu2)+epsilon*du2]);
  20.  
  21. %Stepsize and time.
  22. h=0.02;
  23. t=0:h:2;
  24.  
  25. %Allocate answer arrays
  26. Y1=zeros(side,side,numel(t));
  27. Y2=zeros(side,side,numel(t));
  28.  
  29. %initialize arrays wiht boundary values
  30. Y1(:,:,1)=ones(size(Y));
  31. Y2(:,:,1)=1-((X-0.5).^2+(Y-0.5).^2);
  32. Y1([1,end],:,:)=0;
  33. Y1(:,[1,end],:)=0;
  34. Y2([1,end],:,:)=0;
  35. Y2(:,[1,end],:)=0;
  36.  
  37. for j=2:numel(t)+1 %Timestepping
  38. [gx1,gy1]=gradient(Y1(:,:,j-1),scale,scale); %Calculate gradient of u1 field
  39. [gx2,gy2]=gradient(Y2(:,:,j-1),scale,scale); %and u2 field
  40. du1=del2(Y1(:,:,j-1),scale,scale); %Calculate laplace of u1
  41. du2=del2(Y2(:,:,j-1),scale,scale);
  42. for i=0:numel(X)-1 %Loop over all squares
  43. i1=mod(i,side)+1; %for indexing vectors
  44. i2=floor(i/side)+1;
  45.  
  46. gu1=[gx1(i1,i2);gy1(i1,i2)]; %Define gradient of u1 field in the point
  47. gu2=[gx2(i1,i2);gy2(i1,i2)]; %Same for u2 field
  48. u=[Y1(i1,i2,j-1);Y2(i1,i2,j-1)]; %Fetch values in the point at the specified time
  49.  
  50. k1=uprim(u,gu1,gu2,du1(i1,i2),du2(i1,i2)); %Calculate next step
  51. k2=uprim(u+h*k1,gu1,gu2,du1(i1,i2),du2(i1,i2)); %Using simple fixed point iteration
  52.  
  53. slope=(k1+k2)/2; %Average them
  54.  
  55. Y1(i1,i2,j)=Y1(i1,i2,j-1)+slope(1)*h; %Add values to the answers
  56. Y2(i1,i2,j)=Y2(i1,i2,j-1)+slope(2)*h;
  57. end
  58. Y1([1,end],:,j)=1; %Set boundaries to 0
  59. Y1(:,[1,end],j)=0;
  60. Y2([1,end],:,j)=0;
  61. Y2(:,[1,end],j)=0;
  62. end
  63. %% Record da movie
  64.  
  65. min1=min(Y1(:));
  66. max1=max(Y1(:));
  67. min2=min(Y1(:));
  68. max2=max(Y2(:));
  69.  
  70. for j=1:numel(t)
  71. subplot(2,1,1)
  72. imagesc(Y1(:,:,j))
  73. colorbar
  74. axis off
  75. subplot(2,1,2)
  76. imagesc(Y2(:,:,j))
  77. colorbar
  78. axis off
  79. M(j)=getframe(figure(1));
  80. end
  81.  
  82. %% PLAY!!!
  83. movie(M,3,10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement