Anda di halaman 1dari 2

%FEM MATLAB code for BVP

% u''+uu'-u=exp(2x) , u(0)+u'(0)=2, u(1)=e


% hacemos: u'(0)=2-u(0)
%solu = u=e^x

function FEM_code_mixed()
clear ;
close all;
clc
n=10;
nn=n+1;
lgth=1;
he=lgth/n;
x=(0:he:lgth);
AC=0.00005;
F=zeros(nn,1);
F(1)=1;
F(nn)=exp(1);
c=1.0;
count=0;
tic
while(c>0)
[F1]=assembly(F,n,he);
c=0.0;
for i=1:nn
if(abs(F(1)-F1(1))>AC)
c=c+1;
break;
end
end
F=F1;
count=count+1;
end
disp('Hence solution: ');

%output for primary and secondary variables


diff=abs(F-exp(x)');

fprintf('No of elements=%d\n',n)
%disp('No of elements=%d\n',n)
disp(' x FEM Exact Error')
disp([x',F,exp(x)',diff])
fprintf('No of iterations=%d\n',count)

%Plot in of primary variable


plot(x,F,'--rs','LineWidth',2)
xlabel('x')
ylabel('u(x)')
title('Solution Plot to given BVP')

toc
end

%Derivation of element matrix and assembly


function[F1]=assembly(F,n,he)
nn=n+1;
K=zeros(nn,nn);
R=zeros(nn,1);
syms x
S=[1-x/he,x/he];
dS=diff(S,x);
lmm=[];
for i=1:n
lmm=[lmm;[i,i+1]];
end

Anda mungkin juga menyukai