KEMBAR78
Numerical Method | PDF | Mathematical Analysis | Algebra
0% found this document useful (0 votes)
16 views16 pages

Numerical Method

Uploaded by

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

Numerical Method

Uploaded by

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

EXPERIMENT :1

AIM: Root finding of a polynomial equation using Bisection method.

INPUT:

#include<iostream>
using namespace std;
#define EPSILON 0.01

double func(double x)
{
return x*x*x-x-1;
}
void bisection(double a,double b)
{
if (func(a)*func(b) >=0)
{
cout<<"you have not assumed right a and b \n";
return;
}
double c=a;
while ((b-a) >= EPSILON)
{
c=(a+b)/2;
if (func(c)==0.0)
break;
else if (func(c)*func(a)<0)
b=c;
else
a=c;
}
cout<<"the value of root is:"<<c;
}
int main()
{
double a=1,b=2;
bisection(a,b);
return 0;
}

OUTPUT:

physics@neutron:~/212478$ gedit bisection.cpp


(base) physics@neutron:~/212478$ g++ bisection.cpp
(base) physics@neutron:~/212478$ ./a.out
the value of root is:1.32031(base)
EXPERIMENT:2

AIM: Solving first and second order diiferential equation using Euler’s method.

INPUT:

#include<iostream>
#define f(x,y) x+y

using namespace std;

int main()
{
float x0, y0, xn, h, yn, slope;
int i, n;

cout<<"Enter Initial Condition"<< endl;


cout<<"x0 = ";
cin>> x0;
cout<<"y0 = ";
cin >> y0;
cout<<"Enter calculation point xn = ";
cin>>xn;
cout<<"Enter number of steps: ";
cin>> n;
h = (xn-x0)/n;

cout<<"\nx0\ty0\tslope\tyn\n";
cout<<"------------------------------\n";

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


{
slope = f(x0, y0);
yn = y0 + h * slope;
cout<< x0<<"\t"<< y0<<"\t"<< slope<<"\t"<< yn<< endl;
y0 = yn;
x0 = x0+h;
}

cout<<"\nValue of y at x = "<< xn<< " is " << yn;

return 0;
}
OUTPUT:
(base) physics@neutron:~/212478$ gedit euler.cpp
(base) physics@neutron:~/212478$ g++ euler.cpp
(base) physics@neutron:~/212478$ ./a.out

Enter Initial Condition


x0 = 0
y0 = 1
Enter calculation point xn = 0.5
Enter number of steps: 10

x0 y0 slope yn
------------------------------
0 1 1 1.05
0.05 1.05 1.1 1.105
0.1 1.105 1.205 1.16525
0.15 1.16525 1.31525 1.23101
0.2 1.23101 1.43101 1.30256
0.25 1.30256 1.55256 1.38019
0.3 1.38019 1.68019 1.4642
0.35 1.4642 1.8142 1.55491
0.4 1.55491 1.95491 1.65266
0.45 1.65266 2.10266 1.75779

Value of y at x = 0.5 is 1.75779


EXPERIMENT:3

AIM: Root finding of a polynomial equation using Secant method.

INPUT:

#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>

#define f(x) x*x*x - 5*x+1

using namespace std;

int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1, N;

cout<< setprecision(6)<< fixed;

cout<<"Enter first guess: ";


cin>>x0;
cout<<"Enter second guess: ";
cin>>x1;
cout<<"Enter tolerable error: ";
cin>>e;
cout<<"Enter maximum iteration: ";
cin>>N;

cout<< endl<<"**************"<< endl;


cout<<"Secant Method"<< endl;
cout<<"**************"<< endl;
do
{
f0 = f(x0);
f1 = f(x1);
if(f0 == f1)
{
cout<<"Mathematical Error.";
exit(0);
}

x2 = x1 - (x1 - x0) * f1/(f1-f0);


f2 = f(x2);
cout<<"Iteration-"<< step<<":\t x2 = "<< setw(10)<< x2<<" and f(x2) = "<<
setw(10)<< f(x2)<< endl;

x0 = x1;
f0 = f1;
x1 = x2;
f1 = f2;

step = step + 1;

if(step > N)
{
cout<<"Not Convergent.";
exit(0);
}
}while(fabs(f2)>e);

