0% found this document useful (0 votes)
0 views76 pages

Final CPP Dated 5 - 7 - 24

The document is a practical record for MSc Physics students at Newman College, focusing on computational physics for the academic year 2023-2024. It includes various numerical methods such as the Bisection Method, Newton Raphson Method, Trapezoidal Rule, and Simpson's Rule, along with algorithms and C++ programs for each method. Each experiment aims to find roots of equations or perform numerical integration, with detailed outputs and results provided.
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)
0 views76 pages

Final CPP Dated 5 - 7 - 24

The document is a practical record for MSc Physics students at Newman College, focusing on computational physics for the academic year 2023-2024. It includes various numerical methods such as the Bisection Method, Newton Raphson Method, Trapezoidal Rule, and Simpson's Rule, along with algorithms and C++ programs for each method. Each experiment aims to find roots of equations or perform numerical integration, with detailed outputs and results provided.
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/ 76

1

NEWMAN COLLEGE
THODUPUZHA

MSc PHYSICS
PRACTICAL RECORD
PH010402- COMPUTATIONAL PHYSICS
2023-2024

NAME : …………………………………………

REG. NO : …………………………………………
2

NEWMAN COLLEGE
THODUPUZHA

MSc PHYSICS
PRACTICAL RECORD
PH010402- COMPUTATIONAL PHYSICS
2023-2024

Certified that this is a bonafide record of practical work done

by……………………………………………………………………. with Reg.No

…………………………………...during the period 2023-24.

Date: ……………….

Examiners: Professor in charge

1.

2.
3

CONTENTS PAGE NO.

1. BISECTION METHOD 4

2. NEWTON RAPHSON METHOD 9

3. TRAPEZOIDAL RULE 14

4. SIMPSON’S RULE (1/3) & (3/8) RULE 17

5. SUMMATION OF INFINITE SERIES 25

6. EULER’S METHOD 32

7. MONTE CARLO METHOD 36

8. GAUSS ELIMINATION METHOD 40

9. FOURTH ORDER RUNGE-KUTTA METHOD 47

10. PROJECTILE MOTION 51

11. SUPERCONDUCTIVITY 58

12. SERIES LCR CIRCUIT 68


4

Experiment No:01

BISECTION METHOD

AIM
To find the root of the given non-linear equation by bisection method.

THEORY
This method is used to find the root of a non- linear equation using binary chopping.
If 𝑓(𝑥) is real and continuous in the range [𝑎, 𝑏] and 𝑓(𝑎) ∗ 𝑓(𝑏) < 0, then there
exists at least one real root in the interval [𝑎, 𝑏].
𝑎+𝑏
We can define a point 𝑐 = which must satisfy one of the following conditions:
2
i. 𝑓(𝑐) = 0 i.e., c is the root.
ii. 𝑓(𝑎) ∗ 𝑓(𝑐) < 0 i.e. the root lies between a and c.
iii. 𝑓(𝑎) ∗ 𝑓(𝑐) > 0 i.e. the root lies between c and b.
In the case of (ii) replace b by c and in the case of (iii) replace a by c. Hence the
length of the interval [𝑎, 𝑏] reduces to half. After repeating the process 𝑛 times the
initial range 𝑥 reduces to 𝑥 = 1/2𝑛.
5

ALGORITHM

1. Start

2. Declare a, b, c, maxerr and i

3. Initialize i=0

4. Define a function

5. Input a,b and maxerror

6. Repeat step 5 if f(a)*f(b)>0

7. Calculate c=(a+b)/2

a. Update i

b. If f(c)=0

Display c

Else if f(a) * f(c)<0

Assign c to b

Else assign c to a

8. Repeat step 7 & 8 while fabs((b-a)/a)>max error

9. Display c and i

10.Stop
6

PROGRAM
#include<iostream.h>
#include<fstream.h>
#include<process.h>
#include<math.h>
#include<conio.h>
float f(float x)
{
return (x * x) + (2 * x )- 15;
}
void main()
{
clrscr();
float a, b, c, maxerr;
char ch;
int i;
do
{
i = 0;
cout << "Enter the lower limit, upper limit, and maxerror:";
cin >> a >> b >> maxerr;
if (f(a) * f(b) >= 0)
{
cout << "\n No root in this region\n";
return;
}
do
{
c = (a + b) / 2;
if (f(a) * f(c) < 0)
7

{
b = c;
}
else
{
a = c;
}
i = i + 1;
}
while (fabs((b - a) / a) > maxerr);
cout << "\n Root of the equation after\t" << i << " iterations=\t" << c << "\n";
cout << "\n Do you want to continue(y/n)";
cin >> ch;
}
while (ch == 'y');
getch();
}

OUTPUT

Function x * x + 2 * x - 15;
1)lower limit upper limit maxerrror
0 5 1
Root of the equation after 1 iterations= 2.5

2)lower limit upper limit maxerrror


0 5 0.5
Root of the equation after 2 iterations= 3.75

3)lower limit upper limit maxerrror


8

0 5 0.1
Root of the equation after 5 iterations= 2.96875

Function x*x*x-4*x -9;

1)lower limit upper limit maxerrror


2 3 0.5
Root of the equation after 1 iterations= 2.5
2)lower limit upper limit maxerrror
2 3 0.2
Root of the equation after 1 iterations= 2.5
3)lower limit upper limit maxerrror
2 3 0.1
Root of the equation after 2 iterations= 2.75

RESULT
A cpp program to find out the root of the given non-linear equation by bisection
method is written and executed.
9

Experiment No:2
NEWTON RAPHSON METHOD
AIM
To find the roots of a real valued function using Newton Raphson method.

THEORY
Given a function 𝑓(𝑥), the Newton-Raphson method aims to find a value 𝑥 such
that 𝑓(𝑥) = 0. This value of 𝑥 is known as the root of the function.

The method starts with an initial guess 𝑥0 for the root. It then iteratively refines this
guess to get closer to the true root. At each iteration, the method computes the
tangent line to the graph of 𝑓(𝑥) at the current guess 𝑥𝑛 .The point where this
tangent line intersects the x-axis (i.e., where 𝑓(𝑥) = 0) is used as the next
approximation 𝑥𝑛+1 .

The general formula for finding the next approximation is

𝑓(𝑥𝑛−1 )
𝑥𝑛 = 𝑥𝑛−1 − | |
𝑓 ′ (𝑥𝑛−1 |

Where, 𝑥𝑛−1 is the estimated (𝑛 − 1)th root of the function,

𝑓(𝑥𝑛−1 ) is the value of the equation at (𝑛 − 1)th estimated root and

𝑓 ′ (𝑥𝑛−1 ) is the value of the first order derivative of the equation or function at
𝑥𝑛−1 .

The process continues until the difference between successive approximations falls
below a specified tolerance level or until a maximum number of iterations is
reached.
10

ALGORITHM

1. Start
2. Define the function f(x) whose root needs to be found.
3. Define the derivative function f_prime(x).
4. Define the function newtonRaphson(x0, tolerance, max_iterations) as follows:
a) Initialize x = x0.
b) Initialize iter = 0.
c) While iter < max_iterations:
i. Calculate f(x) and f'(x).
ii. If |f'(x)| is too small, break the loop to avoid division by zero.
iii. Calculate the next approximation using the Newton-Raphson formula:
x_next = x - (f(x) / f'(x))
iv. If |x_next - x| < tolerance, return x_next as the approximate root.
v. Update x = x_next and iter = iter + 1.

d) If the loop terminates without convergence, return an indication of failure.


5. In the main function:

a) Set x0, tolerance, and max_iterations.


b) Call newtonRaphson with these parameters and store the result.
c) If the result indicates convergence, print the approximate root, else print a
failure message.
6. Stop
11

PROGRAM

