# fortran

Fortran representation of symbolic expression

## Syntax

``fortran(f)``
``fortran(f,Name,Value)``

## Description

````fortran(f)` returns Fortran code for the symbolic expression `f`.```

example

````fortran(f,Name,Value)` uses additional options specified by one or more `Name,Value` pair arguments.```

## Examples

collapse all

Generate Fortran code from the symbolic expression `log(1+x)`.

```syms x f = log(1+x); fortran(f)```
```ans = ' t0 = log(x+1.0D0)'```

Generate Fortran code for the 3-by-3 Hilbert matrix.

```H = sym(hilb(3)); fortran(H)```
```ans = ' H(1,1) = 1.0D0 H(1,2) = 1.0D0/2.0D0 H(1,3) = 1.0D0/3.0D0 H(2,1) = 1.0D0/2.0D0 H(2,2) = 1.0D0/3.0D0 H(2,3) = 1.0D0/4.0D0 H(3,1) = 1.0D0/3.0D0 H(3,2) = 1.0D0/4.0D0 H(3,3) = 1.0D0/5.0D0'```

Write generated Fortran code to a file by specifying the `File` option. When writing to a file, `fortran` optimizes the code using intermediate variables named `t0`, `t1`, .… Include comments in the file by using the `Comments` option.

```syms x f = diff(tan(x)); fortran(f,'File','fortrantest')```
``` t0 = tan(x)**2+1.0D0 ```

Include the comment `Version: 1.1`. Comment lines must be shorter than 71 characters to conform with Fortran 77.

`fortran(f,'File','fortrantest','Comments','Version: 1.1')`
```*Version: 1.1 t0 = tan(x)**2+1.0D0```

## Input Arguments

collapse all

Symbolic input, specified as a symbolic expression.

### Name-Value Arguments

Specify optional pairs of arguments as `Name1=Value1,...,NameN=ValueN`, where `Name` is the argument name and `Value` is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose `Name` in quotes.

Example: `fortran(x^2,'File','fortrancode','Comments','V1.2')`

File to write to, specified as a character vector or string. When writing to a file, `fortran` optimizes the code using intermediate variables named `t0`, `t1`, ....

Comments to include in the file header, specified as a character vector, cell array of character vectors, or string vector. Comment lines must be shorter than 71 characters to conform with Fortran 77.

## Tips

• MATLAB® is left-associative while Fortran is right-associative. If ambiguity exists in an expression, the `fortran` function must follow MATLAB to create an equivalent representation. For example, `fortran` represents `a^b^c` in MATLAB as `(a**b)**c` in Fortran.

## Version History

Introduced before R2006a