Graphics Programs Reference
In-Depth Information
The syntax of MATLAB resembles thatofFORTRAN. To get an idea of the similari-
ties, let uscompare the codeswritteninthe twolanguages for solution of simultaneous
equations Ax
=
b by Gauss elimination. Here is the subroutine in FORTRAN 90:
subroutine gauss(A,b,n)
use prec
_
mod
implicit none
real(DP), dimension(:,:), intent(in out) :: A
real(DP), dimension(:),
intent(in out) :: b
integer, intent(in)
:: n
real(DP) :: lambda
integer :: i,k
! --------------Elimination phase--------------
dok=1,n-1
doi=k+1,n
if(A(i,k) /= 0) then
lambda = A(i,k)/A(k,k)
A(i,k+1:n) = A(i,k+1:n) - lambda*A(k,k+1:n)
b(i) = b(i) - lambda*b(k)
end if
end do
end do
! ------------Back substitution phase----------
dok=n,1,-1
b(k) = (b(k) - sum(A(k,k+1:n)*b(k+1:n)))/A(k,k)
end do
return
end subroutine gauss
The statement use prec mod tells the compiler to load the module prec mod
(not shown here), which defines the word length DP forfloating-point numbers.Also
note the use of array sections, such as a(k,k+1:n) , a featurethat was not available
in previous versionsofFORTRAN.
The equivalentMATLAB functionis(MATLAB does not havesubroutines):
functionb=gauss(A,b)
n = length(b);
%-----------------Elimination phase-------------
fork=1:n-1
fori=k+1:n
Search WWH ::

Custom Search