#include <iostream.h>
#include <conio.h>
#include <math.h>
double equation(double x)
{
return x * x -4;
}
double derivative(double x)
{
return 2 * x ;
}
double newtonRaphson(double x0, double tolerance, int maxiterations)
{
double x=x0;
for (int i=0; i < maxiterations; ++i)
{
double f_x = equation(x);
double f_prime_x = derivative(x);
if (f_prime_x == 0)
{
cout<< "Error: Division by zero!"<<endl;
return -1;
}
x = x - f_x / f_prime_x;
if (fabs(f_x) < tolerance)
{
cout << "Root found at x = " << x << " with tolerance " << tolerance << endl;
return x;
}
12

}
cout << "Root not found within maximum iterations!" << endl;
return -1;
}
int main()
{
clrscr();
double x0 = 2.0;
double tolerance = 1e-6;
int maxiterations = 1000;
double root = newtonRaphson(x0, tolerance, maxiterations);
if (root != -1)
{
cout << "Calculated root: " << root << endl;
}
getch();
return 0;
}

OUTPUT

1)f_x = x * x - 4

f_prime_x = 2 * x

Root found at x=2 with tolerance 1e-06

Calculated root:2

2) f_x = x * x - 25

f_prime_x = 2 * x
13

Root found at x=5 with tolerance 1e-06

Calculated root:5

RESULT

A cpp program to find out the roots of a real valued function using Newton
Raphson method is written and executed.
14

Experiment No:03

TRAPEZOIDAL RULE
AIM

To write and execute a c++ program for the numerical integration of a given
function and using trapezoidal rule.

THEORY
The trapezoidal rule, also known as trapezium rule is a technique for
approximating the definite integral of a function. It works by approximating the
area under a curve by dividing it into trapezoids and summing up their areas.The
trapezoidal rule is to find the exact value of a definite integral using a numerical
method.
This rule is mainly based on the Newton Cotes formula which states that one can
find the exact value of the integral as an nth order polynomial. Assume that f(x) be
a continuous function on the given interval [a, b]. Now divide the interval [a, b]
into ‘n’ equal intervals each of width ‘h’.
The trapezoidal rule formula for the definite integral is,

𝑏

∫ 𝑓(𝑥)𝑑𝑥 = [𝑓(𝑥0 ) + 2[𝑓(𝑥1 ) + 𝑓(𝑥2 ) + ⋯ + 𝑓(𝑥𝑛−1 )] + 𝑓(𝑥𝑛 )]
𝑎 2

𝑏−𝑎
where ℎ =
𝑛
𝑎 = 𝑥0 = initial value
𝑏 = 𝑥𝑛 = final value

a h b x
15

ALGORITHM

1. Start
2. Define a function f(x)
3. Enter the initial value (a), final value (b) and no. of intervals (n)
𝑏−𝑎
4. Find out the stepsize ‘h’ using ℎ =
𝑛
5. Set sum1=0
6. sum2= [ f(a)+f(b)]/2
7. Define a loop with variable i
for i=1 to n-1
Set sum1 += f(a+i*h)
8. Find out the integral value using, I=h*(sum1+sum2)
9. Display the integral value
10.Stop

PROGRAM

#include<iostream.h>
#include<conio.h>
#include<math.h>
#define f(x) sin (x)
void main()
{
clrscr();
float a, b, n, h, sum1=0, sum2, I;
cout << "\n Enter the initial value, final value , no. of intervals ";
cin >>a>>b>>n;
h = (b - a) / n;
sum2 = (f(a) + f(b)) / 2;
for (int i = 1; i <= (n - 1); i++)
{
sum1 += f(a + i * h);
}
I = h * (sum1 + sum2);
cout << "The integral value is: " << I;
getch();
}
16

OUTPUT

Function f(x) Lower limit Upper Limit No. of intervals Integral Value
(a) (b) (n)

0 3.14 5 1.933832
Sin (x) 0 3.14 10 1.983539
0 3.14 100 1.999834
0 3.14 5 0.00154
Cos(x) 0 3.14 10 0.001579
0 3.14 100 0.001592
0 1 5 0.5
X 0 1 10 0.5
0 1 100 0.5

RESULT
The c++ program for the numerical integration of a function using trapezoidal rule
is executed and verified.
17

Experiment No:4
SIMPSON’S RULE
SIMPSON’S 1/3 RULE
AIM

To write and execute a CPP program to find the integral of a function using
Simpson’s (1/3) rd. rule.
PRINCIPLE

Simpson’s rule is a numerical method used to approximate a definite integral. This


is done by using quadratic polynomials. In Simpson’s rule, we use parabolas to
approximate each part of the curve. This proves to be very efficient since, it’s
generally more accurate than the other numerical methods.
For a given set of data points (𝑥0 , 𝑦0 ), (𝑥1 , 𝑦1 ),…, (𝑥𝑛 , 𝑦𝑛 ) of a function 𝑦 = 𝑓(𝑥),
the value of definite integral is given by,
𝑏

𝐼 = ∫ 𝑦 𝑑𝑥
𝑎

Let the interval [𝑎, 𝑏] be divided into 𝑛 equal subintervals such that,
𝑎 = 𝑥0 < 𝑥1 < 𝑥2 < ⋯ 𝑥𝑛 = 𝑏 . Also, 𝑥𝑛 = 𝑥0 + 𝑛ℎ, where ℎ is the width of a
subinterval.
On approximating, 𝑦 by Newton’s forward difference formula,
𝑥𝑛
𝑝(𝑝 − 1) 2 𝑝(𝑝 − 1)(𝑝 − 2) 3
𝐼 = ∫ [𝑦0 + 𝑝𝛥𝑦0 + 𝛥 𝑦0 + 𝛥 𝑦0 + ⋯ ] 𝑑𝑥
2 6
𝑥0

On substituting, 𝑥 = 𝑥0 + 𝑝ℎ and 𝑑𝑥 = ℎ 𝑑𝑝 dpd after further simplifications,


𝑥𝑛
𝑛 𝑛(2𝑛 − 3) 2 𝑛(𝑛 − 2)2 3
∫ 𝑦 𝑑𝑥 = 𝑛ℎ [𝑦0 + 𝛥𝑦0 + 𝛥 𝑦0 + 𝛥 𝑦0 + ⋯ ]
2 12 24
𝑥0

From this general formula for numerical integration, different integration formulae
can be obtained by putting 𝑛 = 1,2,3, …
Simpson’s 1/3 rule is obtained by putting 𝑛 = 2 in above equation. Thus,
𝑥2
1 2 ℎ
∫ 𝑦 𝑑𝑥 = 2ℎ [𝑦0 + 𝛥𝑦0 + 𝛥 𝑦0 ] = (𝑦0 + 4𝑦1 + 𝑦2 )
6 3
𝑥0
18

Similarly,
𝑥4

∫ 𝑦 𝑑𝑥 = (𝑦 + 4𝑦3 + 𝑦4 )
3 2
𝑥2

And finally,
𝑥𝑛

∫ 𝑦 𝑑𝑥 = (𝑦 + 4𝑦𝑛−1 + 𝑦𝑛 )
3 𝑛−2
𝑥𝑛−2

On summing up, we get the general expression for Simpson’s (1/3)rd rule as,
𝑥𝑛

∫ 𝑦 𝑑𝑥 = [𝑦 + 4(𝑦1 + 𝑦3 + 𝑦5 + ⋯ + 𝑦𝑛−1 ) + 2(𝑦2 + 𝑦4 + 𝑦6 + ⋯ + 𝑦𝑛−2 )
3 0
𝑥0
+ 𝑦𝑛 ]

ALGORITHM
1) Start
2) Declare variables a, b, n, h, x, sum, f0, f1, f2
3) Enter the initial value (a), final value (b), number of segments (n)
4) Check segments: - if 'n' is not an even number display an error message
"Number of segments must be even for Simpson's 1/3 rule."
5)Calculate step size h= (b-a)/ n
6) Initialize sum = 0
Set f(0) =f(a)+f(b), where f(x)= exp(x)
7)Define a loop for 'j' for 1 to n-1
Calculate 'x' as ' a+ j*h'
19

If 'j' is even update 'sum' as 'sum +2*f(x)


Else update 'sum' as 'sum+ 4*f(x)'

8) Add 'f0' to 'sum'


Multiply 'sum' by h/3 to get the final value.
9) Display the integral value.
10) End

