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);
Vh u,v,f=1,un=1,vh,uh,un1,GradF;

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;

real[int] grad=dE(un);
real normGradF= sqrt(grad’*grad); //norm of the the objective function E(u)

//cout << "Gradient values: " << grad << endl; //print gradient values

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

cout<<“Iteration:”<<iter<<“Gradeint norm:”<<normGradF<<endl;
break; //exit the loop
}else {
problem gradient(un1,vh)=int2d(Th)(un1vh)
-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
gradient;
un1=un; //update the function
}

}

//cout<<“GradF:”<<GradF<<endl;

//plot(un1, value=true, fill=true, wait=true, cmm=“gradient descent solution”);//plot the steepest descent solution
plot(u, wait=true, fill=true,value=true, cmm=“solution of v”); //plot the heat equation