SlideShare a Scribd company logo
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
Contents
1 Introduction 3
2 Root Finding Method 3
2.1 Newton Raphson Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2 Bisection Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3 Integration Method 7
3.1 Trapezoidal Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 Simpson’s 1/3 Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.3 Simpson’s 3/8 Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4 Differentiation Method 12
4.1 Euler’s Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2
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
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
{
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
PROGRAMMING CODE FOR BISECTION METHOD
#include<stdio.h>
float fn(float);
void main()
{
float a,b,c;
int i;
printf("enter the initial root valuesn");
scanf("%ft%f",&a,&b);
if(fn(a)*fn(b)<0)
{
for(i=0;i<=20;i++)
{
c=(a+b)/2;
if(fn(c)==0)
{
printf("the root is=%f",c);
break;
}
else
{
if(fn(a)*fn(c)<0)
b=c;
else
a=c;
}
printf("%fn",c);
}
}
else
6
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
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
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
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
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
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
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
fprintf(fp,"%ft%ft%fn",i,x,y);
}
fclose(fp);
}
-0.25
-0.2
-0.15
-0.1
-0.05
0
0.05
0.1
0.15
0.2
0.25
-0.25 -0.2 -0.15 -0.1 -0.05 0 0.05 0.1 0.15 0.2 0.25
y
x
"SHM.dat" us 2:3
2. The Lorenz Attractor
Equation’s of Motion are:-
dx
dt
= σ(y − x) (4)
dy
dt
= (r − z)(x − y) (5)
dz
dt
= (xy − bz) (6)
PROGRAMMING CODE
#include<stdio.h>
void main()
{
float x0,y0,z0,h,x,y,z,r,a,b,i;
x0=0.02; //intialisation
14
y0=0.3;
z0=0.2;
h=0.01;
a=10;
b=2.66;
r=28;
FILE*fp;
fp=fopen("lorenz.dat","wS");
for(i=0;i<=10000;i++)
{
x=x0+h*(a*(y0-x0));
y=y0+h*((r-z0)*x0-y0);
z=z0+h*(x0*y0-b*z0);
x0=x;
y0=y;
z0=z;
fprintf(fp,"%ft%ft%ft%fn",i,x,y,z);
}
fclose(fp);
}
15
-20 -15 -10 -5 0 5 10 15 20 25-30
-20
-10
0
10
20
30
0
10
20
30
40
50
60
z
lorenz attractor
"lorenz.dat" us 2:3:4
x
y
z
16

More Related Content

What's hot (19)

PPTX
probability assignment help (2)
Statistics Homework Helper
 
PPTX
Statistics Assignment Help
Statistics Assignment Help
 
PPTX
Diffusion Homework Help
Statistics Assignment Help
 
PDF
Chapter 2 solving nonlinear equations
ssuser53ee01
 
PPTX
Newton cotes integration method
shashikant pabari
 
PPTX
Mechanical Engineering Assignment Help
Matlab Assignment Experts
 
PPT
Applications of numerical methods
Tarun Gehlot
 
PDF
Ch2 probability and random variables pg 81
Prateek Omer
 
PPT
Regulafalsi_bydinesh
Dinesh Kumar
 
PDF
Double Robustness: Theory and Applications with Missing Data
Lu Mao
 
PPT
Numerical method
Kumar Gaurav
 
PPTX
Signal Processing Assignment Help
Matlab Assignment Experts
 
PDF
Iteration method-Solution of algebraic and Transcendental Equations.
AmitKumar8151
 
PPTX
Numerical solutions of algebraic equations
Avneet Singh Lal
 
PDF
Numerical
1821986
 
PDF
OPERATIONS RESEARCH
Makaha Rutendo
 
PPTX
Signal Processing Homework Help
Matlab Assignment Experts
 
PPTX
Digital Signal Processing Assignment Help
Matlab Assignment Experts
 
probability assignment help (2)
Statistics Homework Helper
 
Statistics Assignment Help
Statistics Assignment Help
 
Diffusion Homework Help
Statistics Assignment Help
 
Chapter 2 solving nonlinear equations
ssuser53ee01
 
Newton cotes integration method
shashikant pabari
 
Mechanical Engineering Assignment Help
Matlab Assignment Experts
 
Applications of numerical methods
Tarun Gehlot
 
Ch2 probability and random variables pg 81
Prateek Omer
 
Regulafalsi_bydinesh
Dinesh Kumar
 
Double Robustness: Theory and Applications with Missing Data
Lu Mao
 
Numerical method
Kumar Gaurav
 
Signal Processing Assignment Help
Matlab Assignment Experts
 