PROGRAM
#include <iostream.h>
#include <conio.h>
#include <math.h>
#define f(x) exp(x)
void main()
{
clrscr();
float a, b, n, h, x, sum, f0, f1, f2;
cout << "\n Enter the initial limit, final limit, no. of segments: ";
cin >> a >> b >> n;
if (((int)n) % 2 != 0)
{
cout << "Number of segments must be even for Simpson's 1/3 rule." << endl;
getch();
return;
}
h = (b - a) / n;
sum = 0;
f0 = f(a) + f(b);
for (int j = 1; j < n; j++)
{
x = a + j * h;
if (j % 2 == 0)
{
sum += 2 * f(x);
20

}
else
{
sum += 4 * f(x);
}
}
sum = (sum + f0) * (h / 3);
cout << "\n Integral of the function is: " << sum;
getch();
}
OUTPUT

1) Enter the intial limit : 0


Enter the final limit : 3.14
Enter the no. of segments : 2
Integral of the function is : 22.676275

2) Enter the intial limit : 0


Enter the final limit : 3.14
Enter the no. of segments : 44
Integral of the function is : 22.10387

3) Enter the intial limit : 0


Enter the final limit : 3.14
Enter the no. of segments : 88
Integral of the function is : 22.103867

Actual value of integral:22.10386686


21

SIMPSON’S 3/8 RULE


AIM
To write and execute a c++ program to find the integral of a function using
Simpson's 3/8 rule.
THEORY
Simpson’s rule is one of the Newton-Cotes formulas used for approximating the
value of a definite integral. We first divide the function into n equal parts over its
interval (a, b) and then approximate the function using fitting polynomial identities
found by Lagrange interpolation. Integrating these polynomials gives us the
approximation for the area under the curve of the function.
In Simpson’s rule, the fitting polynomial is a parabolic arc and not a straight line.
Here, we calculate the area under a curve by dividing the total area under the curve
into n equal subintervals. Then we use three successive points in the interval and fit
them with parabolas. Simpson’s rule can be derived when we integrate a third-order
Lagrange interpolating polynomial fit to the function at three equally spaced points.

The general formula for Simpson's 3/8 rule can be stated as follows:
𝑏
∫ 𝑓(𝑥)𝑑𝑥 ≈
𝑎
𝑛/3 𝑛/3 𝑛/3
3ℎ
[𝑓(𝑥0 ) + 3 ∑ 𝑓(𝑥3𝑖−2 ) + 3 ∑ 𝑓(𝑥3𝑖−1 ) + 2 ∑ 𝑓(𝑥3𝑖 ) + 𝑓(𝑥𝑛 )]
8
𝑖=1 𝑖=1 𝑖=1

Where:

• 𝑛 is the number of subintervals. The total number of data points is 𝑛 + 1.


22

𝑏−𝑎
• ℎ is the width of each subinterval, given by ℎ = .
𝑛

• 𝑥𝑖 for 𝑖 = 0,1,2, … , 𝑛 are the equally spaced nodes (or points) within the interval
[𝑎, 𝑏].
This formula combines three different terms. The first and last terms, 𝑓(𝑥0 ) and
𝑓(𝑥𝑛 ), are the function values at the endpoints of the interval.

ALGORITHM
1) Start
2) Declare variables a,b,n,h,x,sum,f0,f1,f2,f3,c,d,e
3) Enter the initial value (a), final value(b), number of segments (n)
4) Check segments: - if 'n' is not a multiple of 3 display an error message "Number
of segments must be a multiple of 3 for the 3/8 rule."
5)Calculate step size h= (b-a)/ n
6) Initialize sum = 0
Set 'x' equal to 'a'

Set f(0) =f(a), where f(x)= exp(x)

7) Define a loop for 'j' for 1 to n/3


Calculate 'c' =x+h'
f1=f(c)
d=x+(2*h)
f2=f(d)
e=x+(3*h)
f3= f(e)
Update 'sum'= sum+f0+ 3*f1 +3*f2+f3
• Set f0=f3
• Update x= x+3*h

8) Calculate the integral value i= sum *(3*h/8)


9) Display the integral value
23

10) End

PROGRAM
#include <iostream.h>
#include <conio.h>
#include <math.h>
#define f(x) exp(x)
void main()
{
clrscr();
float a, b, n, h, x, sum, f0, f1, f2, f3, c, d, e;
cout << "\n Enter the initial limit, final limit, no. of segments: ";
cin >> a >> b >> n;
if ((int(n) % 3) != 0)
{
cout << "Number of segments must be a multiple of 3 for the 3/8 rule." << endl;
getch();
return;
}
h = (b - a) / n;
sum = 0;
x = a;
f0 = f(a);
for (int j = 1; j <= (n / 3); j++)
{
c = x + h;
f1 = f(c);
d = x + (2 * h);
f2 = f(d);
e = x + (3 * h);
24

f3 = f(e);
sum += f0 + 3 * f1 + 3 * f2 + f3;
f0 = f3;
x = x + 3 * h;
}
float i = sum * (3 * h / 8);
cout << "\n Integral of the function is: " << i;
getch();
}
OUTPUT
F(x)= exp (x)
Enter the initial limit:0
Enter the final limit :3.14
No of segments :3
Integral of the function is: 22.366234

Enter the initial limit:0


Enter the final limit :3.14
No of segments :33
Integral of the function is: 22.103889

Enter the initial limit:0


Enter the final limit :3.14
No of segments :66
Integral of the function is: 22.103872

Actual value of integral :22.10386686


RESULT
A c++ program for both Simpson’s 1/3 and 3/8 rules is written and executed.
25

Experiment No no:5
SUMMATION OF INFINITE SERIES
AIM

To write and execute a program to find the sum of exponential and logarithmic
series up to a particular number of terms and variable value given.

PRINCIPLE

Exponential Series

The exponential function is a very special kind of mathematical function since it


is the only function whose derivative is itself. It is given by

y = ex

The Taylor series of a real or complex valued function f(x) that is infinitely
differentiable at a real or complex number a is the power series given by


𝑓 ′′ (𝑎)
𝑓(𝑥) = 𝑓(𝑎) + 𝑓 (𝑎) ⋅ (𝑥 − 𝑎) + ⋅ (𝑥 − 𝑎)2 + ⋯
2!

When we assign the value a = 0, the Taylor series becomes Maclaurin series given
by


𝑓 ′′ (0) 2
𝑓(𝑥) = 𝑓(0) + 𝑓 (0) ⋅ 𝑥 + ⋅𝑥 +⋯
2!

This series can be used for expansion of various transcendental functions into
infinite series. So, using equation (2) we can write the exponential series as,
𝑥
𝑥2 𝑥3 𝑥𝑛
𝑒 = 1 + 𝑥 + + + ⋯+ +⋯
2! 3! 𝑛!

for every x in
R
26

It is a convergent series for all values of x. In general, we can write the series in
summation form as;
𝑥𝑛
𝑒 𝑥 = ∑∞ 𝑛=0 𝑛!

Logarithmic Series

The logarithmic function is an inverse function to exponentiation. The logarithmic


function is defined as
y= log 𝑎 (𝑥)for x>0, a>0 and a ≠ 1
y= log 𝑎 (𝑥) is equivalent to x=𝑎 𝑦
The base of the logarithm is a. This can be read it as log base a of x. The most two
common bases used in logarithmic functions are base 10 and base e.

The Maclaurin series is an expansion of Taylor series at 0. The Maclaurin


expansion of log(1+x) is given as;
𝑥2 𝑥3 𝑥4
log(1 + 𝑥) = 𝑥 − + − + ⋯
2 3 4
This series converges for -1<x≤1
27

ALGORITHM

1. Start
2. Define factorial function with a variable x of integer type and runs from
initial value 1 to x

3. Introduce a choice between exponential/logarithmic and also invalid choice

4. Choice, Case 1: exponential

5. Declare variables x, limit, initialize sum = 1, term

6. Read x, limit

7. Define loop with variable i.

8. double term = pow(x,i)/factorial(i).

9. sum+=term

10. Print output for exponential summation.

11. Choice case 2: logarithmic

12. Declare variable y, terms, total initialized to 0.0

13. Read y, terms

14. Define loop with variable j

15. float term = pow(y,j)/j

16. Apply conditionals

• If j%2==0, then total -= terms

• Else total += terms

17. Print output for logarithmic summation

18. Any other choice default invalid


28

19. Stop

PROGRAM
#include<iostream.h>
#include<conio.h>
#include<math.h>
float factorial(int x)
{
float f=1;
for(int i=1;i<=x;i++)
{
f=f*i;
}
return(f);
}
void main()
{
clrscr();
int choice,limit;
float x,term,sum=1;
cout<<"Which summation do you prefer,exponential or logarithmic?\n";
cout<<"Please enter '1' for exponential and '2' for logarithmic\n";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the value of x: ";
cin>>x;
cout<<"Enter the value of no of terms to sum: ";
29

cin>>limit;
for(int i=1;i<=limit;i++)
{
float term=pow(x,i)/factorial(i);
sum+=term;
}
cout<<"\nSum of the exponential series upto the first "<<limit<<" terms=
"<<sum<<"\n";
cout<<"\nExponential("<<x<<")="<<exp(x);
getch();
break;
case 2:
float y,total=0.0;
int terms;
cout<<"Enter the value of x: ";
cin>>y;
cout<<"Enter the no of terms: ";
cin>>terms;
for(int j=1;j<=terms;j++)
{
float term=pow(y,j)/j;
if(j%2==0)
{
total-=term;
}
else
{
total+=term;
}
}
30

cout<<"Sum of the logarithmic series for x= "<<y<<" and "<<terms<<" terms:


"<<total<<"\n";
cout<<"Value of log(1+"<<y<<")="<<log(1+y);
getch();
break;
default:
cout<<"\nInvalid choice!";
getch();
break;
}
}

OUTPUT

1)Which summation do you prefer, exponential or logarithmic?


Please enter ‘1’ for exponential and ‘2’ for logarithmic
1
Enter the value of x: 0.5
Enter value of no of terms to sum: 10
Sum of the exponential series up to the first 10 terms =1.648721
Exponential of (0.5) =1.648721

2)Which summation do you prefer, exponential or logarithmic?


Please enter ‘1’ for exponential and ‘2’ for logarithmic
2
Enter the value of x:0.5
Enter the number of terms:10
Sum of the logarithmic series for x =0.5 and 10 terms: 0.405435
Value of log (1+0.5) = 0.405465
31

3)Which summation do you prefer, exponential or logarithmic?


Please enter ‘1’ for exponential and ‘2’ for logarithmic
3
Invalid choice!

RESULT
A c++ program to find out the sum of exponential and logarithmic series up to a
particular number of terms and variable value given is written and executed.
32

Experiment No:06
EULER’S METHOD

AIM
To find out the numerical solution of a given first order differential equations
using the Euler method.

THEORY
Euler's method is a numerical technique for solving ordinary differential
equations with an initial value problem. In this method, the solution is found
through a step-by-step iterative process. The basic idea behind Euler's method is to
approximate the solution to a differential equation using small, discrete steps.
Begin with an initial value for the dependent variable (y) at a given point (𝑥0 ),
which is the starting point for the solution, divide the interval over which the
solution is to approximated into small steps, each of step size ' ℎ '.
According to Euler's method,

𝒚𝒏+𝟏 = 𝒚𝒏 + 𝒉∗ 𝒇(𝒙𝒏 , 𝒚𝒏 ), where,

𝑦𝑛 is the approximate solution at 𝑛th step.

𝑥𝑛 is the current value of independent variable at 𝑛th step.


𝑓(𝑥𝑛 , 𝑦𝑛 ) is the value of derivative of ' 𝑦 ' w.r.t ' 𝑥 ' at the point (𝑥𝑛 , 𝑦𝑛 )

ALGORITHM

1. Start

2. Define the function f (x, y) = x + y

3. Define the Euler method function that takes initial values x0, y0, step size h, and
the number of iterations n
Set x = x0
Set y = y0
4. Define a loop with variable i
for i= 0 to n-1
y = y + h * f (x, y)
x=x+h
5. Print x, y
6. Enter the initial value x0, y0, step size h, no of iterations n
7. Call the function eulermethod (x0, y0, h, n)
8. Display the output
33

9. Stop

PROGRAM
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
float f(float x,float y)
{
return x+y;
}
void eulermethod(float x0,float y0,float h,int n)
{
float x=x0;
float y=y0;
for(int i=0;i<n;++i)
{
y=y+h*f(x,y);
x=x+h;
cout<<"\nx="<<x;
cout<<"\ty="<<y;
}
}
int main()
{
clrscr();
float x0=0.0;
float y0=1.0;
float h=0.1;
int n=10;
eulermethod(x0,y0,h,n);
34

getch();
return 0;
}

OUTPUT
1)
x+y
x = 0.1 y = 1.1
x = 0.2 y = 1.22
x = 0.3 y = 1.362
x = 0.4 y = 1.5282
x = 0.5 y = 1.72102
x = 0.6 y = 1.943122
x = 0.7 y = 2.197434
x = 0.8 y = 2.487178
x = 0.9 y = 2.815895
x=1 y = 3.187485

2) 0.5* (x*x)+x*y
x = 0.1 y=1
x = 0.2 y = 1.0105
x = 0.3 y = 1.03271
x = 0.4 y = 1.068191
x = 0.5 y = 1.118919
x = 0.6 y = 1.187365
x = 0.7 y = 1.276607
x = 0.8 y = 1.390469
x = 0.9 y = 1.533707
x=1 y = 1.71224
35

RESULT
A cpp program to find out the numerical solution of a given first order differential
equations using the Euler method is written and executed.
36

Experiment No:7
MONTE CARLO METHOD
AIM
To write a program to integrate a function using Monte Carlo method
THEORY
The Monte Carlo method can be thought of as a statistical simulation method that
utilize a sequence of random numbers to perform a simulation. The method can be
used to numerically approximate the value of an integral. For a function of one
variable the different steps are
a. Pick n-randomly distributed points X1, X2, X3… Xn in the interval[a,b]
b. Determine the average value of the function in the interval [a,b] as
˂f>=∑i=0 f(xi)/n
c. Compute the integral ʃab f(x)dx as (b-a) ˂f>
d. Estimate the error in the approximation√((˂f*f>)-(˂f>*˂f>))

ALGORITHM

1. Start
2. Read a, b, n
a - lower limit
b - upper limit
n - no. of observations
3. Set sum=0, sumsq = 0
4. Define a function.
5. Start random number generation
6. Get a random no x in the range [a, b]by,
a. X=a+ [(b-a)*rand()/32767.0]
b. Sum = sum +f(x).
c. Sumsq = sumsq+ f(x)*f(x).
d. Repeat the steps a-b n times
37

7. Integral = (b - a)*sum/n.
8. Print integral.
9. Sum = sum/n
10. Sumsq = sumsq/n
11. Error function e = sqrt [sumsq - sum*sum].
12. Print error.
13. Stop

PROGRAM
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
float f(float x)
{
return cos(x);
}
void main()
{
clrscr();
float sum=0,sumsq=0,x,a,b,inte,error,k;
int i,n;
char ch;
randomize;
do
{
cout<<"\n Enter the lower limit,upper limit and no.of observations\n";
cin>>a>>b>>n;
for(i=0;i<n;i++)
38

{
x=a+(b-a)*rand()/32767.0;
k=f(x);
sum=sum+k;
sumsq=sumsq+k*k;
}
inte=(b-a)*sum/n;
cout<<"\na\tb\tn\n";
cout<<"\n"<<a<<"\t"<<b<<"\t"<<n<<"\n";
cout<<"\nintegral="<<inte;
sum=sum/n;
sumsq=sumsq/n;
error=sqrt(sumsq-sum*sum);
cout<<"\nError function="<<error;
cout<<"\n Do you want to continue (y/n)\n";
cin>>ch;
}
while(ch=='y');
getch();
}

