Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.40 KB | None | 0 0
  1. vSize =12;
  2. hSize = 10;
  3. V = 10;
  4. V_index = 75;
  5.  
  6. U = calculatePotentials(vSize,hSize,V,V_index);
  7. plotPotentials(U,vSize,hSize,V_index);
  8. ----------------------------------------------------------
  9. function U = calculatePotentials(hSize, vSize,U,uIndex)
  10. pCount = hSize*vSize;
  11. if uIndex>pCount
  12.    error('Index of point with potential(%d) is out of range(%d)',uIndex,pCount);
  13. end
  14.  
  15. if uIndex <=vSize || uIndex> vSize*(hSize-1) || mod(uIndex,vSize) == 0 || mod(uIndex,vSize) == 1
  16.     error('Point must be inside area');
  17. end
  18.    
  19. I = eye(vSize);
  20. I1 = diag([0 -ones(1,vSize-2) 0]);
  21. T = diag([1 4*ones(1,vSize-2) 1]) + diag([0 -ones(1,vSize-2)],1)+diag([-ones(1,vSize-2) 0],-1);
  22. C = repmat({T},hSize-2,1);
  23. H = kron(diag([1 zeros(1,hSize-2) 1]),I)+kron(diag([0 ones(1,hSize-2) 0]),T)+kron(diag([0 ones(1,hSize-2)],1),I1)+kron(diag([ones(1,hSize-2) 0],-1),I1);
  24.  
  25. H(uIndex,:) = 0;
  26. H(uIndex,uIndex) = 1;
  27. B = zeros(pCount,1);
  28. B(uIndex) = U;
  29.  
  30. U = gmres(H,B);
  31. end
  32. -----------------------------------------------------------------------------------
  33. function plotPotentials(U,hSize, vSize,V0_index)
  34. close all;
  35.  
  36. u_2d= reshape(U,vSize,hSize);
  37. contour(u_2d,50);
  38. colorbar;
  39. grid on;
  40. hold on;
  41. plot(floor(V0_index/vSize)+1,mod(V0_index,vSize),'k.','MarkerSize',10);
  42. title('Rozklad pola elektrycznego U($x_{i}$,$y_{i}$)','interpreter','latex')
  43. xlabel('i','interpreter','latex');
  44. ylabel('j','interpreter','latex');
  45. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement