function [U,gradU] = vypocti_MHP_reseni_vne_Omega(Uzly,Segmenty,sirkaOmegar,sirkaOmegaJ,vyskaOmegaJ,q,mu0,J,Body)

N = size(Body,2);
n = length(Segmenty);

U = zeros(1,N);
gradU = zeros(2,N);

yMinus = [-(sirkaOmegar+sirkaOmegaJ)/2;0];
yPlus = [(sirkaOmegar+sirkaOmegaJ)/2;0];

for i=1:N
    xi = Body(:,i);

    for j=1:n
       Uzlyj = Uzly(:,Segmenty(:,j));
       yj = sum(Uzlyj,2)/2;
       delkaj = norm(Uzlyj(:,2)-Uzlyj(:,1));
       
       U(i) = U(i) - q(j) * (1/(2*pi)) * log(norm(xi-yj)) * delkaj;       
       gradU(:,i) = gradU(:,i) - q(j) * (1/(2*pi*(xi-yj)'*(xi-yj))) * (xi-yj) *delkaj;
    end
    
    U(i) = U(i) - (mu0*J/(2*pi)) * ( -log(norm(xi-yMinus)) + log(norm(xi-yPlus)) ) * sirkaOmegaJ*vyskaOmegaJ;
    gradU(:,i) = gradU(:,i) - (mu0*J/(2*pi)) * ( -((xi-yMinus))/norm(xi-yMinus)^2 + ((xi-yPlus))/norm(xi-yPlus)^2 ) * sirkaOmegaJ*vyskaOmegaJ;
    
end

        
