% OCW Universidad de Cantabria
% Cálculo Simbólico y Numérico en Ecuaciones Diferenciales
% © Cursos 2001-2014,  M. Eugenia Pérez, UC
%
function y= galerking7(n)
 % 
syms t  
A=ones(n,n);  
f=ones(n,1);
for i=1:n
   f(i)=double(int(sin(i*pi*t),0,1));
   for j=1:n
      if i==j
         A(i,j)=-double(int((i*pi*cos(i*pi*t))^2*exp(-t-2)/(t+1),0,1)-int((sin(pi*i*t))^2*exp(-t-2)/((t+1)^2),0,1));
               else
         A(i,j)=-double(int(i*pi*cos(i*pi*t)*j*pi*cos(j*pi*t)*exp(-t-2)/(t+1),0,1)-int(sin(i*pi*t)*sin(j*pi*t)*exp(-t-2)/((t+1)^2),0,1));
       end
      end
   end
 % 
matriz_sistema=A
termino_independiente=f
% 
c=A\f;
coeficientes_solucion=c
% 
u=0;
for i=1:n
   u=u+c(i)*sin(pi*i*t);
end
% 
solu_aproximada=vpa(u,4)  
% 
% 
v=dsolve('(t+1)*D2y-(t+2)*Dy+y=exp(t+2)*(t+1)^2','y(0)=0','y(1)=0');
% 
solu_exacta=v % 
% 
dt=0:0.05:1;
du=subs(u,t,dt);
ddu=double(du);
plot(dt,ddu,'--')
% 
hold on  
% 
ezplot(v,[0,1])
% 
hold off
% 
alpha=ones(n,1);
for i=1:n
   alpha(i)=double(int(sin(i*pi*t)*v,0,1)/int((sin(pi*i*t))^2,0,1));
end
coeficientes_fourier=alpha
% 
errorfourier_galerkin=alpha-c

