Teaching Fortran to Physics Majors
Aleksandar Donev, Dr.Phillip Duxbury
Michigan State University, December 2000
Rk4 Fortran Code Examples
Fourth Order Runge-Kutta. Intro; First Order; Second; Fourth; Printable; Contents Introduction. In the last section it was shown that using two estimates of the slope (i.e., Second Order Runge Kutta; using slopes at the beginning and midpoint of the time step, or using the slopes at the beginninng and end of the time step) gave an approximation with greater accuracy than using just a single. Related Articles and Code: Runge-Kutta Second Order; RUNGE-KUTTA METHOD; Program to estimate the Differential value of a given function using Runge-Kutta Methods; Program that declares and initialize a 2D array in row major order, and print the contents of the 3rd row and 4th column using Register Indirect mode. A slight variation of the code was used to show the effect of the size of hon the accuracy of the solution (see image below). Note that larger values of h result in poorer approximations, but that the solutions are much better than those obtained with the First Order Runge-Kutta for the same value of h (and appears similar to results obtained. The most widely used programming languages, presently C, C and Fortran and its most recent standard Fortran 20081. However, a high-level and fully object-oriented language like Python is now emerging as a good alternative although C and Fortran still outperform Python when it comes to computational speed. In this text we offer an approach. C Runge Kutta for first order differential equations c PROGRAM Runge-Kutta IMPLICIT none c c declarations c nsteps:number of steps, tstep:length of steps, y: initial position c REAL.8 t, y, tstep INTEGER i, j, nsteps nsteps=10 tstep=0.5 y=1.0 c c open file OPEN(6, FILE='rungef.dat') WRITE (6,.) 0, y c c do loop nsteps of Runga-Kutta algorithm DO 60 j = 1, nsteps t=j.tstep call rk4(t.
This webpage contains sample worksheets for amodelintroductory course in computationalphysics that attempts to teach physics majors Fortran 90/95. In asomewhatmodified form, these have been used in an actual PHY201course at MSU during Fall 2000. These worksheets are by no meanscomplete,and there are significant gaps in the coverage.
Worksheet 1: Introduction
The way students are introduced to this material should vary greatlydepending on the students' backgrounds. The worksheets that follow arenotan introduction to computation. Experience with programming isassumed,such as the PHY102course. Some students found the course easy, some challenging, somedifficult. We see no easy fix for this one. So put any worksheet(s)herethat you deem appropriate.
...
The Error function
This next series of worksheets deals with the calculation of theerror function using Fortran 90. It is meant to introduce basic Fortrantools such as program structure, control flow, as well as some aspectsof numerical accuracy, precision and speed. These worksheets havenothingto do with physics and can be used in any scientific programmingcourse.Further below are physics-related worksheets.
The way students are introduced to this material should vary greatlydepending on the students' backgrounds. The worksheets that follow arenotan introduction to computation. Experience with programming isassumed,such as the PHY102course. Some students found the course easy, some challenging, somedifficult. We see no easy fix for this one. So put any worksheet(s)herethat you deem appropriate.
...
The Error function
This next series of worksheets deals with the calculation of theerror function using Fortran 90. It is meant to introduce basic Fortrantools such as program structure, control flow, as well as some aspectsof numerical accuracy, precision and speed. These worksheets havenothingto do with physics and can be used in any scientific programmingcourse.Further below are physics-related worksheets.
Rk4 Fortran Code Example
Worksheet 2: (PDF, HTML): Solution {Fortran 90 codes [real, complex], Compilation and Execution}
Worksheet 3: (PDF, HTML): Solution {Fortran 90 codes [calculation module,mainprogram, high-precisionmoduleby Allan Miller], Compilation andExecution}
Worksheet 4: (PDF, HTML): Solution {Fortran 90 code, xmgraceplot, Compilation and Execution}
Worksheet 5: (PDF, HTML): Solution {Fortran 90 code [calculation module,mainprogram], Compilation and Execution}
Worksheet 6: (PDF, HTML): Solution (Fortran 90 code [using SimpleGraphics(HTML documentation, sourcecode, initialization script), usingFunGraphics (HTML documentation, sourcecode)], DISLIN plot, Compilationand Execution)
Worksheet 3: (PDF, HTML): Solution {Fortran 90 codes [calculation module,mainprogram, high-precisionmoduleby Allan Miller], Compilation andExecution}
Worksheet 4: (PDF, HTML): Solution {Fortran 90 code, xmgraceplot, Compilation and Execution}
Worksheet 5: (PDF, HTML): Solution {Fortran 90 code [calculation module,mainprogram], Compilation and Execution}
Worksheet 6: (PDF, HTML): Solution (Fortran 90 code [using SimpleGraphics(HTML documentation, sourcecode, initialization script), usingFunGraphics (HTML documentation, sourcecode)], DISLIN plot, Compilationand Execution)
Electromagnetism in Fortran and Mathematica
This next series of worksheets do some meaningful and interestingphysics using Fortran 90 and Mathematica. They may be too complicatedtomove to immediately after finishing the previous seriesof worksheets. For yet more advanced interesting worksheets withmoreemphasis on numerical analysis, please look at the Spring2000 PHY480 webpage of problems and solutions.
Worksheet 7: (PDF, HTML): Solution {Fortran 90 code [source code,graphs(as surface plot, ascontour plot, as vector field plot), Compilation and Execution],Mathematicasolution (HTML, notebook)}
Worksheet 8: (PDF, HTML): Solution {Fortran 90 code [using AllanMiller'squadrature code (utility module &initializationscript, source of quadrature routines& dependency module),exampleof trapezoid integration,vector plot of field,Compilation and Execution], Mathematicasolution (HTML,notebook)}
Worksheet 9: (PDF, HTML): Solution {Fortran 90 code [using RKSUITE (ODEutility module, & initialization script,sourceof quadrature routines & example of RK4Fortran90 code), example of harmonic oscillator ODE usingRKSUITE & using Eulerintegration(the plot), plots (case1, case 2, case3, case 4), Compilationand Execution], Mathematica solution (HTML,notebook)}
Example for Final Exam
Use numerical integration to calculate the value of the errorfunctionfor a given real argument x. Write the result to a file and follow thegood-programming guidelines you've been given in class. Write yourFortran90 code by hand, and pay attention to program organization andefficiencyas much as you can!
Here is a sample solution that uses the modulefrom worksheet 2 for validation.
This next series of worksheets do some meaningful and interestingphysics using Fortran 90 and Mathematica. They may be too complicatedtomove to immediately after finishing the previous seriesof worksheets. For yet more advanced interesting worksheets withmoreemphasis on numerical analysis, please look at the Spring2000 PHY480 webpage of problems and solutions.
Worksheet 7: (PDF, HTML): Solution {Fortran 90 code [source code,graphs(as surface plot, ascontour plot, as vector field plot), Compilation and Execution],Mathematicasolution (HTML, notebook)}
Worksheet 8: (PDF, HTML): Solution {Fortran 90 code [using AllanMiller'squadrature code (utility module &initializationscript, source of quadrature routines& dependency module),exampleof trapezoid integration,vector plot of field,Compilation and Execution], Mathematicasolution (HTML,notebook)}
Worksheet 9: (PDF, HTML): Solution {Fortran 90 code [using RKSUITE (ODEutility module, & initialization script,sourceof quadrature routines & example of RK4Fortran90 code), example of harmonic oscillator ODE usingRKSUITE & using Eulerintegration(the plot), plots (case1, case 2, case3, case 4), Compilationand Execution], Mathematica solution (HTML,notebook)}
Example for Final Exam
Use numerical integration to calculate the value of the errorfunctionfor a given real argument x. Write the result to a file and follow thegood-programming guidelines you've been given in class. Write yourFortran90 code by hand, and pay attention to program organization andefficiencyas much as you can!
Here is a sample solution that uses the modulefrom worksheet 2 for validation.