OUTPUT
Function Lower Upper No. of Value of Error
limit (a) limit(b) observations(n) integral function
0 3.14 5 2.466677 0.260779
cos(x) 0 3.14 10 1.202891 0.675406
0 3.14 100 0.307329 0.683273
0 3.14 5 1.204036 0.40969
sin(x) 0 3.14 10 2.331048 0.219108
0 3.14 100 2.110775 0.294031
0 1 5 1.456598 0.508205
x 0 1 10 1.1456598 0.745904
0 1 100 4.708976 0.855288
39

RESULT
A c++ program to find the integral of a function using Monte Carlo method is
written and executed.
40

Experiment No:8
GAUSS ELIMINATION METHOD
AIM
To execute a C++ program to solve a system of linear equations using Gauss
Elimination Method.
THEORY
The Gaussian elimination method is known as the row reduction algorithm for
solving linear equations systems. It consists of a sequence of operations performed
on the corresponding matrix of coefficients.
Consider a system of n linear equations with n unknowns is given by

a11x1+a12x2+ -----------a1nxn=b1

a21x1+a22x2+ -----------a2nxn=b2

-----------------------------------------------------------

an1x1+ -------------------a2nxn=bn

In matrix notation we can write the above equation as:

a11 a12…………a1n x1 b1

A = a21 a22…………a2n x2 = b2

……………………. . .

an1 an2…………..ann xn bn

The steps of the Gauss elimination method are

(1) Write the given system of linear equations in matrix form AX = B, where A is
the coefficient matrix, X is a column matrix of unknowns and B is the column
matrix of the constants.
(2) Reduce the augmented matrix [A: B] by elementary row operations to get [A’:
B’].
41

(3) We get A’ as an upper triangular matrix.

(4) By the backward substitution in A’X = B’, we get the solution of the given
system of linear equations.

ALGORITHM

1. Start

2. Declare the variables n,i,j,k

3. Enter the order of the matrix n

4. Define a[10],[10], b[10],x[10] and set sum as zero

5. Enter the coefficient


6. Perform the process of elimination
a) repeat till step (e) from i=0 to i=n-1

b) repeat till step(e) from j=i+1 to j=n


c) set bj= bj-bi*aji/aii

d) repeat step (e) from k=i+1 to k=n


e) set ajk= ajk-(aik*aji)/aii

7. Perform back substitution

i. repeat till step (f) from i=n to i=1


ii. set sum= bi

iii. repeat from j= i+1 to j=n

iv. set x[i] =sum/aii

v. i=i-1
8. Display x[i] in the output screen

9. Stop
42

PROGRAM

#include<iostream.h>

#include<conio.h>
#include<math.h>

void main()

clrscr();

int n,i,j,k;
char ch;

do
{
cout<<"\n Enter the order n:";
cin>>n;

float a[10][10],b[10],x[10],sum=0;
cout<<"\n Enter coefficients:\n";

for(i=0;i<n;i++)
{

cout<<"\n Enter b["<<i+1<<"]:";

cin>>b[i];
for(j=0;j<n;j++)

cout<<"\n Enter a["<<i+1<<"]["<<j+1<<"]:";

cin>>a[i][j];

}
}
cout<<"\n";
43

for(i=0;i<n;i++)

for(j=0;j<n;j++)
cout<<a[i][j]<<" ";

cout<<"\n";

float p=0;

for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)

{
p=a[j][i]/a[i][i];
b[j]=b[j]-p*b[i];
for(k=i;k<n;k++)

a[j][k]=a[j][k]- p*a[i][k];
}

for(i=n-1;i>=0;i--)
{

sum=b[i];

for(j=i+1;j<n;j++)
sum=sum-(a[i][j]*x[j]);

x[i]=sum/a[i][i];

cout<<"\nx["<<i+1<<"]="<<x[i];

cout<<"\n do you want to continue(y/n):";


cin>>ch;
}
44

while(ch=='y');

getch();

OUTPUT
1) The given set of equations are

2x1+x2+x3=7

x1-x2+x3=0
4x1+2x2-3x3=4

Enter the order n:3


Enter the coefficients:
Enter b [1] :7
Enter a[1][1]:2

Enter a[1][2]:1
Enter a[1][3]:1

Enter b[2] :0

Enter a[2][1]:1

Enter a[2][2]:-1

Enter a[2][3]:1

Enter b[3]:4
Enter a[3][1]:4

Enter a[3][2]:2
Enter a[3][3]:-3
45

211

1 -1 1

4 2 -3

x[3]=2

x[2]=3

x[1]=1

2) The given set of equations are


6x1+3x2+2x3=6
6x1+4x2+3x3=0
20x1+15x2+12x3=0
Enter order n = 3
Enter coefficients
Enter b[1]=6
a[1][1]=6
a[1][2]=3
a[1][3]=2

Enter b[2]=0
a[2][1]=6
a[2][2]=4
a[2][3]=3
Enter b[3]=0

a[3][1]=20
a[3][2]=15
a[3][3]=12
46

x[3]=29.999
x[2]=-36
x[1]=8.99

RESULT
A program is written and executed and found out the solution of the matrix.
47

Experiment No:9
FOURTH ORDER RUNGE KUTTA METHOD

AIM
Write and execute a program to solve a differential equation using Fourth order
Runge-Kutta method.

THEORY

Runge-Kutta methods are a category of numerical methods to solve differential


equations. The most used Runge kutta method to find the solution of a differential
equation is the fourth order Range Kutta method. It provides the approximate value
of ' 𝑦 ' for a given point 𝑥.
𝑑𝑦
We have a differential equation, 𝑦 ′ = = 𝑓(𝑥, 𝑦), where 𝑦(𝑥0 ) = 𝑦0 , 𝑖𝑒, the
𝑑𝑥

initial conditions are 𝑥 = 𝑥0 and 𝑦 = 𝑦0 . The interval of ' 𝑥 ' is divided into a finite
number, with step size ' ℎ '. Fourth order RK method involves the recurrence
formula
𝑘1 +2𝑘2 +2𝑘3 +𝑘4
𝑦 = 𝑦0 + ,
6

where RK coefficients are given by,

𝑘1 = ℎ𝑓(𝑥, 𝑦)
𝑘2 = ℎ𝑓(𝑥0 + ℎ/2, 𝑦0 + 𝑘1 /2)
𝑘3 = ℎ𝑓(𝑥0 + ℎ/2, 𝑦0 + 𝑘2 /2).
𝑘4 = ℎ𝑓(𝑥0 + ℎ, 𝑦0 + 𝑘3 )

Where ‘h’ is the step size.


48

ALGORITHM

1) Start
2) Define function f(x, y)
3) Read x0, y0, xmax & h
4) Set x = x0, y = y0
5) Compute k1=h f(x,y)
ℎ 𝑘1
k2 = h f(x0+ , y0+ )
2 2
ℎ 𝑘2
k3 = h f(x0 + , y0+ )
2 2

k4 = h f(x0 + h, y0 + k3 )
y = 𝑦0 +(k1 +2k2 + 2k3 + k4)/ 6
6) Increment x by h
7) Print x, y
8) Repeat steps 5-7 while (x < xmax)
9) Stop

PROGRAM
#include<fstream.h>
#include<conio.h>
#include<math.h>
float f(float x,float y)
{
return x*y;
}
void main()
{
clrscr();
float x0,y0,x,y,xmax,h,k1,k2,k3,k4;
char ch;
49

do
{
cout<<"\n Enter the initial values of x and y\n";
cin>>x0>>y0;
cout<<"Enter the value of x at which y is to be determined and step size\n";
cin>>xmax>>h;
cout<<"x0\ty0\txmax\th\t";
cout<<"\n"<<x0<<"\t"<<y0<<"\t"<<xmax<<"\t"<<h<<"\n";
x=x0;
y=y0;
while(x<xmax)
{
k1=h*f(x,y);
k2=h*f(x+h/2,y+k1/2);
k3=h*f(x+h/2,y+k2/2);
k4=h*f(x+h,y+k3);
y=y+(k1+2*k2+2*k3+k4)/6;
x=x+h;
cout<<"\ny("<<x<<")="<<y<<"\n";
}
cout<<"\n Do you want to continue(y/n)?\n";
cin>>ch;
}
while(ch=='y');
getch();
}
50

