# Define the nonlinearity f inside and outside the time loop gave different result

**URL:** https://community.freefem.org/t/define-the-nonlinearity-f-inside-and-outside-the-time-loop-gave-different-result/2602
**Category:** General Discussion
**Created:** [July 13, 2023, 12:57pm UTC](https://community.freefem.org/t/define-the-nonlinearity-f-inside-and-outside-the-time-loop-gave-different-result/2602 "2023-07-13T12:57:03Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![nguyenquynhnga101010](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/nguyenquynhnga101010/32/1800_2.png) [@nguyenquynhnga101010](https://community.freefem.org/u/nguyenquynhnga101010)
#### Post date: [July 13, 2023, 12:57pm UTC](https://community.freefem.org/t/define-the-nonlinearity-f-inside-and-outside-the-time-loop-gave-different-result/2602/1 "2023-07-13T12:57:03Z")

</div>

Hello everyone,  
I am solving a simple heat equation of variable u with the source term f.  
At first, function f only depends on u so I can define f outside the time loops and it gives a good result. But then I want f to depend on time t and I try to define it inside the time loops. I found that even though I haven’t changed the function f, the result I obtained was different from the previous case.  
Does anyone have an idea why I got this difference and how I can avoid it?

Here comes my code:

```auto
real r=0.5, D = 1., nuE = 0.08, muE = 0.05 ,muF = 0.1, b = 10 , K = 200, dt=0.01, Tf=5. ;
real r1 = 1, r2 = 5, c= 0.05, lbda = 500;
border C(z=0, 2*pi){x=10*cos(z); y=10*sin(z);}

// The triangulated domain Th is on the left side of its boundary
mesh Th = buildmesh(C(100));

fespace Vh(Th,P1);
Vh uh,vh,uh0,f;

//Function f outside the time loop
f = r*nuE*b*uh/(b*uh/K - muE - nuE)*(1-exp(-uh))- muF*uh;

func init = 200*(sqrt(x*x+y*y) >= 7);
macro Grad(u)[dx(u),dy(u)]//
problem chaleur(uh,vh) = int2d(Th)(uh*vh/dt + Grad(uh)'*Grad(vh)*D) - int2d(Th, optimize = 0)(uh0*vh/dt + f*vh);

// Time loop
real t = 0;
uh0 = init;
for (int m = 0; m <= Tf/dt; m++){
    // Update
    t = t+dt;
    // Function f inside the time loop
    //f = r*nuE*b*uh/(b*uh/K - muE - nuE)*(1-exp(-uh))- muF*uh;

    // Solve
    chaleur;
    uh0 = uh;

    // Plot
    if (!(m % 10)) {
        plot(uh, cmm="t="+(t-dt)+"[days]", value = true, fill = true, wait=1);
    }
}

```

Thank you very much!

---

<div class="post-metadata">

### Author: ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)
#### Post date: [July 14, 2023, 9:59am UTC](https://community.freefem.org/t/define-the-nonlinearity-f-inside-and-outside-the-time-loop-gave-different-result/2602/2 "2023-07-14T09:59:02Z")

</div>

if you RHS depend on u the problem is no- linear  
and f is not a function so the operator = do interpolation  
if you put the f outside the loop f never change (it is f at time 0)  
over wise in the loop it is f a time t-dt (previous time step).

it is better but you can also make a fixed point also to converge in f by adding a loop to solve the non linear problem

```auto
   for( int iter =0; iter < 10; ++i)
{
    // Function f inside the time loop
    f = r*nuE*b*uh/(b*uh/K - muE - nuE)*(1-exp(-uh))- muF*uh;
   Vh up;
    up[]=u[];: 
    // Solve
    chaleur;
up[] -= u[];
cout << " iter = " <<iter<< " err=" << up[].linfty << endl;
if( up[].linfty < 1e-5) break; 
}

```
