Main Content

fortran

Fortran representation of symbolic expression

Description

fortran(f)returns Fortran code for the symbolic expressionf.

example

fortran(f,Name,Value)uses additional options specified by one or moreName,Valuepair arguments.

Examples

collapse all

Generate Fortran code from the symbolic expressionlog(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 theFileoption. When writing to a file,fortranoptimizes the code using intermediate variables namedt0,t1, .… Include comments in the file by using theCommentsoption.

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

Include the comment已经rsion: 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 asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis 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 encloseNamein 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,fortranoptimizes the code using intermediate variables namedt0,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, thefortranfunction must follow MATLAB to create an equivalent representation. For example,fortranrepresentsa^b^cin MATLAB as(a**b)**cin Fortran.

已经rsion History

Introduced before R2006a