Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computational Physics F33SC2

Similar presentations


Presentation on theme: "Computational Physics F33SC2"— Presentation transcript:

1 Computational Physics F33SC2
Numerical methods in physics

2 Outline Module content Module organization Why C?
Compiling, linking and executing The Salford compiler The components of a C program

3 Module Aims and Objectives
teach the programming language C use it to investigate a range of numerical methods that are widely used to solve physics problems. Objectives: at the end of the module a student should be able to: formulate a physical problem in a manner suitable for computational solution choose appropriate numerical algorithms for the solution of a problem construct a working, structured program in C that includes standard numerical procedures to solve a physical problem assess the accuracy and reliability of their solutions to problems

4 Module Syllabus C-Programming: Introduction to Numerical Methods:
Introduction to the C language. Conversion from MATLAB to C. Use of the C compiler. Introduction to Numerical Methods: Data analysis and curve fitting. Numerical integration. Ordinary differential equations. Stochastic Techniques: Random numbers, non-uniform distributions, testing distributions. Monte-Carlo integration, precision of results. Random walks and Markov chains. Boundary Value Problems and Linear Algebra: Shooting and matching methods. Eigenvalues and eigenfunctions. Applications to quantum mechanics

5 Module Organization One lecture per week Two lab sessions per week
See module web page: One lecture per week Wednesdays 9.00 in C5 Introduces new concepts Two lab sessions per week Thursdays – and – 16.00 Hands-on session with computers You put the ideas into practice by working through example sheets and writing programs Ask for clarification and help from demonstrators

6 Assessment 20% Class test 30% Assignment 1: 50% Assignment 2:
10.00 – on 8 March in B11 Write some simple programs 30% Assignment 1: Given out 22 March, (the last Wed before Easter break) Write some longer piece of code to solve a simple problem Deadline 25 April (first Wed after Easter break) 50% Assignment 2: Given out 2 May Write more code to solve a more extended problem Deadline 17 May

7 Timetable 31/1/07 Introduction to C Programming
07/2/07 Further C Programming 14/2/07 Data analysis and curve fitting 21/2/07 Numerical integration 28/2/07 Ordinary Differential Equations 08/3/07 Class Test 10am B11 14/3/07 Stochastic Methods 1 21/3/07 Stochastic Methods 2 25/4/07 Boundary Value Problems; Linear Algebra 02/5/07 Linear Algebra 2; boundary value problems 10/5/07 Workshop for 2nd assignment

8

9 Why C? The C language is: small powerful portable
fewer keywords than Pascal powerful control structures and data types allow the user to program almost anything portable C compilers are available for most computers once compiled you can share exe files, and your friend then doesn’t even need a C compiler to run them terse but with a powerful set of operators Modular allowing the user to build up libraries of functions generally efficient popular

10 C was written specifically to write the Unix operating system
MS-Dos is also written in C Matlab is written in C Both C++ and Java are based on C. Everything you learn here can be applied later to programming in these languages

11 Why not C? C will let you do almost anything C is very low-level
C can be complicated

12

13 Progaming in C C is a compiled programming language
You need 5 steps to get going Plan your program Type in your code Compile it to create an ‘object module’ Link it with other library modules Run it

14 Developing a program Programmer’s plan Keyboard Source Code Compile
User functions Object Code Library Code Link Executable Program

15 Enter the code Create source code containing a list of C commands written in proper syntax Use any text editor to enter the code and save the c code with the extension ‘.c’ Use a program development environment such as the Salford Plato environment

16 Salford C Compiler

17 Compiling Compile the c code
The compiler reads the C commands and translates them into machine code that gives individual instructions for the computer microprocessors Save the resulting object code to disc Object files make it possible to compile a program module by module, hence saving time.

18 Linking/Building Executing
The linker takes the obj file and links it together with other object files you tell it to link to and with library function object code, such as the sine function The result is an executable file (.exe or .com) This step is called Building on the Salford C compiler Executing Running the executable file will make the computer do whatever you asked it to do

