Final CPP Dated 5 - 7 - 24
Final CPP Dated 5 - 7 - 24
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
Date: ……………….
1.
2.
3
1. BISECTION METHOD 4
3. TRAPEZOIDAL RULE 14
6. EULER’S METHOD 32
11. SUPERCONDUCTIVITY 58
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
3. Initialize i=0
4. Define a function
7. Calculate c=(a+b)/2
a. Update i
b. If f(c)=0
Display c
Assign c to b
Else assign c to a
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
0 5 0.1
Root of the equation after 5 iterations= 2.96875
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 .
𝑓(𝑥𝑛−1 )
𝑥𝑛 = 𝑥𝑛−1 − | |
𝑓 ′ (𝑥𝑛−1 |
𝑓 ′ (𝑥𝑛−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.
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
Calculated root:2
2) f_x = x * x - 25
f_prime_x = 2 * x
13
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
𝐼 = ∫ 𝑦 𝑑𝑥
𝑎
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
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
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
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 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'
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
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
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
ALGORITHM
1. Start
2. Define factorial function with a variable x of integer type and runs from
initial value 1 to x
6. Read x, limit
9. sum+=term
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
OUTPUT
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,
ALGORITHM
1. Start
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
a11 a12…………a1n x1 b1
A = a21 a22…………a2n x2 = b2
……………………. . .
an1 an2…………..ann xn bn
(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
(4) By the backward substitution in A’X = B’, we get the solution of the given
system of linear equations.
ALGORITHM
1. Start
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++)
{
cin>>b[i];
for(j=0;j<n;j++)
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];
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 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
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
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
𝑘1 = ℎ𝑓(𝑥, 𝑦)
𝑘2 = ℎ𝑓(𝑥0 + ℎ/2, 𝑦0 + 𝑘1 /2)
𝑘3 = ℎ𝑓(𝑥0 + ℎ/2, 𝑦0 + 𝑘2 /2).
𝑘4 = ℎ𝑓(𝑥0 + ℎ, 𝑦0 + 𝑘3 )
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)
RESULT
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
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
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
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
RESULT
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.
T 2
BT = (B0 ∗ (1 − ( ) ))
T C
ALGORITHM
1. Start.
7. At T=0, BT=B0.
T=T+Ts
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
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
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;
cin>>ch;
while(ch=='y');
getch();
f.close();
return(0);
}
62
OUTPUT
1) Enter the value of critical temperature = 5 kelvin
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
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
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
Cω
1
for, Lω < , φ<0, the circuit is predominately capacitive and the voltage lags the
Cω
current
1
for, Lω > φ>0, the circuit is predominately inductive and the voltage leads the
Cω
current
Here,
1
Capacitive reactance: XC=
Cω
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ω=
Cω
ALGORITHM
1. Start
4. With the given values of ω,L,C,R calculate the phase angle using the equation
1
Lω −
-1 Cω
φ=tan { }
𝑅
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>
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();
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);
if(phase>0)
72
else if(phase<0)
else
getch();
return 0;
OUTPUT
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
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