0% found this document useful (0 votes)
14 views

Computational Techniques

Uploaded by

meihuunkoosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Computational Techniques

Uploaded by

meihuunkoosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Mathematical Physics 1 (Core Lab)

paper code: 32221101

Submitted by
Ankush Kumar (2007010)
Exam Roll no: 20009567010
B.Sc. Physics (Hons) I sem
Mathematical Physics practical

To
Dr Harender Kumar sir
Ques1: To find value of pi using Monte Carlo simulations

Objective: To find the value of pi


Theory: The idea is to simulate random (x, y) points in a 2-D plane
with domain as a square of side 1 unit. Imagine a circle inside the
same domain with same diameter inscribed into the square. We then
calculate the ratio of number points that lied inside the circle and
the total number of the generated points. We then calculate,
area of the circle no.of points genrated inside the circle
=
area of the square no. of points generated inside the square

Then, check if, x2 + y2 <= 1, if yes, we increment the number of


points. By doing so we can generate the value close to pi.

ALGORITHM:
1. initiate the program
2. initialise the circle, square points and interval to 0
3. generate the random points x and y
4. calculate d,
If d<= 1, increment circle points.
5. increment the square points
6. increment interval
7. if the increments are less than the no. of iterations then repeat
from 2
8. calculate pi
9. end the program

2|Page
FLOWCHART:
Start

Declare radius, pi, no. of


throws

Assign random points under


circle coordinates x, y

i = 1, i <=n, i++

R = sqrt (x2 + y2)

yes
R <= 1

Pi = 4c/n

End

3|Page
Program:

Output:

Percentage error: 0.04%

4|Page
Ques2: Solution of algebraic and transcendental equations using
bisection, newton- Raphson, and secant methods.
a). Bisection method
Objective: To find the solution of the equation using bisection
method
Theory: The method is also called the interval halving method, the
binary search method or the dichotomy method. This method is
used to find root of an equation in a given interval that is value of
‘x’ for which f(x) = 0 .
The method is based on The Intermediate Value Theorem which
states that if f(x) is a continuous function and there are two real
numbers a and b such that f(a)*f(b) 0 and f(b) < 0), then it is
guaranteed that it has at least one root between them. In this
method the function must be continuous between the defined
interval.

ALGORITHM:
1. initiate the program
2. enter the two assumed values for the root say, a and b.
3. Find middle point c= (a + b)/2 .
4. If f(c) == 0, then c is the root of the solution.
5. Else f(c) != 0
a. If value f(a)*f(c) < 0 then root lies between a and c. So
we recur for a and c
b. Else If f(b)*f(c) < 0 then root lies between b and c. So we
recur b and c.
c. Else given function doesn’t follow one of assumptions.
6. Print the root
7. Exit

5|Page
FLOWCHART:
Start

Input a and b

yes

If f(a)*f(b)
>=0

Calculate c = a+b/2

If
f(a)*f(c)<0
No
yes

b=c a=c

If f(c) == 0

End

6|Page
Program:

7|Page
Output:

8|Page
b) Newton-Raphson method
Objective: To find the solution of the equation using Newton-
Raphson method
Theory: root-finding algorithm which produces successively
better approximations to the roots (or zeroes) of a real-
valued function. The most basic version starts with a single-variable
function f defined for a real variable x, the function's derivative f ′,
and an initial guess x0 for a root of f. If the function satisfies
sufficient assumptions and the initial guess is close, then
𝑓(𝑥0 )
X1 = X0 -
𝑓 ′ (𝑥0 )

is a better approximation of the root than x0.


Geometrically, (x1, 0) is the intersection of the x-axis and
the tangent of the graph of f at (x0, f (x0)): that is, the
improved guess is the unique root of the linear
approximation at the initial point. The process is repeated as
𝑓(𝑥𝑛 )
Xn+1 = Xn - ′
𝑓(𝑥
𝑛)
until a sufficiently precise value is reached.

ALGORITHM:
1. Start the program
2. Declare variables
3. Compute values of func(x) and derivFunc(x) for given initial
x
4. Compute h: h = func(x) / derivFunc(x)
5. While h is greater than allowed error
a. h = func(x) / derivFunc(x)
b. x = x – h
6. end

9|Page
FLOWCHART:
Start

Declare variables

Input initiator
value x0

i <= n

yes

Calculate xn = x0 - h

End

10 | P a g e
Program:

Output:

11 | P a g e
c) Secant Method
Objective: To find root of the function using secant method.
Theory: The Secant Method is used to find the roots of an equation.
if xi-1 and xi are two approximations to the root, then the next
approximation xi+1 to root is given by
Xi+1 = xi – fxi (xi-1 – xi)/fxi-1 - fxi
It is an iterative procedure involving linear interpolation to a
root. The iteration stops if the difference between two
intermediate values is less than convergence factor.
ALGORITHM:
1. start the program
2. declare variables
3. make an initial guess x i-1 and xi
4. use the formula give in theory
5. ed the process.

12 | P a g e
FLOWCHART:

Start

Declare variables

Input xi-1 and xi

yes

If f(xi-
1)*f(xi) <0

No

Calculate next value i.e.


Xi+1

No

Check if Xi+1 is the


root of the function

End

13 | P a g e
Program:

Output:

14 | P a g e
Conclusion:
Among Bisection method, Newton-Raphson method and
Secant method, the Method which gives result with great
accuracy is secant method. Since, bisection method need long
process iteration, Newton’s method needs derivative of the
function, which in som cases hard to calculate. While, secant
method give accurate results within less iterations and also
does not need to derivate the function. Hence, Secant
Method is very accurate.
__________________________________________________

15 | P a g e
Ques3 To find integration using Trapezoidal rule, Simpson’s rule
and Monte Carlo method.
a) Trapezoidal rule.
Objective: To integrate the function using Trapezoidal rule.
Theory: This method is used to evaluate definite integrals. This
approximates the region under the curve as a trapezoid. As the
number of rectangles or partitioned are increases the value becomes
more accurate. The formula used for the rule is,
𝑏 ℎ
∫𝑎 𝑓 (𝑥 )𝑑𝑥 = 2 [𝑓 (𝑎) + 𝑓(𝑏) + 2 ∑𝑏−1
𝑖=𝑎+ℎ 𝐹 (𝑖 )]

𝑏−𝑎
Where, h =
𝑛

n = number of rectangles
h = breadth of rectangles
a and b are intervals under which the function need to be evaluated
ALGORITHM:
1. Start the program
2. Define an equation
3. Define the rule using the formula
4. Input the values the upper limit, the lower limit, and the
number of sub-intervals or rectangles.
5. Initialize with ifx = 0
6. Find the value of h = b-a/n
7. Add f(a) and f(b) to ifx
8. Set the value of i = a+h
9. Add h to existing value of i
10. Repeat steps till the i reaches less than b
11. Multiply the final value of ifx with h and divide by to and
assign to ifx
12. print the value
13. end
16 | P a g e
FLOWCHART:
Start

Define function f(x)

Read a, b and n

Calculate h = b-a/n

For i<n

yes

X = a + i*dx

area =
(h/2)*((2*sum)+funct(a)+funct(b));

Print area

End

17 | P a g e
Program:

Output:

18 | P a g e
b) Simpson’s rule
Objective: To find the area of the given function using Simpson’s rule
Theory: This rule is used to evaluate the integrals under the specified
bound of a given function. This rule is an extension to the
trapezoidal rule but evaluates function more accurately. The
formula used in the method is,
𝑏 ℎ
∫𝑎 𝑓(𝑥)𝑑𝑥 = 3 [f(x0) + 4f(x1) + 2f(x2) + 4f(x3) + … + 2f(xn-a) + 4f(xn-2) + f(xn)]
𝑏−𝑎
Where, h =
𝑛

n = number of rectangles
h = breadth of rectangles
a and b are intervals under which the function need to be evaluated
ALGORITHM:
1. start the program
2. define a function fx
3. take the values for lower, upper limits and input the
number of rectangles.
4. Initialize the variable with ifx with 0
5. Find the value of h = b-a/n
6. Add the value of f(a) and f(b) to ifx
7. Set i = a+h
8. Multiply the value fi by 4 and add the result to ifx
9. Add 2*h to existing value of i
10. Repeat above steps and and set the value x = a + 2*h
11. multiply the value fx by 2 and add to ifx
12. add 2*h to existing value of x
13. repeat steps and multiply the final value of ifx with h and
divide by 3 ans assign to ifx
14. print the value of integration
15. end.

19 | P a g e
FLOWCHART:
1.
Start

Define function f(x)

Read a, b and n

Calculate h = b-a/n

For i<n

yes

X = a + i*dx

area= (dx/3)*((4*sumf) + (2*sumt) +


funct(a) + funct(b));

Print area

End

