Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys....

48
Lecture Notes on Computational Physics Dr. A.E.Kotp

Transcript of Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys....

Page 1: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Lecture Notes on

Computational Physics

Dr. A.E.Kotp

Page 2: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Computational Physics - 4050 Phys.1.Introduction to Computation

1.1.Computing elements

1.1.1.Computer1.1.2.Formula1.1.3.Program

1.2.Computer model

1.2.1.input1.2.2.Cpu1.2.3.memory1.2.4.Storage

1.3.Computer types

1.3.1.Analogue Computer1.3.2.Digital Computer1.3.3.Hybrid Computer

1.4.Software model

1.4.1.Hardware1.4.2.ROM1.4.3.Operating System1.4.4.Software

1.5.Problem Solving methods

1.5.1.Analytical method1.5.2.Numerical method

1.6.Computing Concept

1.6.1.Data1.6.2.Processing1.6.3.Information

1.7.Program Design Methods

1.7.1.Program Analysis1.7.2.Algorithm1.7.3.Flow chart

1

Page 3: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

1.8.Programming Languages

1.8.1.Elements of programming language1.8.2.Compiled Languages1.8.3.Interpreted Languages

1.9.Mathematical Software

1.9.1.Numerical Analysis Software1.9.2.Computer Algebra Systems

1.10.Computing Errors

1.10.1.Machine Precision error1.10.2.Approximation error1.10.3.Round off error1.10.4.Random error1.10.5.Human error

2.Simple Computation

2.1.Sum Computation2.2.Product Computation

3.Function Computation

3.1.Linear Functions Computation3.2.Non linear Functions Computation3.3.Function Graphing

4.First Order Equation Computation

4.1.Newton Method

5.Second order Equation Computation

5.1.Formula Method

6.Linear Algebra Computation

6.1.Vectors Computation6.2.Matrices Computation6.3.Tensor Computation

2

Page 4: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

7.Numerical Solution of linear System of Equations

7.1.Marix-vector method

8.Numerical Differentiation

8.1.Forward Difference Method8.2.Backward Difference Method8.3.Central Difference Method

9.Numerical Integration

9.1.Rectangular Method9.2.Trapezoidal Method

10.Solving Ordinary Differential Equations

10.1.Euler Method10.2.Runge Kutte Method

11.An Introduction to Partial Differential Equations

11.1.Finite Element Method11.2.Finite Difference Method

Suggested books:

