# Gradient descent for heat equation

Hello,
I’m a beginner in FreeFem++
I am trying to solve the heat equation but solution of the heat equation is not converging to the solution of the gradient descent. Additionally the gradient of the objective function is zero everywhere. Could you please help to figure out what the problem is? Note! For the mathematical of the heat equation I used ‘‘v’’ as the unknown but in the code I replace ‘‘v’’ by ‘‘u’’.

//
int M=10; //number of element
func uex= sin(pix)sin(piy);
//func f=x
y;
int Maxiter=100; //number of iteration
real mu=0.5; // stepsize
real Tol=1e-6; //tolerance for the stooping criteria
func z=0; //Dirichlet boundary condition (u=0)
//func f=2*pi^(2)sin(pix)sin(piy);

mesh Th=square(M,M); //built the mesh

fespace Vh(Th,P1);

problem Laplace(u, v)=int2d(Th)(dx(u)*dx(v) + dy(u)dy(v))
- int2d(Th) (f
v)
+ on(1,2,3,4, u=z); //Dirichlet boundry condition

//energy functional
func real E(real[int] & u ) {
Vh p;
p=u;
real s= int2d(Th)(0.5*dx(p)*dx(p) + dy(p)*dy(p));
return s;
}

//the gradient of the objective function
func real[int]dE (real[int] uw){
Vh w;
w=uw;
varf up(w,vh)
=int2d(Th)(dx(vh)*dx(w) + dy(w)dy(vh))
-int2d(Th)(f
vh)
+ on(1,2,3,4, w=z)
;
uw=up(0, Vh);
return uw;
}

for(int iter=0;iter<Maxiter; iter++ ){
//solve the heat equation
Laplace;

if(normGradF<Tol){ //check if the norm of the grained is below the tolerance

break; //exit the loop
}else {
-int2d(Th)(un
vh)+int2d(Th)(mu*(dx(un)*dx(vh) + dy(un)*dy(vh)))
+ on(1,2,3,4,un1=z);

// solve the steepest descent problem