19 Developing a program Programmer’s plan Keyboard Source Code Compile
User functions Object Code Library Code Link Executable Program

20 Developing a program Programmer’s plan Keyboard Source Code DEBUG
Editor User functions Object Code Library Code DEBUG Link Executable Program Verify and Check, DEBUG

21 Salford C compiler Access this through the NAL as Plato3 under the ‘Compilers & Development tools’ menu Copies of the compiler can also be obtained from Dr Hawker

22 Compiling linking and executing in Salford
To compile Build > Compile (control + F7) To link Build > Build (control + shift + b) To execute Build > Start Run (control + F5)

23 Points to note Keep source code, as this will allow you to modify programs Give your programs sensible names so you know what they do Put comments in the code to explain what they do and how they do it C++ compilers will also compile C code

24 Components of a C program
1: /* Program to calculate the product of two numbers */ 2: # include <stdio.h> 3: int a,b,c; 4: int main() /**************************************/ 5: { /* Get the first number */ 6: printf ("Enter a number between 1 and 100: "); 7: scanf ("%d", &a); 8: /* Get a second number */ 9: printf ("Enter a number between 1 and 100: "); 10: scanf ("%d", &b); 11: /*Calculate the product */ 12: c = a*b; 13: printf("%d times %d = %d \n", a, b, c); 14: return 0; 15: }

25 Line numbers Text of the program Not part of the code
They are just to help identify lines now Text of the program It is case sensitive – like Matlab The variable A is different to a.

26 main (lines 4-15) main(){} is the only obligtory statement
Generally the main body of the code lies within the braces {} Generally statements enclosed by braces are called a ‘block’ Blocks can actually be used anywhere a statement is used

27 include (line 2) Instructs the C compiler to add lines from the included file during compilation Several header files are provided with the compiler They have extension *.h You never need to modify them

28 Variable definitions (line 3)
A variable is a name assigned to a data storage location A variable must be defined before it is used (different to Matlab) Various types of data are stored (int, float) This informs compiler of variable’s name and data type In this case 3 variables are defined: the integers a, b and c

29 Program statements The actual instructions to do the calculations
Generally written one per line ALWAYS END WITH A SEMICOLON

30 printf() A library function Displays information on screen
Lines of text (lines 7 & 10) Values of variables (line 14)

31 scanf() Another library function
Reads data from keyboard and assigns it to a program variable

32 Assignment statements
Similar to Matlab The statement a = b*c; calculates the value of b times c and stores the result in the location of variable a

33 return() At line 14 it simply returns the value 0 to the operating system as the program ends. This is good style, but the line can be left out. Has more important use in functions

34 Comments (lines 1, 5 etc) Any part of the program that starts with /* and ends with */ is called a comment A comment is just information for a human being, the compiler will ignore it In Salford C you can also start a comment with // provided there is nothing else after it on the line If you use this your program will no longer be portable

35 Style C Programs can be made to be very short indeed
This can make for rapid programming, but can also make it hard to read the program again later A reasonable number of useful comments will help A good layout will also help Indentation of code is essential to make a C program easy to follow

36 In the lab session Download the program from the website
Compile, link (build) and run it Deliberately put in some errors to the text and repeat compiling etc Change it so that it accept and multiplies three numbers Change it so that it accepts and multiplies one of the numbers as a float Follow the worksheet to write other programs

37 for loops Can set up loops to repeat sections of code
for (count = 1 ; count <=3 ; count=count+1) { printf("%d \n", count); }

38 /* program to read 10 numbers from a file data.dat */
/* and calculate the product */ include <stdio.h> int a,b,c,n; int main() /**************************************/ { FILE *fin ; fin = fopen("data.dat", "r") ; for ( n=1 ; n<=10 ; n=n+1) fscanf(fin,"%d %d",&a,&b) ; c = a*b; printf("%d * %d = %d \n", a,b,c); } fclose(fin) ; return 0;


Download ppt "Computational Physics F33SC2"

Similar presentations


Ads by Google