cout<< endl<<"Root is:"<< x2;

return 0;
}

OUTPUT:

(base) physics@neutron:~/212478$ gedit secant.cpp


(base) physics@neutron:~/212478$ g++ secant.cpp
(base) physics@neutron:~/212478$ ./a.out
Enter first guess: 0
Enter second guess: 1
Enter tolerable error: .0001
Enter maximum iteration: 5

**************
Secant Method
**************
Iteration-1: x2 = 0.250000 and f(x2) = -0.234375
Iteration-2: x2 = 0.186441 and f(x2) = 0.074277
Iteration-3: x2 = 0.201736 and f(x2) = -0.000471
Iteration-4: x2 = 0.201640 and f(x2) = -0.000001

Root is:0.201640
EXPERIMENT:4

AIM: Solving first and second order diiferential equation using Runge-Kutta method.

INPUT:
#include<iostream>

#define f(x,y) (y*y-x*x)/(y*y+x*x)

using namespace std;


int main()
{
float x0, y0, xn, h, yn, k1, k2, k3, k4, k;
int i, n;

cout<<"Enter Initial Condition"<< endl;


cout<<"x0 = ";
cin>> x0;
cout<<"y0 = ";
cin >> y0;
cout<<"Enter calculation point xn = ";
cin>>xn;
cout<<"Enter number of steps: ";
cin>> n;

h = (xn-x0)/n;

cout<<"\nx0\ty0\tyn\n";
cout<<"------------------\n";
for(i=0; i < n; i++)
{
k1 = h * (f(x0, y0));
k2 = h * (f((x0+h/2), (y0+k1/2)));
k3 = h * (f((x0+h/2), (y0+k2/2)));
k4 = h * (f((x0+h), (y0+k3)));
k = (k1+2*k2+2*k3+k4)/6;
yn = y0 + k;
cout<< x0<<"\t"<< y0<<"\t"<< yn<< endl;
x0 = x0+h;
y0 = yn;
}

cout<<"\nValue of y at x = "<< xn<< " is " << yn;

return 0;
}
OUTPUT:

(base) physics@neutron:~/212478$ gedit rk.cpp


(base) physics@neutron:~/212478$ g++ rk.cpp
(base) physics@neutron:~/212478$ ./a.out
Enter Initial Condition
x0 = 0
y0 = 1
Enter calculation point xn = 0.2
Enter number of steps: 3

x0 y0 yn
------------------
0 1 1.06649
0.0666667 1.06649 1.13203
0.133333 1.13203 1.19601

Value of y at x = 0.2 is 1.19601


EXPERIMENT -5

AIM-Root finding of a polynomial equation using Newton -Raphson method.

INPUT-
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>

#define f(x) 3*x - cos(x) -1


#define g(x) 3 + sin(x)

using namespace std;

int main()
{
float x0, x1, f0, f1, g0, e;
int step = 1, N;

cout<< setprecision(6)<< fixed;

cout<<"Enter initial guess: ";


cin>>x0;
cout<<"Enter tolerable error: ";
cin>>e;
cout<<"Enter maximum iteration: ";
cin>>N;

cout<< endl<<"*********************"<< endl;


cout<<"Newton Raphson Method"<< endl;
cout<<"*********************"<< endl;
do
{
g0 = g(x0);
f0 = f(x0);
if(g0 == 0.0)
{
cout<<"Mathematical Error.";
exit(0);
}

x1 = x0 - f0/g0;
cout<<"Iteration-"<< step<<":\t x = "<< setw(10)<< x1<<" and f(x) = "<<
setw(10)<< f(x1)<< endl;
x0 = x1;

step = step+1;

if(step > N)
{
cout<<"Not Convergent.";
exit(0);
}

f1 = f(x1);

}while(fabs(f1)>e);

cout<< endl<<"Root is: "<< x1;


return 0;
}