OUTPUT

1) y=x*y
Enter the initial values of x and y:
1
2
Enter the value of x at which y is to be determined and step size:
1.1
0.1
x0 y0 xmax h
1 2 1.1 0.1
y(1.1)= 2.221421

2) y= (x*x)+(y*y)

Enter the initial values of x and y:


1
1.2
Enter the value of x at which y is to be determined and step size:
1.05
0.05
x0 y0 xmax h
1 1.2 1.05 0.05
y(1.05)=1.3325

RESULT

A C++ program for finding the solution of a first order differential


equation using Fourth order Runge-Kutta method is written, executed and the
outputs are noted.
51

Experiment no:10
PROJECTILE MOTION

AIM
Write a program to describe the motion of a projectile in the presence of air drag
using Feynman-Newton method of numerical integration.

PRINCIPLE
The Feynman-Newton method is a theoretical approach that combines elements of
Richard Feynman's path integral formulation of quantum mechanics with Isaac
Newton's laws of motion. Although primarily used in quantum field theory, it can
also be applied to classical systems such as the motion of a projectile in the
presence of air drag.
To describe the motion of a projectile under the influence of air drag using the
Feynman-Newton method, we start by considering the action principle. The action
for the system is given by:
S = ∫ L dt
where L is the Lagrangian, defined as the difference between the kinetic and
potential energy of the system:
L=T-V
For the projectile, the kinetic energy T can be written as:
T = (1/2) m v²
where m is the mass of the projectile and v is its velocity.
The potential energy V is related to the gravitational force and can be expressed as:
V=mgy
where g is the acceleration due to gravity and y is the height of the projectile above
some reference point.
Including the effect of air drag, we introduce a drag force term Fdrag that acts
opposite to the velocity of the projectile. The magnitude of the drag force is
typically proportional to the square of the velocity and can be written as:

Fdrag = - 1/2 ρ Cd A v²
where ρ is the density of the air, Cd is the drag coefficient, and A is the cross-
sectional area of the projectile perpendicular to its motion.
52

Now, the Lagrangian for the system becomes:


L = (1/2) m v² + m g y + 1/2 ρ Cd A v²
Using the principle of least action, we can derive the equations of motion by
varying the action with respect to the variables y and v. This leads to the following
Euler-Lagrange equations:
d/dt (∂L/∂v) - ∂L/∂y = 0
For our system, these equations can be written as:
ma - mg - (1/2) ρ Cd A v² = 0
where a is the acceleration of the projectile. Rearranging this equation, we obtain:
ma = mg + (1/2) ρ Cd A v²
This equation describes the motion of the projectile in the presence of air drag
using the Feynman-Newton method. By solving this equation numerically or using
appropriate approximations, one can determine the trajectory and motion of the
projectile, considering the effects of air drag.

ALGORITHM
1. Initialize the variables:
• Set the initial horizontal position (x).
• Set the initial vertical position (y).
• Set the initial horizontal velocity (vx).
• Set the initial vertical velocity (vy).
• Set the time step (DT).
2. Enter a loop until the projectile hits the ground (y >= 0):
• Compute the magnitude of the velocity (v) using std::sqrt(vx*vx + vy*vy).
• Compute the drag force (Fd) using 0.5 * rho * A * C * v * v.
• Compute the horizontal acceleration (ax) using -Fd * vx / (vx*vx + vy*vy).
• Compute the vertical acceleration (ay) using (-G) - (Fd * vy / (vx*vx + vy*vy)).
• Update the horizontal velocity (vx) using vx += ax * DT.
• Update the vertical velocity (vy) using vy += ay * DT.
• Update the horizontal position (x) using x += vx * dt + 0.5 * ax * DT* DT.
• Update the vertical position (y) using y += vy * DT + 0.5 * ay * DT * DT.
• Print the current position (x, y).
53

3. Exit the loop.

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
#include<math.h>

#define G 9.8
#define DT 0.01
#define CD 0.47
#define RHO 1.2
#define A 0.005

double calculateDragForce(double v);

int main()
{
clrscr();
float x,y,vx,vy,t,ax,ay,v,Fd;
cout<<"Enter initial position X,Y: ";
cin>>x>>y;
cout<<"Enter initial velocity vx,vy: ";
cin>>vx>>vy;
cout<<"Enter initial time t: ";
cin>>t;
cout<<"\nTime\tposition(x,y)\tvelocity(Vx)\tVy\n";
ofstream outputFile("projectile.xls");
outputFile<<"Time\tinitial position x\tinitial position y\tinitial velocity vx\tintitial
velocity vy ";
while(y>=0.0)
{
v=sqrt(vx*vx+vy*vy);
Fd=calculateDragForce(v);
ax=Fd*vx/v;
ay=-G-Fd*vy/v;
vx+=ax*DT;
vy+=ay*DT;
x+=vx*DT;
y+=vy*DT;
t+=DT;
cout<<"\n"<<t<<"\t"<<x<<"\t"<<y<<"\t"<<vx<<"\t"<<vy;
outputFile<<"\n"<<t<<"\t"<<x<<"\t"<<y<<"\t"<<vx<<"\t"<<vy;
}
54

outputFile.close();
getch();
return 0;
}
double calculateDragForce(double v)
{
return (0.5*CD*RHO*A*v*v);
}

OUTPUT
1)Enter the initial position x , y: 0, 0
Enter the initial velocity vx ,vy:5, 5
Enter the initial time :0

Time x y vx vy

0.01 0.050005 0.049015 5.000498 4.901502


0.02 0.100015 0.097045 5.000992 4.803018
0.03 0.15003 0.144091 5.001481 4.704548
0.04 0.200049 0.190152 5.001965 4.606092
0.05 0.250074 0.235228 5.002444 4.507651
0.06 0.300103 0.27932 5.002919 4.409223
0.07 0.350137 0.322428 5.00339 4.310809
0.08 0.400175 0.364552 5.003856 4.212407
0.09 0.450219 0.405693 5.004317 4.114018
0.1 0.500266 0.445849 5.004775 4.015643
0.11 0.550319 0.485022 5.005228 3.917279
0.12 0.600375 0.523211 5.005676 3.818928
0.13 0.650437 0.560417 5.006121 3.720589
0.14 0.700502 0.59664 5.006561 3.622262
0.15 0.750572 0.631879 5.006997 3.523946
0.16 0.800646 0.666136 5.00743 3.425642
0.17 0.850725 0.699409 5.007858 3.327349
0.18 0.900808 0.7317 5.008282 3.229067
0.19 0.950895 0.763008 5.008703 3.130795
0.2 1.000986 0.793333 5.00912 3.032535
0.21 1.051081 0.822676 5.009533 2.934284
0.22 1.101181 0.851036 5.009943 2.836044
0.23 1.151284 0.878414 5.01035 2.737814
0.24 1.201392 0.90481 5.010754 2.639594
0.25 1.251503 0.930224 5.011154 2.541383
0.26 1.301619 0.954656 5.011551 2.443182
0.27 1.351738 0.978106 5.011945 2.34499
0.28 1.401862 1.000574 5.012336 2.246807
0.29 1.451989 1.02206 5.012724 2.148633
55

0.3 1.50212 1.042565 5.013109 2.050467


