27
Finite Element Method 2 Dimensional Laplace Problem Shourya Umang 12678

shourya-umang
• Category

## Documents

• view

40

2

description

2d Laplace problem FEM solution

### Transcript of Finite Element Method

Finite Element Method

2 Dimensional Laplace Problem

Shourya Umang

12678

Problem 1: Γ3

a11=10; Γ1= Γ2=Γ3= Γ4: u=0

a12=a21=0; Γ4 Γ2

a22=1;

r=10; Γ1

Problem 2: Γ1: u=0

Γ2: qn=0

Γ3: qn=10

Γ4: qn=0

a=2

b=2

Problem 1:

a) N=2

b) N=4

c) N=8

d) N=100 ~ Exact Solution

Energy :

0

1

2

3

4

5

6

N=2 N=4 N=8 N=100~ Exact

Energy

Series 1

Problem 2:

a) N=4

b) N=8

c) N=100 ~ Exact

Energy:

800

820

840

860

880

900

920

940

960

980

1000

N=4 N=8 N=100 ~ Exact

Energy Normal Panel

Energy Cracked Panel

Appendix : MATLAB Code for solution

data2d

elem=zeros(2*Nx*Ny,9);

for k=1:(2*Nx*Ny)

if mod(k,2)~=0

elem(k,1)=floor((k+1)/2)+floor(k/(2*Nx));

elem(k,2)=elem(k,1)+1;

elem(k,3)=elem(k,2)+Nx+1;

elem(k,4)=(mod(elem(k,1),(Nx+1))*h)-1;

elem(k,5)=floor(elem(k,1)/(Nx+1))*d;

elem(k,6)=elem(k,4)+h;

elem(k,7)=elem(k,5);

elem(k,8)=elem(k,6);

elem(k,9)=elem(k,7)+d;

else

elem(k,3)=elem(k-1,1);

elem(k,1)=elem(k,3)+Nx+2;

elem(k,2)=elem(k,1)-1;

elem(k,8)=mod(elem(k,3),(Nx+1))*h-1;

elem(k,9)=floor(elem(k,3)/(Nx+1))*d;

elem(k,6)=elem(k,8);

elem(k,7)=elem(k,9)+d;

elem(k,4)=elem(k,6)+h;

elem(k,5)=elem(k,7);

end

end

K=zeros((Nx+1)*(Ny+1));

F=zeros((Nx+1)*(Ny+1),1);

alphas=zeros((Nx+1)*(Ny+1),1);

localk=[a(1,1)*d/(2*h) -a(1,1)*d/(2*h) 0; -a(1,1)*d/(2*h) (a(1,1)*d/(2*h))+(a(2,2)*h/(2*d)) -a(2,2)*h/(2*d); 0 -a(2,2)*h/(2*d) a(2,2)*h/(2*d)];

f=[1/6;1/6;1/6];

f=f.*(h*d*r);

for k=1:(2*Nx*Ny)

for i=1:3

for j=1:3

K(elem(k,i),elem(k,j))=K(elem(k,i),elem(k,j))+localk(i,j);

end

F(elem(k,i))=F(elem(k,i))+f(i);

end

end

for i=1:4

if gamma(i,2)==2

if i==1

for j=1:Nx+1

if j==1 | j==Nx+1

F(j)=F(j)+(gamma(i,1)*h/2);

else

F(j)=F(j)+(gamma(i,1)*h);

end

end

elseif i==2

for j=1:Ny+1

if j==1 | j==Ny+1

F((Nx+1)*j)=F((Nx+1)*j)+(gamma(i,1)*d/2);

else

F((Nx+1)*j)=F((Nx+1)*j)+(gamma(i,1)*d);

end

end

elseif i==3

for j=1:Nx+1

if j==1 | j==Nx+1

F(((Nx+1)*(Ny+1))-Nx-1+j)=F(((Nx+1)*(Ny+1))-Nx-1+j)+(gamma(i,1)*h/2);

else

F(((Nx+1)*(Ny+1))-Nx-1+j)=F(((Nx+1)*(Ny+1))-Nx-1+j)+(gamma(i,1)*h);

end

end

elseif i==4

for j=1:Ny+1

if j==1 | j==Ny+1

