I want to write Matlab code using finite element method in order to solve the above problem but I didn't succeed because am not familiar with that Matlab programming however I have tried to give such code below which it dosn't work ,any help ?
-u''(x)+u(x)=(-4x^2-6) exp(x^2),u(-1)=u(1)=0, x\in ]-1,1[
[
My attempt code :
function [U] = EquaDiff2(n)
%----------------------------------
% d²u/dx² + 6 du/dx + 9 u = x(1-x)
% u(-1) = 0 u(1)= 0
syms x x1 x2 real % déclaration de variables
symboliques
function [Ke, Fe] = MatElt2Nd(x1,x2) % déclaration de la fonction , Fonction pour calculer la
mat. et vect. élementairs
%----------------------------------
x = ]-1:2/n:1['; % modification d’1 borne
d’intégration
K = zeros(n+1 ) ;
F = zeros(n+1,1) ;
for i = 1:n
j = i+1;
t = [i j];
x1 = x(i);
x2 = x(j);
[Ke,Fe] = MatElt2Nd(x1,x2);
K(t,t) = K(t,t) + Ke;
F(t) = F(t) + Fe;
end;
K(1,:) = [];
K(:,1) = [];
F(1) = [];
U = K\F;
U = [0.0;U];
t = 0:0.01:1;
return
%-------------------------------------------
% Calcul de la matrice Ke et du vecteur Fe
%-------------------------------------------
function [Ke,Fe] = MatElt2Nd(x1,x2)
Ke1 = 1/(x2-x1)*[ 1 -1 % les modifications ne
touchent
-1 1 ] ; % essentiellement que les
matrices
Ke2 =(x2-x1)* [ 2 1 % élémentaires
1 2 ] ;
N = [(x-x2)/(x1-x2) (x-x1)/(x2-x1)] % fonctions de forme
Fe =simple( int(N' * (-4*x^2-6)*exp(x^2) , x, x1, x2) ) % vecteur Fe ;
Ke = Ke1 + Ke2 ;
return