# Lecture 7

Finite Differences, More Fortran, and Makefiles

## Recap

We saw that Taylor series could be used to find approximations to derivatives. Let and denote then:

can be combined to

## Recap

To implement this in Fortran it is convenient to use arrays.

1. Arrays in Fortran can be Allocatable or fixed size.
2. Index starts at 1 by default but can be chosen to start at any number.
3. Intrinsic functions are element wise (except matmul and transpose).

## Arrays in Fortran

Allocatable or fixed size

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 integer, parameter :: n = 3 integer :: i real(kind = 8) :: A(1:3,-2:0), B(n,n) real(kind = 8), dimension(:,:), allocatable :: C allocate(C(0:n-1,0:n-1)) A = 1.d0 B = 2.d0 B(3,3) = 0.d0 C = A+B do i = 1,n write(*,*) C(i,:) end do deallocate(C) 

## Arrays in Fortran

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 integer, parameter :: n = 3 integer :: i real(kind = 8) :: A(1:3,-2:0), B(n,n) real(kind = 8), dimension(:,:), allocatable :: C allocate(C(0:n-1,0:n-1)) A = 1.d0 B = 2.d0 B(3,3) = 0.d0 C = A+B do i = 1,n write(*,*) C(i,:) end do deallocate(C) 

## Arrays in Fortran

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 integer, parameter :: n = 3 integer :: i real(kind = 8) :: A(1:3,-2:0), B(n,n) real(kind = 8), dimension(:,:), allocatable :: C allocate(C(0:n-1,0:n-1)) A = 1.d0 B = 2.d0 B(3,3) = 0.d0 C = A*B do i = 1,n write(*,*) C(i,:) end do deallocate(C) 

## Arrays in Fortran

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 integer, parameter :: n = 3 integer :: i real(kind = 8) :: A(1:3,-2:0), B(n,n) real(kind = 8), dimension(:,:), allocatable :: C allocate(C(0:n-1,0:n-1)) A = 1.d0 B = 2.d0 B(3,3) = 0.d0 C = matmul(A,B) do i = 1,n write(*,*) C(i,:) end do deallocate(C) 

## Show Demo Example

1. differentiate_v1.f90

## Makefile5, Macros and Rules

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 FC = gfortran LD = gfortran LDFLAGS = FFLAGS = -O3 F90FLAGS = -O3 OBJECTS = differentiate_v3.o weights.o lglnodes.o .PHONY: clean diff.x: $(OBJECTS)$(LD) $(OBJECTS) -o diff.x %.o : %.f90$(FC) $(F90FLAGS) -c$< %.o : %.f $(FC)$(FFLAGS) -c $< clean: rm -f$(OBJECTS) diff.x *.eps out.txt output.txt 

## Makefile5, Macros and Rules

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 FC = gfortran LD = gfortran LDFLAGS = FFLAGS = -O3 F90FLAGS = -O3 OBJECTS = differentiate_v3.o weights.o lglnodes.o .PHONY: clean diff.x: $(OBJECTS)$(LD) $(OBJECTS) -o diff.x %.o : %.f90$(FC) $(F90FLAGS) -c$< %.o : %.f $(FC)$(FFLAGS) -c $< clean: rm -f$(OBJECTS) diff.x *.eps out.txt output.txt