Sei sulla pagina 1di 4

# clc;

clear;
close all;
warning off;
feature('UseGenericOpenGL',1);

NX=81; NY=61;
dx=1e-3; dy=1e-3;
NLINE_V=15; NLINE_E=100; N_FORCE=2;

EMPTY=pi*exp(1);
eps0=8.85418533673e-12; %eps0=1;
V=zeros(NX,NY);
V0=EMPTY*ones(NX,NY);
epsr=ones(NX,NY);
A=sparse(zeros((NX-2)*(NY-2),(NX-2)*(NY-2)));
B=sparse(zeros((NX-2)*(NY-2),1));

% Condizioni al Contorno %
V0(1:2,:)=0;
V0(NX-1:NX,:)=0;
V0(:,1:2)=0;
V0(:,NY-1:NY)=0;

% % Condizioni al Contorno %
% V0(1:2,:)=ones(2,1)*linspace(-1,1,NY);
% V0(NX-1:NX,:)=ones(2,1)*linspace(-1,1,NY);
% V0(:,1:2)=-1;
% V0(:,NY-1:NY)=1;

% Superfici equipotenziale %

Volt=1;
V0(26:56,33:34)=Volt/2;
V0(26:56,28:29)=-Volt/2;

% V0(26:56,33:39)=1;
% V0(26:56,23:29)=-1;

epsr0=20;
epsr(26:55,29:32)=epsr0;

Path_X=[15 65 65 15];
Path_Y=[30 30 50 50];

eps=eps0*epsr;
for i=2:NX-1,
for j=2:NY-1,
if not(epsr(i,j)==epsr(i-1,j)),
eps(i,j)=.5*eps0*(epsr(i,j)+epsr(i-1,j));
elseif not(epsr(i,j)==epsr(i,j-1))
eps(i,j)=.5*eps0*(epsr(i,j)+epsr(i,j-1));
elseif not(epsr(i,j)==epsr(i-1,j-1))
eps(i,j)=.5*eps0*(epsr(i,j)+epsr(i-1,j-1));
else
eps(i,j)=eps0*epsr(i,j);
end
end
end

## % Riempimento della matrice del sistema lineare A*X=B %

V=V0;
for i=2:NX-1,
for j=2:NY-1,
if V0(i,j)==EMPTY
A(i-1+(j-2)*(NX-2),i-1+(j-2)*(NX-2))=-8*(dx^2+dy^2)*eps(i,j);
if i==2,
B(i-1+(j-2)*(NX-2),1)=-dy^2*(eps(i-1,j)-eps(i+1,j)
+4*eps(i,j))*V0(1,j);
else
A(i-1+(j-2)*(NX-2),i-2+(j-2)*(NX-2))=dy^2*(eps(i-1,j)-eps(i+1,j)
+4*eps(i,j));
end
if i==NX-1,
B(i-1+(j-2)*(NX-2),1)=B(i-1+(j-2)*(NX-2),1)-dy^2*(eps(i+1,j)-eps(i-
1,j)+4*eps(i,j))*V0(NX,j);
else
A(i-1+(j-2)*(NX-2),i+(j-2)*(NX-2))=dy^2*(eps(i+1,j)-eps(i-1,j)
+4*eps(i,j));
end
if j==2,
B(i-1+(j-2)*(NX-2),1)=B(i-1+(j-2)*(NX-2),1)-dx^2*(eps(i,j-1)-
eps(i,j+1)+4*eps(i,j))*V0(i,1);
else
A(i-1+(j-2)*(NX-2),i-1+(j-3)*(NX-2))=dx^2*(eps(i,j-1)-
eps(i,j+1)+4*eps(i,j));
end
if j==NY-1
B(i-1+(j-2)*(NX-2),1)=B(i-1+(j-2)*(NX-2),1)-dx^2*(eps(i,j+1)-
eps(i,j-1)+4*eps(i,j))*V0(i,NY);
else
A(i-1+(j-2)*(NX-2),i-1+(j-1)*(NX-2))=dx^2*(eps(i,j+1)-eps(i,j-
1)+4*eps(i,j));
end
else
A(i-1+(j-2)*(NX-2),i-1+(j-2)*(NX-2))=1;
B(i-1+(j-2)*(NX-2),1)=V0(i,j);
end
end
end

X=A\B;

## % Le incognite nel vettore X vengono rimesse in forma matriciale %

for i=2:NX-1,
for j=2:NY-1,
V(i,j)=X(i-1+(j-2)*(NX-2));
end
end

## % Calcolo del gradiente di V, ovvero del campo elettrico %

E=sqrt(EX.^2+EY.^2);
[X,Y]=ndgrid(0:dx:(NX-1)*dx,0:dy:(NY-1)*dy);
[Xf,Yf]=meshgrid(0:dx:(NX-1)*dx,0:dy:(NY-1)*dy);

