fortran

Fortran representation of symbolic expression

Syntax

fortran(S)
fortran(S,'file',fileName)

Description

fortran(S) returns the Fortran code equivalent to the expression S.

fortran(S,'file',fileName) writes an "optimized" Fortran code fragment that evaluates the symbolic expression S to the file named fileName. "Optimized" means intermediate variables are automatically generated in order to simplify the code. MATLAB® generates intermediate variables as a lowercase letter t followed by an automatically generated number, for example t32.

Examples

The statements

syms x
f = taylor(log(1+x));
fortran(f)

return

ans =
      t0 = x-x**2*(1.0D0/2.0D0)+x**3*(1.0D0/3.0D0)-x**4*(1.0D0/4.0D0)+x*
     &*5*(1.0D0/5.0D0)

The statements

H = sym(hilb(3));
fortran(H)

return

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

The statements

syms x
z = exp(-exp(-x));
fortran(diff(z,3),'file','fortrantest')

return a file named fortrantest containing the following:

      t7 = exp(-x)
      t8 = exp(-t7)
      t0 = t8*exp(x*(-2))*(-3)+t8*exp(x*(-3))+t7*t8
Was this topic helpful?