20 | P a g e
Program:

Output:

21 | P a g e
c) Monte Carlo Method
Objective: To find the area of a function using Monte-Carlo
integration
Theory: Monte carlo integration is a technique for numerical
integration using random numbers. It numerically integrates the
definite integral. Monte carlo randomly chooses the points and
evaluates them.
We use, fx = (b-a)1/n∑𝑛𝑖=1 𝑓(𝑥𝑖)
Using random points where n is randomized.
Algorithm:
1. initiate the program
2. declare the function
3. input the lower, upper bound and random points to be
generated
4. randomize lower and upper bounds
5. after the program generates the points calculate for the
area = points generated under the bounds
6. print the area
7. end

22 | P a g e
FLOWCHART:
Start

Declare function, no. of


throws

Assign random points under


function curves x, y

i = 1, i <=n, i++

If bound under the area

no
Area>n

Area = (y-x)*random
points/n

End

23 | P a g e
Program:

Output:

24 | P a g e
Conclusion:
Although all the three methods are better for finding the
integration, but trapezoidal and Simpson’s rule evaluates
integrals using number of rectangles which in some cases
complex for programming point of view, i.e., the iterative
process of these functions are quite large so Mont-Carlo
method is best suited for integration.
__________________________________________________

25 | P a g e
Ques4:
A) to find roots of a quadratic equation.
Objective: To find the roots of the quadratic equation
Theory: In this program, we need to find the roots of the quadratic
equation. We use the control flow statements in order to find out the
discriminant, and use it to get the roots, we know,

−𝑏 ± √𝑏 2 − 4𝑎𝑐
𝑥=
2𝑎
Where, d = √𝑏 2 − 4𝑎𝑐,
When d>0 we have real roots, if d<0, we get imaginary roots,
else we get two equal real roots
ALGORITHM:
1. start
2. enter the coefficients
3. find the discriminant
4. if d>0 print real roots of d<o print imaginary roots
1. Else print real and equal
5. output roots
6. end

26 | P a g e
FLOWCHART:
start

Declare a, b, c, d

2 If, d < b2- 4ac If, d = b2- 4ac


If, d > b - 4ac

Print Real roots Print imaginary roots Print Equal roots

End

27 | P a g e
Program:

Output:

28 | P a g e
B) to find largest of three numbers
Objective: To find the greatest of the three numbers
Theory: In this program, user is asked to enter the three numbers.
Then this program finds out the largest numbers among three
numbers entered by the user and displays it with proper message.
This program can be done in many ways using if, else if statements
or using nested statements.
ALGORITHM:
1. start
2. enter the three numbers a, b, c
3. if a>b and c the print a is greater
4. Else b or c are greater
FLOWCHART:
Start

Read the three


numbers a, b, c

No yes
Is a>b?

No No
Is b>c? Is a>c?

yes
yes

Print b largest Print c largest Print a largest

29 | P a g e
End
Program:

Output:

30 | P a g e
C) To check whether a number is prime or not

Objective: To check whether a number is prime or not

Theory: A prime is a natural number greater than 1 that has no


positive divisors other than 1 and itself. Examples of first few prime
numbers are 2, 3, 5. If the number is divisible by itself so it is a
prime number other wise not. The program uses various loops to
get the results.

ALGORITHM:
1. Start the program
2. Set i=1 and count = 0
3. Check i <=n
If yes, check if it is divisible by itself
Else, check if it is 2
4. Print the result
5. end

31 | P a g e
FLOWCHART:
Start

Read n

Set i=1, count=0

yes
i <= n

yes yes
Count n%i==0
== 2

Count = count + 1
Display not prime

Display prime i=i+1

End

32 | P a g e
Program:

Output:

33 | P a g e
d) to list prime numbers up to 1000

Objective: To list prime numbers up to 1000

Theory: prime numbers are those which are only divided by number
1 and itself. To print prime numbers up to 1000 the program uses
various loops, check the number whether it is prime or not and thus
prints them. Mainly the program uses for loops.

ALGORITHM:
1. start the program
2. enter the value up to which numbers are to be listed
3. check whether the number is prime or not
4. list if it is prime or drop if it is not
5. continue the process till limit is reached
6. stop the process
7. end

34 | P a g e
FLOWCHART:
Start

Input the interval

Set i=2, count=0

for (int i = 2; i <=


n; i++)

no
Check if it is prime

yes

Display number

End

35 | P a g e
Program:

Output:

36 | P a g e

You might also like