Fortran representation of symbolic expression
Generate Fortran Code from Symbolic Expression
Generate Fortran code from the symbolic expression
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 Fortran Code to File with Comments
Write generated Fortran code to a file by specifying the
File option. When writing to a file,
fortran optimizes the code using intermediate variables
t1, .… Include comments in the
file by using the
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.
*Version: 1.1 t0 = tan(x)**2+1.0D0
f — Symbolic input
Symbolic input, specified as a symbolic expression.
Specify optional pairs of arguments as
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.
File — File to write to
character vector | string
File to write to, specified as a character vector or string. When
writing to a file,
fortran optimizes the code using
intermediate variables named
Comments — Comments to include in file header
character vector | cell array of character vectors | string vector
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.
MATLAB® is left-associative while Fortran is right-associative. If ambiguity exists in an expression, the
fortranfunction must follow MATLAB to create an equivalent representation. For example,
a^b^cin MATLAB as
Introduced before R2006a