Iteration method-Solution of algebraic and Transcendental Equations.
AmitKumar8151
 
Numerical solutions of algebraic equations
Avneet Singh Lal
 
Numerical
1821986
 
OPERATIONS RESEARCH
Makaha Rutendo
 
Signal Processing Homework Help
Matlab Assignment Experts
 
Digital Signal Processing Assignment Help
Matlab Assignment Experts
 

Similar to Numerical differentation with c (20)

PDF
Mit18 330 s12_chapter4
CAALAAA
 
PDF
Introduction to comp.physics ch 3.pdf
JifarRaya
 
PDF
Algorithm of some numerical /computational methods
Chandan
 
PDF
Numerical Method Analysis: Algebraic and Transcendental Equations (Non-Linear)
Minhas Kamal
 
PDF
1.pdf
Dhiraj Bhaskar
 
PPTX
Presentation on application of numerical method in our life
Manish Kumar Singh
 
PPT
Newton Raphson Method.ppt
UmarSaba1
 
PPT
Newton raphson method
Bijay Mishra
 
PDF
Best of numerical
CAALAAA
 
PDF
104newton solution
VictorQuezada38
 
PPTX
Solution to Non-Linear Problems for Applied Numerical
mohdnuradi
 
PDF
James_F_Epperson_An_Introduction_to_Numerical_Methods_and_Analysis.pdf
FahimSiddiquee2
 
PPTX
Finding root of equation (numarical method)
Rajan Thakkar
 
PDF
Numerical Analysis
Mallela Niteesh Kumar Reddy
 
PDF
Numerical Computing
Er. Shiva K. Shrestha
 
DOCX
MCA_UNIT-1_Computer Oriented Numerical Statistical Methods
Rai University
 
DOCX
Roots of equations
Mileacre
 
DOCX
Equations root
Mileacre
 
PDF
Lecture_06_roots_English.pdf
JejyBorres
 
Mit18 330 s12_chapter4
CAALAAA
 
Introduction to comp.physics ch 3.pdf
JifarRaya
 
Algorithm of some numerical /computational methods
Chandan
 
Numerical Method Analysis: Algebraic and Transcendental Equations (Non-Linear)
Minhas Kamal
 
Presentation on application of numerical method in our life
Manish Kumar Singh
 
Newton Raphson Method.ppt
UmarSaba1
 
Newton raphson method
Bijay Mishra
 
Best of numerical
CAALAAA
 
104newton solution
VictorQuezada38
 
Solution to Non-Linear Problems for Applied Numerical
mohdnuradi
 
James_F_Epperson_An_Introduction_to_Numerical_Methods_and_Analysis.pdf
FahimSiddiquee2
 
Finding root of equation (numarical method)
Rajan Thakkar
 
Numerical Analysis
Mallela Niteesh Kumar Reddy
 
Numerical Computing
Er. Shiva K. Shrestha
 
MCA_UNIT-1_Computer Oriented Numerical Statistical Methods
Rai University
 
Roots of equations
Mileacre
 
Equations root
Mileacre
 
Lecture_06_roots_English.pdf
JejyBorres
 
Ad

Recently uploaded (20)

PPTX
Structure and uses of DDT, Saccharin..pptx
harsimrankaur204
 
PDF
Continuous Model-Based Engineering of Software-Intensive Systems: Approaches,...
Hugo Bruneliere
 
PPTX
Hypothalamus_nuclei_ structure_functions.pptx
muralinath2
 
PDF
Insect Behaviour : Patterns And Determinants
SheikhArshaqAreeb
 
PPTX
Pratik inorganic chemistry silicon based ppt
akshaythaker18
 
PPTX
Animal Reproductive Behaviors Quiz Presentation in Maroon Brown Flat Graphic ...
LynetteGaniron1
 
PDF
Carbon-richDustInjectedintotheInterstellarMediumbyGalacticWCBinaries Survives...
Sérgio Sacani
 
PPTX
Vectors and applications of genetic engineering Pptx
Ashwini I Chuncha
 
PPTX
Diagnostic Features of Common Oral Ulcerative Lesions.pptx
Dr Palak borade
 
PPTX
Diuretic Medicinal Chemistry II Unit II.pptx
Dhanashri Dupade
 
PDF
NRRM 330 Dynamic Equlibrium Presentation
Rowan Sales
 
PDF
Pharma Part 1.pdf #pharmacology #pharmacology
hikmatyt01
 
PPTX
Immunopharmaceuticals and microbial Application
xxkaira1
 