OUTPUT-
physics@neutron:~/212478$ gedit newton2.cpp
physics@neutron:~/212478$ gedit newton2.cpp
physics@neutron:~/212478$ g++ newton2.cpp
physics@neutron:~/212478$ ./a.out
Enter initial guess: 2
Enter tolerable error: 0.000001
Enter maximum iteration: 10

*********************
Newton Raphson Method
*********************
Iteration-1: x = 0.614547 and f(x) = 0.026607
Iteration-2: x = 0.607108 and f(x) = 0.000023
Iteration-3: x = 0.607102 and f(x) = -0.000000

Root is: 0.607102


EXPERIMENT-6

AIM- Numerical integration using Simpson‘s 1/3 rule.

INPUT-
#include<iostream>
#include<math.h>
#define f(x) 1/(1+pow(x,2))

using namespace std;


int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;

cout<<"Enter lower limit of integration: ";


cin>>lower;
cout<<"Enter upper limit of integration: ";
cin>>upper;
cout<<"Enter number of sub intervals: ";
cin>>subInterval;

stepSize = (upper - lower)/subInterval;

integration = f(lower) + f(upper);

for(i=1; i<= subInterval-1; i++)


{
k = lower + i*stepSize;

if(i%2==0)
{
integration = integration + 2 * (f(k));
}
else
{
integration = integration + 4 * (f(k));
}

integration = integration * stepSize/3;

cout<< endl <<"Required value of integration is: "<< integration;

return 0;
}
OUTPUT-

physics@neutron:~/212478$ gedit simpson1.cpp


physics@neutron:~/212478$ g++ simpson1.cpp
physics@neutron:~/212478$ ./a.out
Enter lower limit of integration: 0
Enter upper limit of integration: 6
Enter number of sub intervals: 12
EXPERIMENT -7

AIM- Numerical integration using Simpson’s 1/3 rule.

INPUT:
#include<iostream>
#include<math.h>
#define f(x) 1/(1+pow(x,2))

using namespace std;


int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;

cout<<"Enter lower limit of integration: ";


cin>>lower;
cout<<"Enter upper limit of integration: ";
cin>>upper;
cout<<"Enter number of sub intervals: ";
cin>>subInterval;

stepSize = (upper - lower)/subInterval;

integration = f(lower) + f(upper);

for(i=1; i<= subInterval-1; i++)


{
k = lower + i*stepSize;

if(i%2==0)
{
integration = integration + 2 * (f(k));
}
else
{
integration = integration + 4 * (f(k));
}

integration = integration * stepSize/3;

cout<< endl <<"Required value of integration is: "<< integration;

return 0;
}
OUTPUT:
(base) physics@neutron:~/212478$ gedit simpson1.cpp
(base) physics@neutron:~/212478$ g++ simpson1.cpp
(base) physics@neutron:~/212478$ ./a.out
Enter lower limit of integration: 0
Enter upper limit of integration: 6
Enter number of sub intervals: 12

Required value of integration is: 1.4037(base)


EXPERIMENT-8
AIM-Numerical integration using simpson’s 3/8 rule.

INPUT:
#include<iostream>
#include<math.h>

#define f(x) 1/(1+pow(x,2))

using namespace std;


int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;

cout<<"Enter lower limit of integration: ";


cin>>lower;
cout<<"Enter upper limit of integration: ";
cin>>upper;
cout<<"Enter number of sub intervals: ";
cin>>subInterval;

stepSize = (upper - lower)/subInterval;

integration = f(lower) + f(upper);

for(i=1; i<= subInterval-1; i++)


{
k = lower + i*stepSize;

if(i%3==0)
{
integration = integration + 2 * (f(k));
}
else
{
integration = integration + 3 * (f(k));
}

integration = integration * stepSize*3.0/8.0;

cout<< endl <<"Required value of integration is: "<< integration;

return 0;
}
OUTPUT:
(base) physics@neutron:~/212478$ gedit simpson3.cpp
(base) physics@neutron:~/212478$ g++ simpson3.cpp
(base) physics@neutron:~/212478$ ./a.out
Enter lower limit of integration: 0
Enter upper limit of integration: 1
Enter number of sub intervals: 12

Required value of integration is: 0.785398

You might also like