Computational language have been used in physics research
for many years and there is a plethora of programs and packages on the Web which can be used to solve dierent problems. In this report I trying to use as many of these available solutions as possible and not reinvent the wheel. Some of these packages have been written in C program. As I stated above, physics relies heavily on graphical representations. Usually,the scientist would save the results
from some calculations into a file, which then can be read and used for display by a graphics package like Gnuplot.
1. A Report on Computational Physics
By
Yagya Dev Bhardwaj
Int. M.Sc B.Ed Physics
4th Sem 2015IMSBPH023
Submitted to
Dr. Rakesh Kumar
Assistant Professor
Department of Physics
Central University Of Rajasthan
3. 1 Introduction
Computational physics provides a means to solve complex numerical problems.In
itself it will not give any insight into a problem (after all, a computer is only as
intelligent as its user), but it will enable you to attack problems which otherwise
might not be solvable. A typical introductory physics problem is to calculate the
motion of a cannon ball in two dimensions. This problem is always treated
without air resistance. One of the difficulties of physics is that the moment one
goes away from such an idealized system, the task rapidly becomes rather
complicated. If we want to calculate the solution with real-world elements things
become rather difficult. A way out of this mess is to use the methods of
computational physics to solve this linear differential equation.
Physics is deeply connected to mathematics and
requires a lot of calculation skills. If one is only interested in a conceptual
understanding of the field, or an estimate of the outcome of an experiment,
simple calculus will probably suffice. We can solve the problem of a cannon ball
without air resistance or Coriolis force with very elementary math, but once we
include these effects, the solution becomes quite a bit more complicated. Physics,
being an experimental science, also requires that the measured results are
statistically significant, meaning we have to repeat an experiment several times,
necessitating the same calculation over and over again and comparing the results.
This then leads to the question of how to present your results. It is much easier
to determine the compatibility of data points from a graph, rather than to try to
compare say 1000 numbers with each other and determine whether there is a
significant deviation. Computational language have been used in physics research
for many years and there is a plethora of programs and packages on the Web
which can be used to solve different problems. In this report I trying to use as
many of these available solutions as possible and not reinvent the wheel. Some of
these packages have been written in C program. As I stated above, physics relies
heavily on graphical representations. Usually,the scientist would save the results
from some calculations into a file, which then can be read and used for display by
a graphics package like gnuplot.
2 Root Finding Method
The roots of a quadratic polynomial ax2
+ bx + c with a = 0 are given
by the formula
−b ±
√
b2 − 4ac
2a
The problem of finding the roots of a quadratic equation is a particular case of nonlinear
equations f(x) = 0. The function f(x) can be a polynomial, transcendental, or a combination
of different functions, like f(x) = exp(x)+log(x)−cos(x). In science we encounter many forms
of nonlinear equations besides the quadratic ones. As a rule, it is difficult or not feasible to
find analytic solutions. A few lines of computer code can find numerical solutions instantly.
You may already have some experience with solving nonlinear equations with a programmable
graphical calculator. In the following, let f(x) be a function of a single real variable x, and we
will look for a real root on an interval [a,b].
3
4. 2.1 Newton Raphson Method
This method is also called Newton’s method. This method is also a chord
method in which we approximate the curve near a root, by a straight line. Let x0 be an initial
approximation to the root of f(x) = 0. Then, (x0, f0), where f0 = f(x0), is a point on the
curve. Draw the tangent to the curve . We approximate the curve in the neighborhood of the
root by the tangent to the curve at the point. The point of intersection of the tangent with
the x-axis is taken as the next approximation to the root. The process is repeated until the
required accuracy is obtained. The equation of the tangent to the curve y = f(x) at the point
(x0, f0) is given by
y − f(x0) = (x − x0)f (x0)
where f (x0) is the slope of the tangent to the curve . Setting y = 0 and solving for x, we get
x = x0 −
f(x0)
f (x0)
, f (x0) = 0
The next approximation to the root is given by
x1 = x0 −
f(x0)
f (x0)
, f (x0) = 0
We repeat the procedure. The iteration method is defined as
xn+1 = xn −
f(xn)
f (xn)
, f (n0) = 0
This method is called the Newton-Raphson method or simply the Newton’s method. The
method is also called the tangent method.
PROGRAMMING CODE
#include<stdio.h>
#include<math.h>
float f(float x)
4
5. {
return x*log10(x) - 1.2;
}
float df (float x)
{
return log10(x) + 0.43429;
}
int main()
{
int i, max;
float h, x0, x1, error;
printf("nEnter x0, allowed error and maximum iterationsn");
scanf("%f %f %d", &x0, &error, &max);
for (i=1; i<=max; i++)
{
h=f(x0)/df(x0);
x1=x0-h;
printf(" At Iteration no. %3d, x = %9.6fn", i, x1);
if (fabs(h) < error)
{
printf("After %3d iterations, root = %8.6fn", i, x1);
return 0;
}
x0=x1;
}
printf(" The required solution does not converge or iterations are insufficientn");
return 1;
}
2.2 Bisection Method
The Bisection method is the simplest but most robust method. This
method never fails. Let f(x) be a continuous function on [a b] that changes sign between x
= a and x = b, i.e. f(a)f(b) < 0. In this case there is at least one real root on the interval
[a ,b]. The bisectional procedure begins with dividing the initial interval [a b] into two equal
intervals with the middle point at x1 = (a − b)/2 There are three possible cases for the product
of f(a)f(x1)
f(a)f(x1) =
< 0 there is a root[a, x1];
> 0 there is a root[x1, b];
= 0 then x1 is a root.
We can repeat the bisectional procedure for a new interval where the function f(x) has a root.
On each bisectional step we reduce by two the interval where the solution occurs. After n steps
the original interval will be reduced to the interval(b − a)/2n
. The bisectional procedure is
repeated until (b − a)/2n
is less than the given tolerance.
5
7. printf("the interval vales are incorrect");
}
float fn(float x)
{
float f;
f=(x*x)-1;
return f;
}
3 Integration Method
The need often arises for evaluating the definite integral of a function
that has no explicit antiderivative or whose antiderivative is not easy to obtain. The basic
method involved in approximating
b
a
f(x) dx.
is called numerical quadrature. It uses a sum n
i=1 aif(xi) to approximate
b
a
f(x) dx.
3.1 Trapezoidal Method
This rule is also called the trapezoidal rule. Let the curve y = f(x), a x
b, be approximated by the line joining the points (a, f(a)), (b, f(b)) on the curve. Using the
Newton’s forward difference formula, the linear polynomial approximation to f(x)
7
8. interpolating at the points (a, f(a)), (b, f(b)), is given by
f(x) = f(x0) +
1
h
f(x0), f (x0) = 0
I =
b
a
f(x), dx.
x0+nh
x0
f(x)dx=h
2
[(y0 + yn) + 2(y1 + y2 + ......... + yn−2)]
PROGRAMMING CODE FOR
I =
b
a
x2
dx.
#include<stdio.h>
float fn(float);
void main()
{
float a,b,n,r,t,h,s=0;
int i;
printf("enter the value of limitsn");
printf("enter the lower limit a=n");
scanf("%f",&b);
printf("enter the upper limit b=n");
scanf("%f",&b);
printf("number of steps n=n");
scanf("%f",&n);
h=(b-a)/n;
for(i=1;i<=(n-1);i++)
{
fn(a+i*h);
t=fn(a+(i*h));
s=s+t;
printf("%fn",t);
}
r=(h/2)*(fn(a)+(2*s)+fn(b));
printf("%ft%ft%ft%fn",h,fn(a),fn(b),s);
printf("result of integration is=%fn",r);
}
float fn(float x)
{
float f;
f=(x*x);
return f;
}
3.2 Simpson’s 1/3 Method
The trapezoidal rule was based on approximating the integrand by a first
order polynomial, and then integrating the polynomial over interval of integration.
8
9. Simpson’s 1/3 rule is an extension of Trapezoidal rule where the integrand
is approximated by a second order polynomial Just like in multiple-segment trapezoidal rule,
one can subdivide the interval [a,b] into n segments and apply Simpson’s 1/3 rule repeatedly
over every two segments. Divide interval [a,b] into n equal segments, so that the segment width
is given by General form for the integration using this method is as followed
x0+nh
x0
f(x)dx=h
3
[(y0 + yn) + 4(y1 + y3 + ....... + yn−1 + 2(y2 + y4 + ..... + yn−2)]
PROGRAMMING CODE
#include<stdio.h>
#include<math.h>
int main()
{
float x[10],y[10],sum=0,h,temp;
int i,n,j,k=0;
float fact(int);
printf("nhow many record you will be enter: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("nnenter the value of x%d: ",i);
scanf("%f",&x[i]);
printf("nnenter the value of f(x%d): ",i);
scanf("%f",&y[i]);
}
h=x[1]-x[0];
n=n-1;
sum = sum + y[0];
for(i=1;i<n;i++)
{
if(k==0)
{
sum = sum + 4 * y[i];
k=1;
}
9
10. else
{
sum = sum + 2 * y[i];
k=0;
}
}
sum = sum + y[i];
sum = sum * (h/3);
printf("nn I = %f ",sum);
}
3.3 Simpson’s 3/8 Method
The Approximate Int(f(x), x = a..b, method = simpson[3/8], opts) com-
mand approximates the integral of f(x) from a to b by using Simpson’s 3/8 rule. This rule is
also known as Newton’s 3/8 rule. The first two arguments (function expression and range) can
be replaced by a definite integral.
x0+nh
x0
f(x)dx=3h
8
[(y0 + yn) + 3(y1 + y2 + y4 + y5 + .... + yn−1) + 2(y3 + y6 + ....yn−3)]
While using this method the number of sub intervals should be taken as multiple of 3.
PROGRAMMING CODE FOR
I =
b
a
e−x2
dx.
#include<stdio.h>
#include<math.h>
float fn(float);
int main()
{
int i,n;
float sum1,sum2,sum3,I,h,b,a,y[100],x[100];
printf("write the value of nn");
scanf("%d",&n);
10
11. printf("enter the value of b or upper limitn");
scanf("%f",&b);
printf("enter the value of a or lower limitn");
scanf("%f",&a);
h= (b-a)/n ;
x[0]=a;
y[0]=F(x[0]);
x[n]=b;
y[n]=F(x[n]);
printf("x t f(x)n");
printf("%f t %fn",x[0],y[0]);
for(i=1;i<n;i++)
{
x[i]= x[i-1]+ h;
y[i]= F(x[i]);
printf("%f t %fn n",x[i],y[i]);
}
printf("%f t %fn",x[n],y[n]);
sum1=0;
for(i=1;i<n;i=i+3)
{
sum1 = sum1 +3*y[i];
}
sum2=0;
for(i=2;i<n;i=i+3)
{
sum2 = sum2 +3*y[i];
}
sum3=0;
for(i=3;i<n;i=i+3)
{
sum3 = sum2 + 2*y[i];
}
I= ((3*h)*(y[n]+y[0]+sum1+sum2+sum3))/8 ;
printf("value of integration of given by = %f",I);
}
float fn(float x)
{
float f;
f= exp(-(x*x));
return f;
}
Question-Evaluate
6
0
1
1+x2 dx by using
1. Trapezoidal Rule,
2. Simpson 1/3 Rule,
11
12. 3.Simpson 3/8 Rule
solution Divide the interval (0,6)into six parts each of width h=1.The values of f(x) = 1
1+x2
are given below
x f(x)
0 1
1 0.5
2 0.2
3 0.1
4 0.0588
5 0.0385
6 0.027
1. By Trapezoidal rule,
6
0
1
1+x2 dx = h
2
[(y0 + y6) + 2(y1 + y2 + y3 + y4 + y5]
=6
2
[(1 + 0.027) + 2(0.5 + 0.2 + 0.1 + 0.0588 + 0.0385)]
=1.4108
2. By Simpson 1/3 Rule
6
0
1
1+x2 dx = h
3
[(y0 + y6) + 4(y1 + y3 + y5) + 2(y2 + y4)]
=6
3
[(1 + 0.027) + 4(0.5 + 0.1 + 0.0385) + 2(0.2 + 0.0588)]
=1.3662
3.By Simpson 3/8 Rule
6
0
1
1+x2 dx = 3h
8
[(y0 + y6) + 3(y1 + y2 + y4 + y5) + 2y3]
=3
8
[(1 + 0.027) + 3(0.5 + 0.2 + 0.0588 + 0.0385) + 2(0.1)]
=1.3571
4 Differentiation Method
Numerical differentiation deals with the following problem:we are given
the function y = f(x) and wish to obtain one of its derivatives at the point x = xk. The
term“given” means that we either have an algorithm for computing the function, or possess a
set of discrete data points (xi , yi ), i = 0, 1, . . . , n. In either case, we have access to a finite
number of (x, y) data pairs from which to compute the derivative. Numerical differentiation is
related to interpolation,means of finding the derivative is to approximate the function locally
by a polynomial and then differentiate it.
An equally effective tool is the Taylor series expansion of f(x) about the point
xk, which has the advantage of providing us with information about the error involved in the
approximation. Numerical differentiation is not a particularly accurate process.
The derivative of the function f at x0 is
f (x0) = lim
h→0
f(x0 + h) − f(x0)
h
12
13. This formula gives an obvious way to generate an approximation to f(x0); simply compute
f(x0 + h) − f(x0)
h
for small values of h.
4.1 Euler’s Method
In mathematics and computational science, the Euler method is a first-
order numerical procedure for solving ordinary differential equations (ODEs) with a given initial
value. It is the most basic explicit method for numerical integration of ordinary differential
equations.
This can be described as a technique of developing a piecewise linear approximation to the
solution.In the initial value problem .the starting point of the solution curve and the slope of
the curve at the starting point are given.With this information the method extrapolates the
solution curve using the specified step size.
Example’s:-
1. Simple Harmonic Motion
Equation of motion are:
d2
x
dt2
= −kx (1)
y =
dx
dt
(2)
dy
dt
= −kx (3)
PROGRAMMING CODE
#include<stdio.h>
void main()
{
float x0,y0,h,k,x,y,i;
x0=0.1; //intialisation
y0=0.2;
h=0.001;
k=1;
FILE*fp;
fp=fopen("SHM.dat","w");
for(i=0;i<=50000;i++)
{
x=x0+h*y0;
y=y0+h*(-k)*x0;
x0=x;
y0=y;
13