You are on page 1of 6

clc;

clear all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Main programme to solve the equation
%-d^u/dx^-u-x^2=0 (u(0)=0; du/dx|(x=1)=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

NPE=2; % Nodes per element


NEL=10; % Number of elements
L=1;% Length of the domain
M=mesh1D(L,NPE,NEL);
NDF=1; %Number of degrees of freedom per node.
[CONN,GLX]=mesh1D(L,NPE,NEL); % Get the mesh and
connectivity information;
% GLX has global coordinates
% CONN has the connectivity
NODES=NPE*NEL-(NEL-1); % Total number of Nodes (Global)
%======================================================
=========================
NUSP=2; % Number of nodes where Primary variables are
specified
NQSP=0; %Number of nodes where Secondary variables are
specified
USP=zeros(NODES,1); %Intializing the array of specified
displacelements
QSP=zeros(NODES,1); %Initializing the array of
specified Forces
%======================================================
=========================
GNPV=[1 M(end)];% ARRAY of Global nodes where Primary
variable is specified
GNSV=[NODES]; % ARRAY of Global nodes where Secondary
variable is spefcified
USP(1)=0; USP(M(end))=0;%Value of Primary variables at
speficied nodes
QSP(NODES)=1; % Value of Secondary variable at
speficied nodes.
%======================================================
=========================
KG=zeros(NODES,NODES);% Global stiffness matrix.
FG=zeros(NODES,1); % Global Forge vector
%==============
for Elem=1:NEL % Loop over all elements
for KK=1:NPE % Get local element numbers
ELX(KK)=GLX(CONN(Elem,KK));
end
%==========================
[K,F]=Elementalk(2,NPE,ELX); % Calculate the element
stiffness matrix and distributed force
[KG,FG]=assemble(K,F,NPE,CONN,Elem,KG,FG); % Assemble
them.
end
[KGM,FGM]=boundary(KG,FG,USP,QSP,NUSP,NQSP,GNPV,GNSV,NO
DES); % apply Boundary conditions
GU=inv(KGM)*FGM; % Solve
% Get the actual solution and plot it.
XX=linspace(0,1,10);
for p=1:length(XX)
uactual(p)=(1-
2*cos(1))/sin(1)*sin(XX(p))+2*cos(XX(p))+XX(p)*XX(p)-2;
end
GU
plot(GLX,GU,'r-+');
hold on;
plot(XX,uactual,'b-o');
GU =

0
-0.0095
-0.0189
-0.0276
-0.0351
-0.0407
-0.0434
-0.0420
-0.0353
-0.0218
0
clc;
clear all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Main programme to solve the equation
%-d^u/dx^-u-x^2=0 (u(0)=0; du/dx|(x=1)=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

NPE=3; % Nodes per element


NEL=5; % Number of elements
L=1;% Length of the domain
M=mesh1D(L,NPE,NEL);
NDF=1; %Number of degrees of freedom per node.
[CONN,GLX]=mesh1D(L,NPE,NEL); % Get the mesh and
connectivity information;
% GLX has global coordinates
% CONN has the connectivity
NODES=NPE*NEL-(NEL-1); % Total number of Nodes (Global)
%======================================================
=========================
NUSP=2; % Number of nodes where Primary variables are
specified
NQSP=0; %Number of nodes where Secondary variables are
specified
USP=zeros(NODES,1); %Intializing the array of specified
displacelements
QSP=zeros(NODES,1); %Initializing the array of
specified Forces
%======================================================
=========================
GNPV=[1 M(end)];% ARRAY of Global nodes where Primary
variable is specified
GNSV=[NODES]; % ARRAY of Global nodes where Secondary
variable is spefcified
USP(1)=0; USP(M(end))=0;%Value of Primary variables at
speficied nodes
QSP(NODES)=1; % Value of Secondary variable at
speficied nodes.
%======================================================
=========================
KG=zeros(NODES,NODES);% Global stiffness matrix.
FG=zeros(NODES,1); % Global Forge vector
%==============
for Elem=1:NEL % Loop over all elements
for KK=1:NPE % Get local element numbers
ELX(KK)=GLX(CONN(Elem,KK));
end
%==========================
[K,F]=Elementalk(2,NPE,ELX); % Calculate the element
stiffness matrix and distributed force
[KG,FG]=assemble(K,F,NPE,CONN,Elem,KG,FG); % Assemble
them.
end
[KGM,FGM]=boundary(KG,FG,USP,QSP,NUSP,NQSP,GNPV,GNSV,NO
DES); % apply Boundary conditions
GU=inv(KGM)*FGM; % Solve
% Get the actual solution and plot it.
XX=linspace(0,1,10);
for p=1:length(XX)
uactual(p)=(1-
2*cos(1))/sin(1)*sin(XX(p))+2*cos(XX(p))+XX(p)*XX(p)-2;
end
GU
plot(GLX,GU,'r-+');
hold on;
plot(XX,uactual,'b-o');
GU =

0
-0.0096
-0.0189
-0.0276
-0.0352
-0.0408
-0.0434
-0.0420
-0.0353
-0.0218
0

You might also like