Computational Physics: Problem Solving, Rubin H. landau, Manuel J. Pa`es, John Wiley &Sons, Inc., 1997.

An Introduction to Computational Physics, Tao Pang, Cambridge University press, 1997.

Computational Physics 2nd ed, Nicholas J. Giordano, Hisao Nakanishi, 2005, ISBN:0-13-146990-8

3

Page 5: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Lecture.11.Introduction to Computation

1.1.Computing elements:There are three elements which must be found in order to do computation these elements are: (a)Computer(b)Formula(c)Program

1.1.1.Computer:

To compute some problem we need a computer which is nowadays had different types. These types are:

(a) Pocket Computer.(b)Laptop Computer. (c)Desktop Computer (d)Workstation Computer.(e)Super Computer

1.1.2.Formula:

A formula is a kind of equation that can be computed and it have different types:

(a)Simple Algebraic formula.(b)Calculus formula.(c)Linear Algebra formula.

1.2.3.Program:

A program is a kind of text instructions that can be obeyed by the computer. This program can be made using: a programming language such as, Basic, C, and C++.

a mathematical software such as, MatLab, Mathematica, and Euler.

1

Page 6: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

1.2.Computer model:

The above schematic diagram represent the model of any computer which are:

Input devices such as, keyboard, and mouse.

Central Processing unit which is the device able to compute.

Output devices such as, monitor, and printer.

Storage devices such as, hard desk,and CD-ROM.

1.3.Computer types:

1.3.1.Analogue Computer:

The Analogue computer is a computer which controls a different devices and its input and output are volts.

1.3.2.Digital Computer:

The digital computer is a computer which its input and output are digits.

1.3.3.Hybrid Computer:

The hybrid computer is a computer which its input and output are digits and volts.

2

CPUInputDevices

OutputDevices

Storage

Page 7: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

1.4.Software model:

The software model of the computer is a schematic diagram which represent how the software are composed on a computer.

1.4.1.Hardware:

The core of the model is hardware itself. Which is the computer devices.

1.4.2.ROM Programs:

The first layer is the ROM programs. These programs are:

(a) POST : Power On Self Test. Which is a program to check all the computer devices.

(b) BOOT : Which loads the operating systems.

1.4.3.Operating System:

The second layer is the Operating system is a collection of programs which able to do the following tasks:

(a)Input(b)Output(c)Storage

1.4.4.Software:

The third layer is the computing software.

3

Hardware

ROM programs

Operating System programs

Computing Software

Page 8: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Lecture.2

1.5.Problem Solving methods:

Any problem can be solved using two methods by which we can solve the problem

1.5.1.Analytical method:

In this method we use the mathematical analysis theory to solve the problem analytically. Which mean

that this method gives a formula to solve the problem.

1.5.2.Numerical method:

In this method we use the Numerical analysis theory to solve the problem numerically. Which mean that this method gives an algorithm to solve the problem.

1.6.Computing Concepts:

There are three main concepts of the computing process. The first is the data, the second is the processing and the third is the information.

1.6.1.Data:Is the pure data represented as numbers and tables of figures without any processing.

1.6.2.Processing:Is the manipulation of these data to produce some other useful numbers and figures.

1.6.3.Information:Is the results of data processing, which is a useful numbers and figures to human and constitute the solution of the problem.

1

DATA PROC INFO

Page 9: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

1.7.Program Design Methods:

There are three methods for program design, which help to build a good working program these methods are:

(a)Program Analysis(b)Algorithm(c)Flow chart

1.7.1.Program Analysis:

In this method of program analysis we determine what are the inputs, outputs variables and formulas needed to solve the problem. This method leads to give the skeletal of the program.

An example:

The analysis of a program to calculate the area of a circle using the radius and the constant value of pi will be as flow:

Inputs; r, pi

Formula; Area = Pi * r2

Outputs; A

2

R

Page 10: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

1.7.2.Algorithm:

In the algorithm method write in details what are the inputs in which we use the word:

READ, var1, var2, …......

Then we write in detail how to calculate the formula which solves the problem as:

Formula1.......Formula2......

In the algorithm method write in details what are the outputs in which we use the word:

PRINT, var1, var2,...............

And in the case of a loop we use the word:

For var = value in value.. value loop…........….......end loop

And in decision making we use the words:

IF Condition THEN Command

3

Page 11: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

1.7.3.Flow chart:

In the flow chart method we use a system of graphs to represent the output and input and processingin addition to conditions and loops. These graphs are:

READ

Processing Formula

PRINT

If Conditions

4

Page 12: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

START

END

For LOOP

5

Page 13: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Example:Area of the circle

6

Start

R, pi

A=pi*R2

End

A

Page 14: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Lecture.3

1.8.Programming Languages:

Programming languages are some instructions given as a text to the computer to be executed.

1.8.1.Elements of programming language:

(a)Comments:

Comments used to document the program.

(b)Constants:

Constants are some memory places which holds fixed values.

(c)Variables:

Variables are some memory places which holds a variable values.

(d)Loops:

Loops are used to repeat some calculations.

(e)Conditions:

Conditions are used to let the program decide something

(f)Functions:

Functions are used to do some calculations repeatedly.

(g)Subprogram:

Subprograms are small number of functions (1-10) to do some calculations.

1

Page 15: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

1.8.2.Compiled Languages:

Compiled languages are languages uses a program called “compiler” to change the program text to

an executing program.

1.8.3.Interpreted Languages:

Interpreted languages are languages uses a program called “interpreter” to change the program text

to an executing program.

2

Program Compiler

Executed program

Program Interpreter

Executed program

Page 16: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

1.9.Mathematical Software:

Mathematical software are software programmed to help to solve mathematical problems. These

programs such as; MATHEMATICA , Maxima, Euler, Octave.

1.9.1.Numerical Analysis Software:

Numerical software are software programmed to help to solve mathematical problems numerically.

These programs such as; Mat-lab, Octave, Euler, and Free-mat.

1.9.2.Computer Algebra Systems:

Algebra software are software programmed to help to solve mathematical problems symbolically.

These programs such as; MATHEMATICA , Maxima.

3

MathematicalSoftware

Numerical Symbolic

Page 17: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

1.10.Computing Errors:

Computing errors are errors take place during the computation process. These errors are as the

following:

1.10.1.Machine Precision error:

This error come from the precision of the computer where when the computer compute

For example the operation:

7 + 7 x 10-7 = 7

value + very small value = the same value

1.10.2.Approximation error:

This error come from the approximation processing of the computer where when the computer compute

the function:

e x=∑n=1

∞ xn

n!

Where, ∞ can not be represented on the computation so we substitute with N where, N is chosen as

N >> n

Then the function can then computed using the approximation

e x=∑n=1

N xn

n!

1.10.3.Round off error:

This error come from the round off of the computer where when the computer compute

For example the operation:

0.6666666 - 0.6666667 = 0

Small value - very small value = 0

4

Page 18: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

1.10.4.Random error:

This error come from the electronics of the computer where when the computer compute an operation

the electronic fluctuations in the circuits give a random values in the memory places.

1.10.5.Human error

This error come from the operator which enter the data to the computer to do some calculations. So he

enters some wrong numbers.

5

Page 19: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Lecture.4

2.Simple Computation:

In this lecture we will learn the computation of the sum and product.

2.1.Sum Computation:

The sum mean to add an item number of times. And it gave by:

S=∑i=1

n

x

Program Analysis:

Inputs; xS=S+xOutputs; S

Algorithm:

READ x

for i=1 in loop 1..n

S=S+x

End loop i

PRINT S

1

Page 20: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Flowchart:

2

x

Start

I=1,n

S=S+x

i

S

End

Page 21: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Example:

To compute the sum of numbers 1.2, 3.1, 4.12, 2.3, 3.2, 1.23

Mat-lab Program:

%Sum of Numbersclc S=0;

x=[1.2,3.1,4.12,2.3,3.2,1.23];

for i=1:6

S=S+x(i);

end

S

3

Page 22: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

2.2.Product Computation:

The sum mean to multiply an item number of times. And it gave by:

P=∏i=1

n

x

Program Analysis:

Inputs; xP=P*xOutputs; P

Algorithm:

READ x

for i=1 in loop 1..n

P=P*x

End loop i

PRINT P

4

Page 23: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Flowchart:

5

x

Start

I=1,n

P=P*x

i

P

End

Page 24: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Example:

To compute the product of numbers 1.2, 3.1, 4.12, 2.3, 3.2, 1.23

Mat-lab Program:

%Product of Numbersclc P=1;

x=[1.2,3.1,4.12,2.3,3.2,1.23];

for i=1:6

P=P*x(i);

end

P

6

Page 25: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Lecture.5Function Computation

Linear function computing:

Example:

Compute the function: y (x )=3x+4 for any value of x.

Program analysis:

Inputs: x Formula: y(x)=3*x+4

Outputs: y(x)

Algorithm:

Read x

y(x)=3*x+4

Print y(x)

Flow chart:

1

Start

y(x)=3x+4

x

y(x)

End

Page 26: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Mat-Lab program:

To make the program we give x = 2

x=2;

yx=3*x+4;

yx

Graphing the function:

plot(r) to plot the variable r

xlabel('current') to make x-axies lable

ylabel('time') to make y-axies label

title('Fig') to make title

grid to make grid

2

Page 27: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Lecture.6

Solving the 1 st order equation.

Newton method:

The Newton method for solving 1st order equation is given by the formula:

x1=xo−f ( xo)f ( xo)

Where, xo is the guessed solution.

x1 is the computed solution

f (xo) is the value of function at xo

f (xo) is the value of function at xo

Example:

Solve the equation f(x)=2x-12 using the guessed value 7.

sol:

The guessed value xo = 7.0

at xo =7.0

f(xo)=2x-12

= 2*7-12

=14-12 =2

f (x )=2

1

Page 28: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

So the solution is given by:

x1=xo−f ( xo)f ( xo)

x1=7.0−22

x1=7.0−1=6

Mat-Lab program:

xo=7.0 fdxo=2.0 fxo=2*xo-12 x1=xo-(fxo/fdxo) x1

2

Page 29: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Lecture.7

Solving second order equation.

2 nd order equation:

a x2+b x+c=0

This equation can be solved numerically using the formula method.

Formula method:

The two roots of the equation are given by:

x1=−b+√(b2−4ac)

2a

x2=−b−√(b2−4ac)

2a

Example:

Write the mat lab program to solve the equation 3 x2+4x+12=0 using the formula method.

sol:

The algorithm:

Read a,b,c

x1=−b+√(b2−4∗a∗c)/2∗a

x2=−b−√(b2−4∗a∗c )/2∗a

Print x1,x2

1

Page 30: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Mat-lab program:

a=3;b=4;c=12;

x1=(-b+sqrt(b^2-4*a*c))/2*ax2=(-b-sqrt(b^2-4*a*c))/2*a

x1x2

2

Start

End

a,b,c

x1,x2

X1,x2

Page 31: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Lecture.8

Solving Linear Equation System.

Linear Equation System:

The general form :

a11 x1+a12 x2+a13 x3+.........+a1n xn=b1

…... ….... …... ….... …... … … …. ….. ..

am1 x1+am2 x2+am3 x3+.........+amn xn=bm

Where, a11, a12 , a13,. ...... , amn are the system coefficients. x1, x2 , x3,. ...... , xn are the system unknowns.

b1, b2 , b3,. ...... , bm Are the system constants.

Which have the general form : Ax=b

The numerical solution is given by :x=b/A

Algorithm:

Read A, b

x=b/A

Print x

1

Page 32: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Flowchart:

Example:Write a Mat Lab program to solve the linear system:

2x1 + 5x2 + 6x3 = 19 4x1 + 4x2 + 7x3 = 10 5x1 + 6x2 + 7x3 =3

Mat Lab program:

%solving Linear system% 2x1+5x2+6x3=19% 4x1+4x2+7x3=10% 5x1+6x2+7x3=3%-----------------clcA=[2,5,6; 4,4,7; 5,6,7];b=[19,10,3];'System Solution is:'x=b/A

2

Start

End

A,b

x

x=b/A

Page 33: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Lecture.9

Numerical Differentiation

Central Difference method:

for the function f (x ) the 1st derivative is given by:

f ' ( x)≈ f (h+x )− f (x−h)2h

And the 2nd derivative is given by:

f ' ' (x )≈f (h− x)−2f ( x)+ f ( x+h)

h2

Algorithm:

Read x,h,f(x)

fdx=(f(x+h)-f(x-h))/2*h)

Print fdx

Flowchart:

1

Start

End

x,h,f(x)

fdx

fdx

Page 34: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Example:

Write the Mat Lab program to find the 1st derivative f'(x) for the function f(x)=2x2 at x=4 consider that h=0.001.

Mat Lab Program:

clch=0.001;x=4;f=inline('2*x^2','x')fdx=((f(x+h)-f(x-h))/(2*h));fdxf_exact=4*x

2

Page 35: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Lecture.10

Numerical Integration

Trapezoidal Method:

For the definite integration of the function f(x) from a to b:

∫a

b

f (x )dx≈(b−a)f (a)+ f (b)

2

Algorithm:

Read a,b, f(x)

intfx=((b-a)*(f(a)+f(b)/2))

Print intfx

Flowchart:

1

Start

End

a,b,f(x)

intfx

intfx

Page 36: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Example:

Write the Mat Lab program to find the integration of the function f(x)=2x2 from a= 0 to b =1 using the trapezoidal method.

Mat Lab Program:

clc

a=0;b=1;f=inline('2*x^2','x');intfx=(b-a)*((f(a)+f(b))/2);intfx

2

Page 37: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Questions.1

Q1.Discuss in details the computing elements.

Q2.Draw the schematic diagram of a computer.

Q3.write down the steps of the program analysis.

Q4.what are the methods of program design.

Q5.Write short notes on the flowcharts.

Q6.State the difference between the DATA and INFORMATION.

Q7.Write short notes on Computer software model.

Q8.Write short notes on Rom programs.

Q9.Write short notes the mathematical softwares.

Q10.Write short notes on Operating System.

Q11.Classify the computer based on its size.

Q12.Classify the computer based on its output and input.

Q13.How to judge the good algorithm.

Q14.Discuss the computing errors.

Q15.Discuss the computer programming languages.

1

Page 38: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Problems.2

1-Write a Mat Lab Program to Find the sum: ∑=

=5

1

2

ixR for the x values from 1 to 5.

2-Write a Mat Lab Program to Find the product: ∏=

=5

1ixP for the x values from 1 to 5.

3-Write a Mat Lab program to find the solution of the 1st degree equation 0123 =−x using Newton

Method.

4-Draw a flowchart for computing the electrostatic potential given by: r

qkV 1*= consider

k=8.988 x 10-9 N m2 /C , q=6 mC, r= 20 m.

5- Analyse the program for computing the electrostatic field given by:

2

1*r

qkE =

Consider k=8.988e-9 N m2 /C , q=6 mC, r= 20 m.

6- Calculte using Matlab the current passing through a conductor given by:

I = n A q v

Consider n=2x1010 electron, v= 0.0029 m/s, q=1.602 x10-16 , A=2 x10-3 m2.

7- Draw a flowchart for Mat Lab program to differentiate the function f (x )=2 x2+4 at the point

x=3 using the central difference method.

8- Write a MatLab program for computing the function f(x)=2x+sin x for the range of x=[1,10].

1

Page 39: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

9- Write the algorithm for computing the integral for the function f(x)=sin x -3x from 0 to 3

10- Draw the flowchart and write the program for a MatLab program to compute the photons emitted

from an antenna in one second if the electromagnetic energy emitted by the antenna in one second

is E = 45000 J. using the formula:

υhEn 1=

Consider h=6.63 x 10 -34 J s, v=4 x 106 Hz.

2

Page 40: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Problems.2

1-Write a Mat Lab Program to Find the sum: ∑=

=5

1

2

ixR for the x values from 1 to 5.

A1:

Mat-lab program:

clc;

R=0;x=[1,2,3,4,5];for i=1:5;R=R+x(i);endR

2-Write a Mat Lab Program to Find the product: ∏=

=5

1ixP for the x values from 1 to 5.

A2:

Mat-lab program:

clc;

P=1;x=[1,2,3,4,5];for i=1:5;P=P*x(i);endP

3-Write a Mat Lab program to find the solution of the 1st degree equation 0123 =−x using Newton

Method.

A3:

clc

xo=1f=inline('3*x-12','x')fdx=3;x=xo-(f(xo)/fdx);x

1

Page 41: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

4-Draw a flowchart and write the Mat-lab program for computing the electrostatic potential given by:

rqkV 1*= consider: k=8.988 x 10-9 N m2 /C , q=6 m C, r= 20 m.

A4:

(a)Flowchart:

(b)Mat-lab program:

k=8.988e-9;

q=6e-6;

r=20;

v=k*q*(1/r);

v

2

start

k,r,q

v=k*q*(1/r)

v

end

Page 42: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

5- Analyze and write the program for computing the electrostatic field given by:2

1*r

qkE =

Consider k=8.988e-9 N m2 /C , q=6 mC, r= 20 m.

A5:

(a)Program Analysis:

Inputs: k,q,r

Formula: E=k*q(1/r*r)

Outputs: E

(b)Mat-lab program:

clc;

k=8.988e-9;

q=6e-6;

r=20;

E=k*q(1/r*r);

E

6- Write a Mat-lab program to compute the current passing through a conductor given by: I = n A q v

Consider n=2x1010 electron, v= 0.0029 m/s, q=1.602 x10-16 C , A=2 x10-3 m2.

A6:

clc

n=2e10;v=0.0029;q=1.602e-16;A=2e-3;I=n*A*q*v;I

3

Page 43: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

7- Write Mat-Lab program to differentiate the function f (x )=2 x2+ 4 at the point x=3 using the

central difference method. Use h=0.001.

A7:

Mat-lab program:

clch=0.001;x=3;f=inline('2*x^2+4','x')fdx=((f(x+h)-f(x-h))/(2*h));fdx

8- Write a Mat-lab program for computing and draw the function f(x)=2x+4sin x for the range of x=[1,10].

A8:

Mat-lab program:

clc

x=1:10;

fx=2*x+4*sin(x);

fx

plot(fx,'d-')

4

Page 44: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

9- Write the algorithm and Mat-lab program for computing the integral for the function f(x)=sin x -3x from 0 to 3.A9:

(a)Algorithm:

Read a,b,f(x)

intf=(b-a)*((f(a)+f(b))/2)

Print intf

(b)Mat-lab program:

clc

a=0;b=3;f=inline('sin(x)-3*x','x');intf=(b-a)*((f(a)+f(b))/2);intf

10- Write the program for a Mat-lab program to compute the photons emitted from an antenna in one

second if the electromagnetic energy emitted by the antenna in one second is E = 45000 J. using

the formula: υh

En 1= Consider h=6.63 x 10 -34 J s, v=4 x 106 Hz.

A10:

clc

E=45000;h=6.63e-34;v=4e6;n=E*(1/(h*v));n

5

Page 45: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Laboratory 4050 Phys.

Lab.1:

Introduction to computing:

(a) Mat-lab setup on the Laptops

(b) Some simple computing [ adding - subtracting -Multiplying - Dividing]

(b) To understand Computing process using the Mat-lab direct programming.

Lab.2:

Simple physics problems

(a)Mathematics

(b)One dimension mechanics

(c)Coulomb force

Lab.3:

Some more complicated problems

(a)Projectiles.

(a) Moon Problem.

(b) Newton Apple.

(c) Nuclear Radiation.

Lab.4:

Computing sum and product.

(a)Sum examples.

(b)Product examples.

Lab.5:

Computing Functions

(a)Computing linear functions.

(b)Computing nonlinear functions.

(c)Functions graphs.

Lab.6:

Solving 1 st degree equations.

(a)Newton method

1

Page 46: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

Dr. A. E. Kotp - Lecture notes 4050 Phys.

Lab.7:

Solving 2 nd degree equations.

(a)Formula method

Lab.8:

Linear algebra

(a)Vectors.

(b)Matrices.

(c)Tensors.

Lab.9:

Numerical differentiation.

(a)Forward method.

(b)Backward method.

(c)Center method.

Lab.10:

Numerical integration.

(a)Rectangle method.

(b)Trapezoidal method.

Lab.11:

Numerical Ordinary Differential Equation solving.

(a) Euler method.

(b) Runge-Kutta method.

2

Page 47: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

FreeMat Basic CommandsFreeMat Basic CommandsFreeMat Basic CommandsFreeMat Basic Commands

clear :clear :clear :clear :clc to erase the consolclc to erase the consolclc to erase the consolclc to erase the consolclf to clear the graph windowclf to clear the graph windowclf to clear the graph windowclf to clear the graph windowcla to clear the drawcla to clear the drawcla to clear the drawcla to clear the draw------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------mathmathmathmath::::

3+4 to add 3 and 4 3+4 to add 3 and 4 3+4 to add 3 and 4 3+4 to add 3 and 4 40404040----3 to substract 3 from 403 to substract 3 from 403 to substract 3 from 403 to substract 3 from 402*3 to multiply 2 by 32*3 to multiply 2 by 32*3 to multiply 2 by 32*3 to multiply 2 by 312/4 to devide 12 by 412/4 to devide 12 by 412/4 to devide 12 by 412/4 to devide 12 by 4------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Graph:Graph:Graph:Graph:

plot(r) to plot the variable rplot(r) to plot the variable rplot(r) to plot the variable rplot(r) to plot the variable r xlabel('current') to make x xlabel('current') to make x xlabel('current') to make x xlabel('current') to make x----axies lableaxies lableaxies lableaxies lableylabel('time') to make yylabel('time') to make yylabel('time') to make yylabel('time') to make y----axies labelaxies labelaxies labelaxies label title('Fig') to make title title('Fig') to make title title('Fig') to make title title('Fig') to make titlegrid to make gridgrid to make gridgrid to make gridgrid to make grid'b' 'b' 'b' 'b' ---- Color Blue Color Blue Color Blue Color Blue 'g' 'g' 'g' 'g' ---- Color Green Color Green Color Green Color Green 'r' 'r' 'r' 'r' ---- Color Red Color Red Color Red Color Red 'c' 'c' 'c' 'c' ---- Color Cyan Color Cyan Color Cyan Color Cyan 'm' 'm' 'm' 'm' ---- Color Magenta Color Magenta Color Magenta Color Magenta 'y' 'y' 'y' 'y' ---- Color Yellow Color Yellow Color Yellow Color Yellow 'k' 'k' 'k' 'k' ---- Color Black Color Black Color Black Color Black The symbolspec specifies the (optional) symbol to be drawn at each data point: The symbolspec specifies the (optional) symbol to be drawn at each data point: The symbolspec specifies the (optional) symbol to be drawn at each data point: The symbolspec specifies the (optional) symbol to be drawn at each data point: '.' '.' '.' '.' ---- Dot symbol Dot symbol Dot symbol Dot symbol 'o' 'o' 'o' 'o' ---- Circle symbol Circle symbol Circle symbol Circle symbol 'x' 'x' 'x' 'x' ---- Times symbol Times symbol Times symbol Times symbol '+' '+' '+' '+' ---- Plus symbol Plus symbol Plus symbol Plus symbol '*' '*' '*' '*' ---- Asterisk symbol Asterisk symbol Asterisk symbol Asterisk symbol 's' 's' 's' 's' ---- Square symbol Square symbol Square symbol Square symbol

1

Page 48: Lecture Notes on Computational Physics Dr. A.E€¦ · Dr. A. E. Kotp - Lecture notes 4050 Phys. 7.Numerical Solution of linear System of Equations 7.1.Marix-vector method 8.Numerical

'd' 'd' 'd' 'd' ---- Diamond symbol Diamond symbol Diamond symbol Diamond symbol 'v' 'v' 'v' 'v' ---- Downward Downward Downward Downward----pointing triangle symbol pointing triangle symbol pointing triangle symbol pointing triangle symbol '^' '^' '^' '^' ---- Upward Upward Upward Upward----pointing triangle symbol pointing triangle symbol pointing triangle symbol pointing triangle symbol '<' '<' '<' '<' ---- Left Left Left Left----pointing triangle symbol pointing triangle symbol pointing triangle symbol pointing triangle symbol '>' '>' '>' '>' ---- Right Right Right Right----pointing triangle symbol pointing triangle symbol pointing triangle symbol pointing triangle symbol The linestylespec specifies the (optional) line style to use for each data series: The linestylespec specifies the (optional) line style to use for each data series: The linestylespec specifies the (optional) line style to use for each data series: The linestylespec specifies the (optional) line style to use for each data series: ''''----' ' ' ' ---- Solid line style Solid line style Solid line style Solid line style ':' ':' ':' ':' ---- Dotted line style Dotted line style Dotted line style Dotted line style ''''----.' .' .' .' ---- Dot Dot Dot Dot----DashDashDashDash----DotDotDotDot----Dash line style Dash line style Dash line style Dash line style ''''--------' ' ' ' ---- Dashed line style Dashed line style Dashed line style Dashed line style ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------variables:variables:variables:variables:who to check the vars who to check the vars who to check the vars who to check the vars clear : to clear the vars.clear : to clear the vars.clear : to clear the vars.clear : to clear the vars.------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------n=12 to assing variablen=12 to assing variablen=12 to assing variablen=12 to assing variablea=1:0.2:10 to make variabe from 1 to 10 with step 0.2 a=1:0.2:10 to make variabe from 1 to 10 with step 0.2 a=1:0.2:10 to make variabe from 1 to 10 with step 0.2 a=1:0.2:10 to make variabe from 1 to 10 with step 0.2 b=[1,2,3,4,5,3] to make vector variableb=[1,2,3,4,5,3] to make vector variableb=[1,2,3,4,5,3] to make vector variableb=[1,2,3,4,5,3] to make vector variablec=[1,2,3;4,5,6;7,8,9] to make matrix 3x3 c=[1,2,3;4,5,6;7,8,9] to make matrix 3x3 c=[1,2,3;4,5,6;7,8,9] to make matrix 3x3 c=[1,2,3;4,5,6;7,8,9] to make matrix 3x3 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------loop:loop:loop:loop:for i=1:0.5:10 for i=1:0.5:10 for i=1:0.5:10 for i=1:0.5:10 endendendend--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Dr.A.E.KotpDr.A.E.KotpDr.A.E.KotpDr.A.E.Kotp------------------------------------------------------------------------------------

2