# Access matrix element

**URL:** https://community.freefem.org/t/access-matrix-element/885
**Category:** General Discussion
**Created:** [March 31, 2021, 6:55pm UTC](https://community.freefem.org/t/access-matrix-element/885 "2021-03-31T18:55:50Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![yongxing](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/yongxing/32/3632_2.png) [@yongxing](https://community.freefem.org/u/yongxing)
#### Post date: [March 31, 2021, 6:55pm UTC](https://community.freefem.org/t/access-matrix-element/885/1 "2021-03-31T18:55:50Z")

</div>

Dear all,

I can understand we can create the system matrix in FreeFEM++, my question is:  
Can we modify the element of the matrix, such as putting a penalty (a large number) on the diagonal for a specific degree of freedom?

Best,  
Yongxing.

---

<div class="post-metadata">

### Author: ![Daoudi](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@Daoudi](https://community.freefem.org/u/Daoudi)
#### Post date: [March 31, 2021, 11:39pm UTC](https://community.freefem.org/t/access-matrix-element/885/2 "2021-03-31T23:39:38Z")

</div>

[https://doc.freefem.org/tutorials/heatEquationOptimization.html](https://doc.freefem.org/tutorials/heatEquationOptimization.html)

---

<div class="post-metadata">

### Author: ![yongxing](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/yongxing/32/3632_2.png) [@yongxing](https://community.freefem.org/u/yongxing)
#### Post date: [April 1, 2021, 6:17am UTC](https://community.freefem.org/t/access-matrix-element/885/3 "2021-04-01T06:17:33Z")

</div>

Thank you, but this is still operating the whole matrix rather than a particular element of the matrix. I am thinking a penalty method to enforce Drichlet BC at a point?

---

<div class="post-metadata">

### Author: ![Daoudi](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@Daoudi](https://community.freefem.org/u/Daoudi)
#### Post date: [April 1, 2021, 7:11am UTC](https://community.freefem.org/t/access-matrix-element/885/4 "2021-04-01T07:11:11Z")

</div>

Can you send me the copy of your freefem code  
and the specific point?

---

<div class="post-metadata">

### Author: ![yongxing](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/yongxing/32/3632_2.png) [@yongxing](https://community.freefem.org/u/yongxing)
#### Post date: [April 1, 2021, 10:43am UTC](https://community.freefem.org/t/access-matrix-element/885/5 "2021-04-01T10:43:03Z")

</div>

[LidDrivenCavityDisc.edp](https://community.freefem.org/uploads/short-url/mVxEAKRyW70t2q7NvV2zpqWdm3i.edp) (4.4 KB)  
Thanks very much for your help Daoudi.  
Can you please fix the velocity (u,v)=(0,0) at point (0.6, 0.5) on mesh Th – expecting that the solid disc would keep rotating stably without shifting away?  
Best,  
Yongxing.

---

<div class="post-metadata">

### Author: ![LucasPa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/lucaspa/32/347_2.png) [@LucasPa](https://community.freefem.org/u/LucasPa)
#### Post date: [April 2, 2021, 4:53pm UTC](https://community.freefem.org/t/access-matrix-element/885/6 "2021-04-02T16:53:38Z")

</div>

I am not 100% sure but here is what I would do (supposing that (0.6, 0.5) is a node of your mesh):

I did not look in details your script, but let’s assume we want to modify the matrix Aadj to impose a condition on the first component u (it would then be easy to do the same for other components) where Aadj is defined by:  
`fespace RhAdj(Th,[P2,P2,P2,P2,P1,P1]);`  
`varf NSAdj([u,v,uhat,vhat,p,phat],[uh,vh,uhath,vhath,ph,phath]) = ...;`  
`matrix Aadj = NSAdj(RhAdj,RhAdj,tgv=tgv);`

The question is which entry (I,I) of the matrix should we modify?

`int I; // index in the matrix corresponding to (0.6, 0.5) --> to find`

Let’s do the following:

fespace Vh(Th,P2);  
Vh xx = x, yy=y;  
RhAdj [w1x, w2x, s1x, s2x, wpx, spx] = [xx,0,0,0,0,0];  
RhAdj [w1y, w2y, s1y, s2y, wpy, spy] = [yy,0,0,0,0,0];

```
for (int k = 0; k < w1x[].n; ++k)
{
  if (w1x[][k] == 0.6 && w1y[][k]=0.5)
  {
    I = k;
  } 
}

```

And that should do it!

---

<div class="post-metadata">

### Author: ![yongxing](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/yongxing/32/3632_2.png) [@yongxing](https://community.freefem.org/u/yongxing)
#### Post date: [April 4, 2021, 8:36pm UTC](https://community.freefem.org/t/access-matrix-element/885/7 "2021-04-04T20:36:53Z")

</div>

Thank you very much! I will try this out.  
Best,  
Yongxing.