F(((j-1)*(Nx+1))+1)=F(((j-1)*(Nx+1))+1)+(gamma(i,1)*d/2);

else

F(((j-1)*(Nx+1))+1)=F(((j-1)*(Nx+1))+1)+(gamma(i,1)*d);

end

end

end

end

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i=1:4

if gamma(i,2)==1

if i==1

for j=1:Nx+1

F(j)=gamma(i,1);

for l=1:(Nx+1)*(Ny+1)

if l~=j

F(l)=F(l)-(K(l,j)*gamma(i,1));

end

K(l,j)=0;

K(j,l)=0;

end

K(j,j)=1;

end

elseif i==2

for j=1:Ny+1

F((Nx+1)*j)=gamma(i,1);

for l=1:(Nx+1)*(Ny+1)

if l~=(Nx+1)*j

F(l)=F(l)-(K(l,(Nx+1)*j)*gamma(i,1));

end

K(l,(Nx+1)*j)=0;

K((Nx+1)*j,l)=0;

end

K((Nx+1)*j,(Nx+1)*j)=1;

end

elseif i==3

for j=1:Nx+1

F(((Nx+1)*(Ny+1))-Nx-1+j)=gamma(i,1);

for l=1:(Nx+1)*(Ny+1)

if l~=((Nx+1)*(Ny+1))-Nx-1+j

F(l)=F(l)-(K(l,((Nx+1)*(Ny+1))-Nx-1+j)*gamma(i,1));

end

K(l,((Nx+1)*(Ny+1))-Nx-1+j)=0;

K(((Nx+1)*(Ny+1))-Nx-1+j,l)=0;

end

K(((Nx+1)*(Ny+1))-Nx-1+j,((Nx+1)*(Ny+1))-Nx-1+j)=1;

end

elseif i==4

for j=1:Ny+1

F(((j-1)*(Nx+1))+1)=gamma(i,1);

for l=1:(Nx+1)*(Ny+1)

if l~=((j-1)*(Nx+1))+1

F(l)=F(l)-(K(l,((j-1)*(Nx+1))+1)*gamma(i,1));

end

K(l,((j-1)*(Nx+1))+1)=0;

K(((j-1)*(Nx+1))+1,l)=0;

end

K(((j-1)*(Nx+1))+1,((j-1)*(Nx+1))+1)=1;

end

end

end

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

alphas=K\F;

x=0;

y=0;

xnod=zeros(1,2);

k=1;

Z=zeros(1);

for i=1:Ny+1

for j=1:Nx+1

xnod(((i-1)*(Nx+1))+j,1)=x;

xnod(((i-1)*(Nx+1))+j,2)=y;

Z(j,i)=alphas(k);

k=k+1;

x=x+h;

end

x=0;

y=y+d;

end

x=0:h:h*Nx;

y=0:d:d*Ny;

[X,Y]=meshgrid(x,y);

k=1;

for i=1:Nx+1

for j=1:Ny+1

Z(i,j)=alphas(k);

k=k+1;

end

end

figure

surf(X,Y,Z);

title('Normal Panel');

xlabel('x');

ylabel('y');

legend('u');

xx=zeros(1);

yy=zeros(1);

qx=zeros(1);

qy=zeros(1);

for i=1:Nx

for j=1:Ny+1

xx(j,i)=(X(j,i)+X(j,i+1))/2;

qx(j,i)=(Z(j,i+1)-Z(j,i))/(X(j,i+1)-X(j,i));

yy(j,i)=Y(j,i);

end

end

qx=qx.*a(1,1);

figure

surf(xx,yy,qx);

title('Normal Panel');

xlabel('x');

ylabel('y');

legend('q_x');

xx=zeros(1);

yy=zeros(1);

for i=1:Ny

for j=1:Nx+1

xx(i,j)=X(i,j);

yy(i,j)=(Y(i,j)+Y(i+1,j))/2;

qy(i,j)=(Z(i+1,j)-Z(i,j))/(Y(i+1,j)-Y(i,j));

end

end

qy=qy.*a(2,2);

figure

surf(xx,yy,qy);

title('Normal Panel');

xlabel('x');

ylabel('y');

legend('q_y');

Energy=0.5*alphas'*K*alphas

%%%%%%%%%%%%%%%%%

