# Partial derivative

**URL:** <https://fortran-lang.discourse.group/t/partial-derivative/2625>\
**Category:** Help\
**Created:** [January 21, 2022, 3:01pm UTC](https://fortran-lang.discourse.group/t/partial-derivative/2625 "2022-01-21T15:01:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dka](https://avatars.discourse-cdn.com/v4/letter/d/c37758/32.png) [@dka](https://fortran-lang.discourse.group/u/dka)\
**Post date:** [January 21, 2022, 3:01pm UTC](https://fortran-lang.discourse.group/t/partial-derivative/2625/1 "2022-01-21T15:01:12Z")

</div>

Hi  
I am very new to Fortran, I am trying to find if there is a simple way to do partial derivative, like in MATLAB or python for instance-. Is there a package that I can use ?  
Thank you !

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [January 21, 2022, 3:29pm UTC](https://fortran-lang.discourse.group/t/partial-derivative/2625/2 "2022-01-21T15:29:59Z")

</div>

Welcome to the forum. Some threads on automatic differentiation are

> [@Backward Mode Auto-Diff in Modern Fortran](https://fortran-lang.discourse.group/t/backward-mode-auto-diff-in-modern-fortran/2334):
>
> Hello, I don’t know if you are interested, I’m also not sure if we are doing the right thing? @St_Maxwell wrote a [Fortran code](https://gist.github.com/St-Maxwell/0a936b03ecf99e284a05d10dd994516e) for backward automatic differentiation. I modified it slightly, and I believe it can draw inspiration from the [joddlehod/DNAD](https://github.com/joddlehod/dnad)(Forward Mode) code and become a complete backward differentiation code. [zoziha/Auto-Diff: Fortran backward mode automatic differentiation. (github.com)](https://github.com/zoziha/Auto-Diff) Currently there is only one example: fpm run --example demo1 
> 
> > **demo1.f90**
> >
> > !\> Backward aut…

> [@Automatic differentiation of Fortran code, opinions?](https://fortran-lang.discourse.group/t/automatic-differentiation-of-fortran-code-opinions/369):
>
> Hello community, I am planning to implement the automatic differentiation functionality on code which I have developed in Fortran. What are your opinions regarding the numerous packages listed on [http://www.autodiff.org](http://www.autodiff.org)? Does anybody have good experience with one or anothe and would recommend specific package? Thank you Kind regards

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [January 21, 2022, 3:54pm UTC](https://fortran-lang.discourse.group/t/partial-derivative/2625/3 "2022-01-21T15:54:21Z")

</div>

Welcome @dka!

Could you be more specific what type of partial derivatives do you need? Is it related to finite differences, finite elements, or a perhaps just a Jacobian calculation?

---

<div class="post-metadata">

**Author:** ![dka](https://avatars.discourse-cdn.com/v4/letter/d/c37758/32.png) [@dka](https://fortran-lang.discourse.group/u/dka)\
**Post date:** [January 21, 2022, 3:57pm UTC](https://fortran-lang.discourse.group/t/partial-derivative/2625/4 "2022-01-21T15:57:04Z")

</div>

Hi @ivanpribec  
Yes it is related to finite elements. I need to calculate the Jacobian and B-matrix for a specific element.

---

<div class="post-metadata">

**Author:** ![Ashok](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Ashok](https://fortran-lang.discourse.group/u/Ashok)\
**Post date:** [January 22, 2022, 2:40am UTC](https://fortran-lang.discourse.group/t/partial-derivative/2625/5 "2022-01-22T02:40:16Z")

</div>

For analytical jacobian or partial derivatives, I think you would require symbolic computing. But for the numerical ones - simple forward, backward or central difference would do.  
For which element are you doing ? Are you working on solid/structural elements - beam, plate, shell …?

---

<div class="post-metadata">

**Author:** ![dka](https://avatars.discourse-cdn.com/v4/letter/d/c37758/32.png) [@dka](https://fortran-lang.discourse.group/u/dka)\
**Post date:** [January 23, 2022, 1:49pm UTC](https://fortran-lang.discourse.group/t/partial-derivative/2625/6 "2022-01-23T13:49:52Z")

</div>

Hi @Ashok  
it is for a 4 nodes isoparametric element

---

<div class="post-metadata">

**Author:** ![rwmsu](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@rwmsu](https://fortran-lang.discourse.group/u/rwmsu)\
**Post date:** [January 23, 2022, 3:41pm UTC](https://fortran-lang.discourse.group/t/partial-derivative/2625/7 "2022-01-23T15:41:12Z")

</div>

For FEM, the Jacobian and other derivatives depend on the shape functions you are using. The Jacobian to map derivatives between iso-parametric and physical coordinates can be computed analytically (they are just polynomials or rational functions such as NURBS). The resulting equations are the same in any programming language so I’m not sure why you think Fortran would be any different. Any FEM textbook written in the last 50 years will discuss how to compute the derivatives and there are a multitude of open source FEM codes in various programming languages you can use as an example.

There are as many FEM textbooks as there are grains of sand on a beach but here are three that I find useful.

Thomas Hughes,“The Finite Element Method - Linear, Static, and Dynamic Finite Element Analysis”, Dover Publications

Covers the basics well and is a lot cheaper than other FEM textbooks which can be over $200 for the most recent editions.

Smith and Griffiths," Programming the Finite Element Method", Wiley  
There is companion Fortran code for this book

Gennady Nikishkov, “Programming Finite Elements in Java”, Springer  
A useful book if you want to take an object oriented approach because the author defines the various classes you need to create a FEM code and Java is a relatively easy language to translate into Fortran.

For the open source code, a web search or searching github will show you a wealth of working codes.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [January 23, 2022, 5:44pm UTC](https://fortran-lang.discourse.group/t/partial-derivative/2625/8 "2022-01-23T17:44:51Z")

</div>

> [@rwmsu](#):
>
> For FEM, the Jacobian and other derivatives depend on the shape functions you are using. The Jacobian to map derivatives between iso-parametric and physical coordinates can be computed analytically (they are just polynomials or rational functions such as NURBS). The resulting equations are the same in any programming language so I’m not sure why you think Fortran would be any different. Any FEM textbook written in the last 50 years will discuss how to compute the derivatives and there are a multitude of open source FEM codes in various programming languages you can use as an example.
> 
> There are as many FEM textbooks as there are grains of sand on a beach but here are three that I find useful.
> 
> Thomas Hughes,“The Finite Element Method - Linear, Static, and Dynamic Finite Element Analysis”, Dover Publications
> 
> Covers the basics well and is a lot cheaper than other FEM textbooks which can be over $200 for the most recent editions.
> 
> Smith and Griffiths," Programming the Finite Element Method", Wiley  
> There is companion Fortran code for this book

One can also search “finite elements” at my [Fortran-related books](https://github.com/Beliavsky/Fortran-related-books) list, which has links to codes when available.

---

<div class="post-metadata">

**Author:** ![Ashok](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Ashok](https://fortran-lang.discourse.group/u/Ashok)\
**Post date:** [January 24, 2022, 12:38am UTC](https://fortran-lang.discourse.group/t/partial-derivative/2625/9 "2022-01-24T00:38:38Z")

</div>

For a 4 node isoparametric element, the shape functions are:

```auto
N1 = (1-s)(1-t)/4
N2 = (1-s)(1+t)/4
N3 = (1+s)(1+t)/4
N4 = (1+s)(1-t)/4

```

The B matrix would be as shown in the attached image. Replace x and y in the figure with s and t (isoparametric coordinates)

![b_matrix](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/a/a96a2c3bc90da6e7ae80583bd7b77efda87f421d.png)

The partial derivatives can be calculated simply by hand and for each element you have to pass the nodal coordinates - to evaluate the B matrix for that element.

---

<div class="post-metadata">

**Author:** ![dka](https://avatars.discourse-cdn.com/v4/letter/d/c37758/32.png) [@dka](https://fortran-lang.discourse.group/u/dka)\
**Post date:** [January 26, 2022, 5:10pm UTC](https://fortran-lang.discourse.group/t/partial-derivative/2625/10 "2022-01-26T17:10:42Z")

</div>

@Ashok @Beliavsky@rwmsu Thank you very much! I am actually aware of the mathematical part, I was wondering if there is a tool that does this automatically like diff() in matlab.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [January 26, 2022, 8:34pm UTC](https://fortran-lang.discourse.group/t/partial-derivative/2625/11 "2022-01-26T20:34:45Z")

</div>

MATLAB has two `diff` routines:

1. [`diff(X)`](https://www.mathworks.com/help/matlab/ref/diff.html) to calculate differences and approximate derivatives
2. [`diff(f,var)`](https://www.mathworks.com/help/symbolic/diff.html) to differentiate a symbolic expression or function.

I guess the second one is what you had in mind. Here’s a MATLAB script, which generates the Jacobian for the shape functions given by @Ashok:

```nohighlight
syms s t real

N1 = (1-s)*(1-t)/4;
N2 = (1-s)*(1+t)/4;
N3 = (1+s)*(1+t)/4;
N4 = (1+s)*(1-t)/4;

J = jacobian([N1,N2,N3,N4],[s, t]);

fortran(J,'file','four_node_jac.inc');

```

The resulting Fortran code:

```fortran
      t2 = s/4.0D0
      t3 = t/4.0D0
      t4 = -t2
      t5 = -t3
      A0(1,1) = t3-1.0D0/4.0D0
      A0(1,2) = t2-1.0D0/4.0D0
      A0(2,1) = t5-1.0D0/4.0D0
      A0(2,2) = t4+1.0D0/4.0D0
      A0(3,1) = t3+1.0D0/4.0D0
      A0(3,2) = t2+1.0D0/4.0D0
      A0(4,1) = t5+1.0D0/4.0D0
      A0(4,2) = t4-1.0D0/4.0D0

```

You’d probably want to edit this to match expected variables names, or change the precision.

SymPy has a more customizable Fortran code printer compared to MATLAB; see [Fortran printing](https://docs.sympy.org/latest/modules/printing.html#fortran-printing) in the SymPy documentation. A previous post of mine gives a complete example: [Code generation using SymPy](https://fortran-lang.discourse.group/t/code-generation-using-sympy/321).