0.31 1.552255 1.062088 5.013492 1.952311
0.32 1.602394 1.08063 5.013873 1.854163
0.33 1.652536 1.09819 5.014251 1.756023
0.34 1.702682 1.114769 5.014627 1.657892
0.35 1.752832 1.130367 5.015 1.559768
0.36 1.802986 1.144983 5.015371 1.461653
0.37 1.853144 1.158618 5.015741 1.363545
0.38 1.903305 1.171273 5.016109 1.265445
0.39 1.953469 1.182946 5.016474 1.167353
0.4 2.003638 1.193639 5.016839 1.069268
0.41 2.05381 1.203351 5.017201 0.971191
0.42 2.103986 1.212082 5.017563 0.873121
0.43 2.154165 1.219833 5.017923 0.775058
0.44 2.204348 1.226603 5.018282 0.677002
0.45 2.254534 1.232392 5.018641 0.578954
0.46 2.304724 1.237202 5.018998 0.480913
0.47 2.354918 1.24103 5.019355 0.382879
0.48 2.405115 1.243879 5.019711 0.284851
0.49 2.455315 1.245747 5.020067 0.186831
0.5 2.50552 1.246635 5.020422 0.088818
0.51 2.555727 1.246544 5.020778 -0.009188
0.52 2.605939 1.245472 5.021133 -0.107188
0.53 2.656154 1.24342 5.021489 -0.20518
0.54 2.706372 1.240388 5.021844 -0.303165
0.55 2.756594 1.236377 5.022201 -0.401144
0.56 2.80682 1.231386 5.022557 -0.499115
0.57 2.857049 1.225415 5.022915 -0.59708
0.58 2.907281 1.218464 5.023273 -0.695037
0.59 2.957518 1.210534 5.023632 -0.792988
0.6 3.007758 1.201625 5.023992 -0.890931
0.61 3.058001 1.191736 5.024354 -0.988867
0.62 3.108248 1.180869 5.024716 -1.086795
0.63 3.158499 1.169021 5.025081 -1.184717
0.64 3.208754 1.156195 5.025446 -1.28263
0.65 3.259012 1.14239 5.025814 -1.380537
0.66 3.309274 1.127605 5.026184 -1.478435
0.67 3.359539 1.111842 5.026555 -1.576326
0.68 3.409809 1.0951 5.026928 -1.674209
0.69 3.460082 1.077379 5.027304 -1.772084
0.7 3.510358 1.05868 5.027682 -1.86995
0.71 3.560639 1.039001 5.028062 -1.967809
0.72 3.610923 1.018345 5.028445 -2.065659
0.73 3.661211 0.99671 5.02883 -2.163501
0.74 3.711504 0.974097 5.029218 -2.261334
56

0.75 3.7618 0.950505 5.029609 -2.359158


0.76 3.8121 0.925935 5.030003 -2.456973
0.77 3.862404 0.900387 5.0304 -2.554779
0.78 3.912712 0.873862 5.0308 -2.652576
0.79 3.963024 0.846358 5.031204 -2.750363
0.8 4.01334 0.817877 5.03161 -2.848141
0.81 4.06366 0.788418 5.032021 -2.945909
0.82 4.113985 0.757981 5.032434 -3.043666
0.83 4.164313 0.726567 5.032852 -3.141414
0.839999 4.214646 0.694175 5.033273 -3.239151
0.849999 4.264983 0.660807 5.033698 -3.336878
0.859999 4.315324 0.626461 5.034126 -3.434594
0.869999 4.365669 0.591138 5.034559 -3.532299
0.879999 4.416019 0.554838 5.034996 -3.629992
0.889999 4.466374 0.517561 5.035436 -3.727674
0.899999 4.516733 0.479308 5.035881 -3.825345
0.909999 4.567096 0.440077 5.03633 -3.923004
0.919999 4.617464 0.399871 5.036784 -4.020651
0.929999 4.667837 0.358688 5.037241 -4.118286
0.939999 4.718214 0.316529 5.037704 -4.215908
0.949999 4.768595 0.273394 5.03817 -4.313518
0.959999 4.818982 0.229283 5.038641 -4.411114
0.969999 4.869373 0.184196 5.039117 -4.508698
0.979999 4.919769 0.138133 5.039598 -4.606267
0.989999 4.97017 0.091095 5.040083 -4.703824
0.999999 5.020575 0.043081 5.040573 -4.801367
1.009999 5.070986 -0.005908 5.041068 -4.898896
57

RESULT

A C++ program using Feynman-Newton method of numerical integration to


describe the motion of a projectile in the presence of air drag executed and graph
plotted using x- y graph.
58

Experiment No:11
SUPERCONDUCTIVITY
AIM
To study the variation of magnetic field B(T) with temperature, in the
superconducting state of various materials.
THEORY
Superconducting is a phenomenon of exactly zero electrical resistance
and expulsion of magnetic flux fields occurring in certain materials called
superconductors, when cooled below a characteristic critical temperature.
Superconductivity is a quantum mechanical phenomenon.

It is characterized by the Meissner effect, the complete ejection of magnetic


field lines from the interior of the superconductor as it goes into the
superconducting state. In a superconductor, the resistance drops abruptly to zero
when the material is cooled below its critical temperature. An electric current
flowing through a loop of superconducting wire can persist indefinitely with no
power source.

Superconductors possess bound pairs of electrons known as Cooper pairs. This


pairing is caused by an attractive force between electrons from the exchange of
phonons. The Cooper pair can flow without energy dissipation. In superconducting
materials, the characteristics of superconductivity appear when the temperature T is
lowered below a critical temperature Tc.

At a fixed temperature below critical temperature, superconducting materials cease


to superconduct when an external magnetic field is applied which is greater than
critical magnetic field.

Magnetic field at any temperature up to critical temperature is given by:

T 2
BT = (B0 ∗ (1 − ( ) ))
T C

BT: Magnetic field B, at any temperature T.

Bo: Magnetic field at absolute zero temperature


59

Tc= critical temperature

ALGORITHM
1. Start.

2. Float values of T, Tc, B0, BT, Ts.

3. Choose the value of critical temperature Tc.

4. Choose the value of magnetic field at absolute zero Bo.

5. Choose the step temperature Ts.

6. Initially give T=0.

7. At T=0, BT=B0.

8. While T<=Tc, B0>=0, Ts>0.


T T
BT = (B0 ∗ (1 − (( ) ∗ ( )))
TC TC

9. Increment the value of T by step temperature Ts

T=T+Ts

10. Repeat 8 and 9 until T=Tc.

11. Tabulate data obtained.

12. Read Ch=(y/n)


60

13. If Ch=y, Repeat the program.

14. Stop.

PROGRAM

#include<iostream.h>

#include<fstream.h>

#include<iomanip.h>

#include<conio.h>

#include<math.h>

int main ()

clrscr();

float B0,T,Tc,BT,Ts;

char ch;

fstream f("SPC.xls",ios::out);

do

cout<<"\n Enter the value of critical temperature Tc in Kelvins,\nMagnetic field at


absolute temperature B0 in Tesla and\n Step temperature ";

cin>>Tc>>B0>>Ts;

if (Tc<0||B0<0||Ts<=0)

cout<<"\nPlease enter a positive value of Tc,B0 and Ts. (Ts other than zero)";

}
61

else

cout<<"For Tc="<<Tc<<"kelvins and B0="<<B0<<"tesla";

cout<<"\n Temperature T\t Magnetic field BT";

f<<"\n Temperature T\t Magnetic field BT";

T=0;

while(T<=Tc&&B0>=0&&Ts>0)

BT=B0*(1-((T/Tc)*(T/Tc)));

cout<<setprecision(2)<<"\n"<<"\t"<<T<<"\t\t"<<BT;

f<<setprecision(2)<<"\n"<<T<<"\t"<<BT;

T=T+Ts;

cout<<"\n Do you want to continue(y/n)?";

cin>>ch;

while(ch=='y');

getch();

f.close();

return(0);

}
62

OUTPUT
1) Enter the value of critical temperature = 5 kelvin

Magnetic field at absolute zero =150 tesla

Step temperature =0.1

Temperature T Magnetic field BT

0 150

0.1 149.94

0.2 149.76

0.3 149.46

0.4 149.04

0.5 148.5

0.6 147.84

0.7 147.06

0.8 146.16

0.9 145.14

1 144

1.1 142.74

1.2 141.36

1.3 139.86

1.4 138.24

1.5 136.5

1.6 134.64

1.7 132.66
63

1.8 130.56

1.9 128.34

2 126

2.1 123.54

2.2 120.96

2.3 118.26

2.4 115.44

2.5 112.5

2.6 109.44

2.7 106.26

2.8 102.96

2.9 99.54

3 96

3.1 92.34

3.2 88.56