%% CRACK %%'

%%%%%%%%%%%%%%%%%

i=1;

for k=((Nx*Ny)+Nx+1):((Nx*Ny)+(2*Nx))

if mod(k,2)~=0

elem(k,1)=elem(k-2,2);

elem(k,2)=((Nx+1)*(Ny+1))+i;

i=i+1;

else

elem(k,3)=elem(k-1,1);

end

end

K=zeros(((Nx+1)*(Ny+1))+(floor(Nx/2)));

F=zeros(((Nx+1)*(Ny+1))+(floor(Nx/2)),1);

alphas=zeros(((Nx+1)*(Ny+1))+(floor(Nx/2)),1);

localk=[a(1,1)*d/(2*h) -a(1,1)*d/(2*h) 0; -a(1,1)*d/(2*h) (a(1,1)*d/(2*h))+(a(2,2)*h/(2*d)) -a(2,2)*h/(2*d); 0 -a(2,2)*h/(2*d) a(2,2)*h/(2*d)];

f=[1/6;1/6;1/6];

f=f.*(h*d*r);

for k=1:(2*Nx*Ny)

for i=1:3

for j=1:3

K(elem(k,i),elem(k,j))=K(elem(k,i),elem(k,j))+localk(i,j);

end

F(elem(k,i))=F(elem(k,i))+f(i);

end

end

for i=1:4

if gamma(i,2)==2

if i==1

for j=1:Nx+1

if j==1 | j==Nx+1

F(j)=F(j)+(gamma(i,1)*h/2);

else

F(j)=F(j)+(gamma(i,1)*h);

end

end

elseif i==2

for j=1:Ny+1

if j==1 | j==Ny+1

F((Nx+1)*j)=F((Nx+1)*j)+(gamma(i,1)*d/2);

else

F((Nx+1)*j)=F((Nx+1)*j)+(gamma(i,1)*d);

end

end

F(((Nx+1)*(Ny+1))+floor(Nx/2))=F(((Nx+1)*(Ny+1))+floor(Nx/2))+(gamma(i,1)*d/2);

F((Nx+1)*((Ny/2)+1))=F((Nx+1)*((Ny/2)+1))-(gamma(i,1)*d/2);

elseif i==3

for j=1:Nx+1

if j==1 | j==Nx+1

F(((Nx+1)*(Ny+1))-Nx-1+j)=F(((Nx+1)*(Ny+1))-Nx-1+j)+(gamma(i,1)*h/2);

else

F(((Nx+1)*(Ny+1))-Nx-1+j)=F(((Nx+1)*(Ny+1))-Nx-1+j)+(gamma(i,1)*h);

end

end

elseif i==4

for j=1:Ny+1

if j==1 | j==Ny+1

F(((j-1)*(Nx+1))+1)=F(((j-1)*(Nx+1))+1)+(gamma(i,1)*d/2);

else

F(((j-1)*(Nx+1))+1)=F(((j-1)*(Nx+1))+1)+(gamma(i,1)*d);

end

end

end

end

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i=1:4

if gamma(i,2)==1

if i==1

for j=1:Nx+1

F(j)=gamma(i,1);

for l=1:(Nx+1)*(Ny+1)

if l~=j

F(l)=F(l)-(K(l,j)*gamma(i,1));

end

K(l,j)=0;

K(j,l)=0;

end

K(j,j)=1;

end

elseif i==2

for j=1:Ny+1

F((Nx+1)*j)=gamma(i,1);

for l=1:(Nx+1)*(Ny+1)

if l~=(Nx+1)*j

F(l)=F(l)-(K(l,(Nx+1)*j)*gamma(i,1));

end

K(l,(Nx+1)*j)=0;

K((Nx+1)*j,l)=0;

end

K((Nx+1)*j,(Nx+1)*j)=1;

end

F(((Nx+1)*(Ny+1))+floor(Nx/2))=gamma(i,1);

for l=1:((Nx+1)*(Ny+1))+floor(Nx/2)

if l~=((Nx+1)*(Ny+1))+floor(Nx/2)

F(l)=F(l)-(K(l,((Nx+1)*(Ny+1))+floor(Nx/2))*gamma(i,1));

end

K(l,((Nx+1)*(Ny+1))+floor(Nx/2))=0;

