# 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:** 1\
**Showing post:** 11

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

---

_[View the full topic](https://fortran-lang.discourse.group/t/partial-derivative/2625)._