PDF
Adding Geochemistry To Understand Recharge Areas - Kinney County, Texas - Jim...
Texas Alliance of Groundwater Districts
 
PDF
A young gas giant and hidden substructures in a protoplanetary disk
Sérgio Sacani
 
PPTX
MICROBIOLOGY PART-1 INTRODUCTION .pptx
Mohit Kumar
 
PPTX
Cooking Oil Tester How to Measure Quality of Frying Oil.pptx
M-Kube Enterprise
 
PPTX
LESSON 2 PSYCHOSOCIAL DEVELOPMENT.pptx L
JeanCarolColico1
 
PPT
Cell cycle,cell cycle checkpoint and control
DrMukeshRameshPimpli
 
PDF
Phosphates reveal high pH ocean water on Enceladus
Sérgio Sacani
 
Structure and uses of DDT, Saccharin..pptx
harsimrankaur204
 
Continuous Model-Based Engineering of Software-Intensive Systems: Approaches,...
Hugo Bruneliere
 
Hypothalamus_nuclei_ structure_functions.pptx
muralinath2
 
Insect Behaviour : Patterns And Determinants
SheikhArshaqAreeb
 
Pratik inorganic chemistry silicon based ppt
akshaythaker18
 
Animal Reproductive Behaviors Quiz Presentation in Maroon Brown Flat Graphic ...
LynetteGaniron1
 
Carbon-richDustInjectedintotheInterstellarMediumbyGalacticWCBinaries Survives...
Sérgio Sacani
 
Vectors and applications of genetic engineering Pptx
Ashwini I Chuncha
 
Diagnostic Features of Common Oral Ulcerative Lesions.pptx
Dr Palak borade
 
Diuretic Medicinal Chemistry II Unit II.pptx
Dhanashri Dupade
 
NRRM 330 Dynamic Equlibrium Presentation
Rowan Sales
 
Pharma Part 1.pdf #pharmacology #pharmacology
hikmatyt01
 
Immunopharmaceuticals and microbial Application
xxkaira1
 
Adding Geochemistry To Understand Recharge Areas - Kinney County, Texas - Jim...
Texas Alliance of Groundwater Districts
 
A young gas giant and hidden substructures in a protoplanetary disk
Sérgio Sacani
 
MICROBIOLOGY PART-1 INTRODUCTION .pptx
Mohit Kumar
 
Cooking Oil Tester How to Measure Quality of Frying Oil.pptx
M-Kube Enterprise
 
LESSON 2 PSYCHOSOCIAL DEVELOPMENT.pptx L
JeanCarolColico1
 
Cell cycle,cell cycle checkpoint and control
DrMukeshRameshPimpli
 
Phosphates reveal high pH ocean water on Enceladus
Sérgio Sacani
 
Ad

Numerical differentation with c

  • 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
  • 2. Contents 1 Introduction 3 2 Root Finding Method 3 2.1 Newton Raphson Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2.2 Bisection Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3 Integration Method 7 3.1 Trapezoidal Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3.2 Simpson’s 1/3 Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 3.3 Simpson’s 3/8 Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 4 Differentiation Method 12 4.1 Euler’s Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2
  • 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
  • 6. PROGRAMMING CODE FOR BISECTION METHOD #include<stdio.h> float fn(float); void main() { float a,b,c; int i; printf("enter the initial root valuesn"); scanf("%ft%f",&a,&b); if(fn(a)*fn(b)<0) { for(i=0;i<=20;i++) { c=(a+b)/2; if(fn(c)==0) { printf("the root is=%f",c); break; } else { if(fn(a)*fn(c)<0) b=c; else a=c; } printf("%fn",c); } } else 6
  • 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
  • 14. fprintf(fp,"%ft%ft%fn",i,x,y); } fclose(fp); } -0.25 -0.2 -0.15 -0.1 -0.05 0 0.05 0.1 0.15 0.2 0.25 -0.25 -0.2 -0.15 -0.1 -0.05 0 0.05 0.1 0.15 0.2 0.25 y x "SHM.dat" us 2:3 2. The Lorenz Attractor Equation’s of Motion are:- dx dt = σ(y − x) (4) dy dt = (r − z)(x − y) (5) dz dt = (xy − bz) (6) PROGRAMMING CODE #include<stdio.h> void main() { float x0,y0,z0,h,x,y,z,r,a,b,i; x0=0.02; //intialisation 14
  • 16. -20 -15 -10 -5 0 5 10 15 20 25-30 -20 -10 0 10 20 30 0 10 20 30 40 50 60 z lorenz attractor "lorenz.dat" us 2:3:4 x y z 16