figure;
set(gcf,'renderer','painters');
[c,h]=contourf(X,Y,V,linspace(min(min(V)),max(max(V)),NLINE_V));
set(h,'LineStyle',':');
colormap hot;
caxis([-1e7 -1e6]);
hold on;
[c,h]=contour(X,Y,eps>1,[1 1]);
set(h,'Edgecolor','g','LineStyle',':','LineWidth',3);
streamslice(Xf,Yf,EX',EY',N_FORCE);

h=patch(dx*Path_X,dy*Path_Y,'w');
set(h,'Facecolor','none','LineStyle',':','EdgeColor','y','LineWidth',3);

## % Le celle per cui si è assegnato il potenziale vengono evidenziate %

for i=2:NX,
for j=2:NY,
if not(V0(i,j)==EMPTY) & not(V0(i-1,j)==EMPTY) & not(V0(i,j-1)==EMPTY) &
not(V0(i-1,j-1)==EMPTY),
patch([X(i,j) X(i,j)-dx X(i,j)-dx X(i,j)],[Y(i,j) Y(i,j) Y(i,j)-dy
Y(i,j)-dy],'w');
elseif not(V0(i,j)==EMPTY) & not(V0(i-1,j)==EMPTY) & V0(i,j+1)==EMPTY,
patch([X(i,j) X(i,j)-dx X(i,j)-dx X(i,j)],[Y(i,j)+.05*dy Y(i,j)+.05*dy
Y(i,j)-.05*dy Y(i,j)-.05*dy],'k');
elseif not(V0(i,j)==EMPTY) & not(V0(i,j-1)==EMPTY) & V0(i+1,j)==EMPTY,
patch([X(i,j)+.05*dx X(i,j)-.05*dx X(i,j)-.05*dx X(i,j)+.05*dx],[Y(i,j)
Y(i,j) Y(i,j)-dy Y(i,j)-dy],'k');
end
end
end
grid off;
axis equal;
xlabel('X (m)');
ylabel('Y (m)');
title('Voltage (V)');

figure;
set(gcf,'renderer','painters');
colorbar;
[c,h]=contourf(X,Y,E,linspace(0,max(max(E)),NLINE_E));
set(h,'LineStyle','none');
colormap hot;
colorbar;
hold on;
streamslice(Xf,Yf,EX',EY',N_FORCE);
[c,h]=contour(X,Y,eps>1,[1 1]);
set(h,'Edgecolor','g','LineStyle',':','LineWidth',3);

h=patch(dx*Path_X,dy*Path_Y,'w');
set(h,'Facecolor','none','LineStyle',':','EdgeColor','y','LineWidth',3);

for i=2:NX,
for j=2:NY,
if not(V0(i,j)==EMPTY) & not(V0(i-1,j)==EMPTY) & not(V0(i,j-1)==EMPTY) &
not(V0(i-1,j-1)==EMPTY),
patch([X(i,j) X(i,j)-dx X(i,j)-dx X(i,j)],[Y(i,j) Y(i,j) Y(i,j)-dy
Y(i,j)-dy],'w');
elseif not(V0(i,j)==EMPTY) & not(V0(i-1,j)==EMPTY) & V0(i,j+1)==EMPTY,
patch([X(i,j) X(i,j)-dx X(i,j)-dx X(i,j)],[Y(i,j)+.05*dy Y(i,j)+.05*dy
Y(i,j)-.05*dy Y(i,j)-.05*dy],'k');
elseif not(V0(i,j)==EMPTY) & not(V0(i,j-1)==EMPTY) & V0(i+1,j)==EMPTY,
patch([X(i,j)+.05*dx X(i,j)-.05*dx X(i,j)-.05*dx X(i,j)+.05*dx],[Y(i,j)
Y(i,j) Y(i,j)-dy Y(i,j)-dy],'k');
end
end
end
grid off;
axis equal;
xlabel('X (m)');
ylabel('Y (m)');
title('|E-field| (V/m)');

## % Calcolo della carica accumulata %

Q=-
sum(eps(Path_X(1):Path_X(2),Path_Y(1):Path_Y(2)).*EY(Path_X(1):Path_X(2),Path_Y(1):
Path_Y(2))*dx)+...
sum(eps(Path_X(3):-1:Path_X(4),Path_Y(3):-1:Path_Y(4)).*EY(Path_X(3):-
1:Path_X(4),Path_Y(3):-1:Path_Y(4))*dx)+...
sum(eps(Path_X(2):Path_X(3),Path_Y(2):Path_Y(3)).*EX(Path_X(2):Path_X(3),Path_Y
(2):Path_Y(3))*dy)+...
-sum(eps(Path_X(4):-1:Path_X(1),Path_Y(4):-1:Path_Y(1)).*EX(Path_X(4):-
1:Path_X(1),Path_Y(4):-1:Path_Y(1))*dy);

## % Valore teorico di capacità %

C0=eps0*epsr0*dx*30/(dy*4)

## % Valore numerico di capacità %

C=Q/Volt

% Errore percentuale %
Errore_percentuale=100*(C-C0)/C0