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. | 
                     
                    
                    
No comments
Comments feed for this article
Trackback link: https://kohar.ca/programming/fortran-for-matlab-programmers/trackback/