K(((Nx+1)*(Ny+1))+floor(Nx/2),l)=0;

end

K(((Nx+1)*(Ny+1))+floor(Nx/2),((Nx+1)*(Ny+1))+floor(Nx/2))=1;

elseif i==3

for j=1:Nx+1

F(((Nx+1)*(Ny+1))-Nx-1+j)=gamma(i,1);

for l=1:(Nx+1)*(Ny+1)

if l~=((Nx+1)*(Ny+1))-Nx-1+j

F(l)=F(l)-(K(l,((Nx+1)*(Ny+1))-Nx-1+j)*gamma(i,1));

end

K(l,((Nx+1)*(Ny+1))-Nx-1+j)=0;

K(((Nx+1)*(Ny+1))-Nx-1+j,l)=0;

end

K(((Nx+1)*(Ny+1))-Nx-1+j,((Nx+1)*(Ny+1))-Nx-1+j)=1;

end

elseif i==4

for j=1:Ny+1

F(((j-1)*(Nx+1))+1)=gamma(i,1);

for l=1:(Nx+1)*(Ny+1)

if l~=((j-1)*(Nx+1))+1

F(l)=F(l)-(K(l,((j-1)*(Nx+1))+1)*gamma(i,1));

end

K(l,((j-1)*(Nx+1))+1)=0;

K(((j-1)*(Nx+1))+1,l)=0;

end

K(((j-1)*(Nx+1))+1,((j-1)*(Nx+1))+1)=1;

end

end

end

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

alphas=K\F;

x=0;

y=0;

xnod=zeros(1,2);

k=1;

Z=zeros(1);

for i=1:Ny+1

for j=1:Nx+1

xnod(((i-1)*(Nx+1))+j,1)=x;

xnod(((i-1)*(Nx+1))+j,2)=y;

Z(j,i)=alphas(k);

k=k+1;

x=x+h;

end

x=0;

y=y+d;

end

x=0:h:h*Nx;

k=1;

y0=0;

while k<=(Ny+2)

y(k)=y0;

if k==((Ny/2)+1)

k=k+1;

y(k)=y(k-1);

end

k=k+1;

y0=y0+d;

end

[X,Y]=meshgrid(x,y);

k=1;

for i=1:Ny+2

if i==((Ny/2)+2)

k=k-(Nx+1);

end

for j=1:Nx+1

Z(i,j)=alphas(k);

k=k+1;

end

end

for k=1:(Nx/2)

Z(((Ny/2)+2),mod((((Nx+1)*(Ny/2))+(Nx/2)+1),(Ny+1))+k)=alphas(((Nx+1)*(Ny+1))+k);

end

Energy_crack=0.5*alphas'*K*alphas

%'

figure

surf(X,Y,Z);

title('Cracked Panel');

xlabel('x');

ylabel('y');

legend('u');

xx=zeros(1);

yy=zeros(1);

qx=zeros(1);

qy=zeros(1);

for i=1:Nx

for j=1:Ny+2

xx(j,i)=(X(j,i)+X(j,i+1))/2;

qx(j,i)=(Z(j,i+1)-Z(j,i))/(X(j,i+1)-X(j,i));

yy(j,i)=Y(j,i);

end

end

qx=qx.*a(1,1);

figure

surf(xx,yy,qx);

title('Cracked Panel');

xlabel('x');

ylabel('y');

legend('q_x');

xx=zeros(1);

yy=zeros(1);

for i=1:(Ny/2)+1

for j=1:Nx+1

xx(i,j)=X(i,j);

yy(i,j)=(Y(i,j)+Y(i+1,j))/2;

qy(i,j)=(Z(i+1,j)-Z(i,j))/(Y(i+1,j)-Y(i,j));

end

end

for i=(Ny/2)+2:Ny+1

for j=1:Nx+1

xx(i,j)=X(i,j);

yy(i,j)=(Y(i,j)+Y(i+1,j))/2;

qy(i,j)=(Z(i+1,j)-Z(i,j))/(Y(i+1,j)-Y(i,j));

end

end

qy=qy.*a(2,2);

figure

surf(xx,yy,qy);

title('Cracked Panel');

xlabel('x');

ylabel('y');

legend('q_y');