Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

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 to simplify the code. MATLAB® generates intermediate variables as a lowercase t followed by an automatically generated number, for example t32.

Examples

The statements

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

return

ans =
  1×95 char array
      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 =
  1×236 char array
      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

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.

Introduced before R2006a

Was this topic helpful?