# Lecture 7

Finite Differences, More Fortran, and Makefiles

## Recap

We saw that Taylor series could be used to find approximations to derivatives. Let $$x_i + h = x_{i+1}$$ and denote $$f_i = f(x_i)$$ then:

$\begin{eqnarray} f_{i+1} \approx f_i + h \frac{d f_i}{d x} + \frac{h^2}{2} \frac{d^2 f_i}{d x^2} + \frac{h^3}{6} \frac{d^3 f_i}{d x^3} + \mathcal{O}(h^4), \\ f_{i-1} \approx f_i - h \frac{d f_i}{d x} + \frac{h^2}{2} \frac{d^2 f_i}{d x^2} - \frac{h^3}{6} \frac{d^3 f_i}{d x^3} + \mathcal{O}(h^4). \end{eqnarray}$

can be combined to

$$$\frac{f_{i+1} - f_{i-1}}{2h} \approx \frac{d f_i}{d x} + \mathcal{O}(h^2).$$$

## 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