Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vSize =12;
- hSize = 10;
- V = 10;
- V_index = 75;
- U = calculatePotentials(vSize,hSize,V,V_index);
- plotPotentials(U,vSize,hSize,V_index);
- ----------------------------------------------------------
- function U = calculatePotentials(hSize, vSize,U,uIndex)
- pCount = hSize*vSize;
- if uIndex>pCount
- error('Index of point with potential(%d) is out of range(%d)',uIndex,pCount);
- end
- if uIndex <=vSize || uIndex> vSize*(hSize-1) || mod(uIndex,vSize) == 0 || mod(uIndex,vSize) == 1
- error('Point must be inside area');
- end
- I = eye(vSize);
- I1 = diag([0 -ones(1,vSize-2) 0]);
- T = diag([1 4*ones(1,vSize-2) 1]) + diag([0 -ones(1,vSize-2)],1)+diag([-ones(1,vSize-2) 0],-1);
- C = repmat({T},hSize-2,1);
- 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);
- H(uIndex,:) = 0;
- H(uIndex,uIndex) = 1;
- B = zeros(pCount,1);
- B(uIndex) = U;
- U = gmres(H,B);
- end
- -----------------------------------------------------------------------------------
- function plotPotentials(U,hSize, vSize,V0_index)
- close all;
- u_2d= reshape(U,vSize,hSize);
- contour(u_2d,50);
- colorbar;
- grid on;
- hold on;
- plot(floor(V0_index/vSize)+1,mod(V0_index,vSize),'k.','MarkerSize',10);
- title('Rozklad pola elektrycznego U($x_{i}$,$y_{i}$)','interpreter','latex')
- xlabel('i','interpreter','latex');
- ylabel('j','interpreter','latex');
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement