# Gradient descent for heat equation

**URL:** <https://community.freefem.org/t/gradient-descent-for-heat-equation/3443>\
**Category:** General Discussion\
**Created:** [August 14, 2024, 2:23pm UTC](https://community.freefem.org/t/gradient-descent-for-heat-equation/3443 "2024-08-14T14:23:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sami](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@Sami](https://community.freefem.org/u/Sami)\
**Post date:** [August 14, 2024, 2:23pm UTC](https://community.freefem.org/t/gradient-descent-for-heat-equation/3443/1 "2024-08-14T14:23:29Z")

</div>

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’’.

 ![WhatsApp Image 2024-08-14 at 09.56.34](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/6/61a01f7c3432c6b4e7366192aca56237b3cf18c2.jpeg)  
//  
int M=10; //number of element  
func uex= sin(pi_x)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(pi_x)_sin(pi_y);

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)(un1_vh)  
-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
