Fortran for Matlab Programmers

These notes were developed as I was learning how to program in Fortran 90. Matlab is a great scripting language; however, when you need to do a lot of numerical computations, nothing beats Fortran (or C).

Here, I list below code snippets from Matlab and Fortran 90 to illustrate similiarities and differences between the syntax of the two languages.

Comments

Matlab Fortran 90
Uses % Uses !

Continuation of programming line

Matlab Fortran 90
A = 174.5 * year ...
       + count / 100
A = 174.5 * year &
       + count / 100

If Condition

Matlab Fortran 90
if <condition>
 <statements>
elseif <condition>
 <statements>
else
 <statements>
end
if (<condition>) then
 <statements>
else if (<condition>) then
 <statements>
else
 <statements>
end do

For Loop

Matlab Fortran 90
for i = 1:5
 <statements>
end
do i = 1, 5
 <statements>
end do

While Loop

Matlab Fortran 90
while (<condition>)
 <statement>
end
do while (<condition>)
 <statements>
end do

Function

Matlab Fortran 90
function y = foo(x)
 <statement>
 y = <value>
end
function foo(x)
  real :: x
  real :: foo
  foo = <value>
end function foo

Relational Operators

Matlab Fortran 90
<
<
>
>
<=
<=
>=
>=
==
==
~=
/=

Logical Operators

Matlab Fortran 90
~
.not.
||
.or.
&&
.and.
==
.eqv.
~=
.neqv.

Tags: ,

Reply

Your email address will not be published. Required fields are marked *