3.3 84.66

3.4 80.64

3.5 76.5

3.6 72.24

3.7 67.86

3.8 63.36

3.9 58.74

4 54
64

4.1 49.14

4.2 44.16

4.3 39.06

4.4 33.84

4.5 28.5

4.6 23.04

4.7 17.46

4.8 11.76

4.9 5.94

5 0

2) Enter the value of critical temperature = 7 kelvin

Magnetic field at absolute zero =170 tesla

Step temperature =0.2


65

Temperature T Magnetic field BT

0 170

0.2 169.86

0.4 169.44

0.6 168.75

0.8 167.78

1 166.53

1.2 165

1.4 163.2

1.6 161.12

1.8 158.76

2 156.12

2.2 153.21

2.4 150.02

2.6 146.55

2.8 142.8

3 138.78

3.2 134.47

3.4 129.89

3.6 125.04

3.8 119.9

4 114.49

4.2 108.8
66

4.4 102.83

4.6 96.59

4.8 90.07

5 83.27

5.2 76.19

5.4 68.83

5.6 61.2

5.8 53.29

6 45.1

6.2 36.64

6.4 27.89

6.6 18.87

6.8 9.58

7 0
67

RESULT

A C++ program to study the variation of magnetic field B(T) with


temperature T, in the superconducting state of various materials was written
and executed. Data was tabulated and T- BT graphs were plotted.
68

Experiment No:12

LCR CIRCUIT
AIM
To execute a C++ program to study the variation in phase relation between applied
voltage and current of a series LCR circuit with given values of L and C.
THEORY
A Series LCR circuit, also known as a resonant circuit or tuned circuit, is
an electrical circuit consisting of an inductor (L), capacitor (C) and resistor (R)
connected in series.
An inductor (L), capacitor (C), and resistor (R) are linked in series in the
electrical circuit, which is powered by an AC voltage supply. The alternating
voltage V is supplied by the voltage source, where
V= 𝑉0 sin(ωt)
where, 𝑉0 is the amplitude of the applied voltage
ω is the frequency of the applied voltage.
For the series LCR circuit, the phase difference,
1
Lω −
-1
φ=tan { Cω
}
𝑅
1
for, Lω = , φ=0, the circuit is in resonance and voltage and current are in phase

1
for, Lω < , φ<0, the circuit is predominately capacitive and the voltage lags the

current
1
for, Lω > φ>0, the circuit is predominately inductive and the voltage leads the

current

Here,
1
Capacitive reactance: XC=

Inductive reactance:XL= ωL
Impedance: Z= √𝑅² + (𝑋𝐶 − 𝑋𝐿 )²
69

The frequency at which impedance of the circuit is minimum and the current
admitted is maximum is known as resonant frequency.At resonant frequency,
1
Lω=

ALGORITHM

1. Start

2. Give circuit parameters including voltage, current, inductance, capacitance,


resistance, frequency and value of pi

3. Define angular frequency ω and phase and set ω = 2πf

4. With the given values of ω,L,C,R calculate the phase angle using the equation
1
Lω −
-1 Cω
φ=tan { }
𝑅

5. Start the loop

6. Store the values in Excel sheet and plot the curve


70

7. Depending on the value of phase angle the phase variation between applied
voltage and current can be studied.

8. Stop

PROGRAM

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<math.h>

#define SIZE 100

void writetoexcel(float voltage[],float current[])

ofstream file("LCR.csv");

file<<"Time,Voltage,Current"<<endl;

for(int i=0;i<SIZE;++i)

file<<i<<","<<voltage[i]<<","<<current[i]<<endl;

file.close();

int main()
71

clrscr();

const float PI=3.141592 ,v0=10.0,I0=5.0,L=0.5,C=0.001,R=100,f=50.0;

float t,dt, voltage[SIZE],current[SIZE];

dt=1.0/(f*SIZE);

float w,phase;

w=2*PI*f;

phase=atan(((w*L)-(1/(w*C)))/R);

for(int n=0;n<SIZE;++n)

t=n*dt;

voltage[n]=v0*sin(w*t);

current[n]=I0*sin(w*t+phase);

cout<<"Time\tVoltage\tCurrent"<<endl;

for(int i=0;i<SIZE;++i)

cout<<i<<"\t"<<voltage[i]<<"\t"<<current[i]<<endl;

writetoexcel(voltage,current);

cout<<"\n angular frequency:"<<w<<"rad/s";

cout<<"\n phase angle:"<<phase<<"radians";

if(phase>0)
72

cout<<"\nvoltage leads the current.";

else if(phase<0)

cout<<"\nvoltage lags the current.";

else

cout<<"\n voltage and current are in phase.";

getch();

return 0;

OUTPUT

Time Voltage Current


0 0 4.192626
1 0.627905 4.355413
2 1.253332 4.501012
3 1.873813 4.628848
4 2.486898 4.738416
5 3.090169 4.829283
6 3.681245 4.901092
7 4.257792 4.953558
8 4.817536 4.986475
9 5.358267 4.999712
10 5.877851 4.993217
11 6.374239 4.967017
12 6.84547 4.921215
13 7.289685 4.85599
14 7.705131 4.771601
15 8.090169 4.668381
16 8.443278 4.546737
17 8.763065 4.407149
18 9.048269 4.250167
73

19 9.297764 4.076413
20 9.510564 3.886571
21 9.685831 3.68139
22 9.822872 3.46168
23 9.921146 3.228309
24 9.980268 2.982198
25 10 2.724316
26 9.980268 2.455683
27 9.921147 2.177359
28 9.822873 1.890442
29 9.685832 1.596064
30 9.510566 1.295387
31 9.297767 0.989598
32 9.048272 0.679903
33 8.763068 0.367525
34 8.443282 0.053697
35 8.090173 -0.260344
36 7.705135 -0.573357
37 7.289689 -0.884107
38 6.845475 -1.191368
39 6.374244 -1.493927
40 5.877858 -1.79059
41 5.358272 -2.080188
42 4.817542 -2.361574
43 4.257799 -2.633641
44 3.68125 -2.895315
45 3.090176 -3.145562
46 2.486903 -3.383395
47 1.873819 -3.607875
48 1.253339 -3.818116
49 0.62791 -4.013289
50 6.22E-06 -4.192624
51 -0.627898 -4.355412
52 -1.253327 -4.501011
74

53 -1.873806 -4.628847
54 -2.486891 -4.738415
55 -3.090164 -4.829283
56 -3.681239 -4.901091
57 -4.257785 -4.953557
58 -4.817531 -4.986474
59 -5.358261 -4.999712
60 -5.877848 -4.993217
61 -6.374234 -4.967018
62 -6.845465 -4.921215
63 -7.289681 -4.85599
64 -7.705127 -4.771602
65 -8.090164 -4.668382
66 -8.443275 -4.546738
67 -8.763062 -4.40715
68 -9.048266 -4.25017
69 -9.297762 -4.076415
70 -9.510562 -3.886573
71 -9.68583 -3.681392
72 -9.822871 -3.461683
73 -9.921145 -3.228312
74 -9.980267 -2.982199
75 -10 -2.724319
76 -9.980268 -2.455687
77 -9.921148 -2.177361
78 -9.822874 -1.890445
79 -9.685834 -1.596067
80 -9.510569 -1.295391
81 -9.297768 -0.989599
82 -9.048274 -0.679905
83 -8.763072 -0.367528
84 -8.443285 -0.0537
85 -8.090178 0.26034
86 -7.705141 0.573353
75

87 -7.289692 0.884105
88 -6.845479 1.191366
89 -6.374249 1.493924
90 -5.877862 1.790587
91 -5.358279 2.080184
92 -4.817544 2.361573
93 -4.257802 2.63364
94 -3.681256 2.895313
95 -3.090182 3.145559
96 -2.486912 3.383392
97 -1.873828 3.607872
98 -1.253342 3.818115
99 -0.627916 4.01328

Voltage leads the current.

RESULT
A C++ program to study the variation in phase relation between applied voltage and
current of a series LCR circuit with given values of L and C is executed and graph
is plotted.
76

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy