Anda di halaman 1dari 10

/* Akash Maurya ( Physics I year ) */

#include<iostream>
#include<math.h>
#include<cstdlib>
#include<time.h>
using namespace std ;

// Function for calculating values

double Pi_Generator ( double n )


{
double c=0 ; // c holds the no. of points lying within the quarter unit circle

// loop for generating points x and y

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


{
double x= ( double ) rand ( ) /RAND_MAX ;
double y= ( double ) rand ( ) /RAND_MAX ;
if ( ( ( x*x ) + ( y*y ) ) <=1 )
c+=1 ;
}

// returning the calculated pi value

return ( ( 4*c ) /n ) ;
}

// Function for displaying the table

void Display ( double A[10][2],int n )


{
cout<<"\n\t\t\tTABLE\n\nS.No.\t\tEstimated value\t\t% Error\n\n" ;
for ( int i=0 ; i<n ; i+=1 )
{
cout<<" "<<i+1<<"\t\t" ;
for ( int j=0 ; j<2 ; j+=1 )
{
cout<<A[i][j]<<"\t\t\t" ;
}
cout<<"\n" ;
}
}

main ( )
{
char ch=227 ;

//variable declaration

int seed=time ( NULL ) ; /*'seed' initialized with system time value ( to be used in
srand( ) function ) */
int N ; // 'N' holds the no. of -values to be generated
double n ; /* 'n' holds the number of data points to be used for each
value evaluation */
double A[N][2] ; // 2-D array 'A' holds the tabular data

srand ( seed ) ;

//Entering values in N and n

cout<<"Enter the number of "<<ch<<"-values you want " ;


cin>>N ;
cout<<"\n\nEnter the number of data points to be used "
<<"to evaluate each "<<ch<<"-value " ;
cin>>n ;

//loop for populating the array 'A'

for ( int i=0 ; i<N ; i+=1 )


{
A[i][0]=Pi_Generator ( n ) ;
A[i][1]= (( fabs( A[i][0]-3.1415926535897932384626433832795 )) /
3.1415926535897932384626433832795 )*100 ;
}
//Displaying the table
Display ( A,N ) ; }
/* Akash Maurya ( Physics I year ) */

#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std ;
main ( )
{
char ch=227 ;

// variable declaration

int niter ; // 'niter' holds the number of data points to be used for each
value evaluation
int N ; // 'N' holds the total no. of pi-values to be generated in the text file
double pi ; // 'pi' holds each calculated pi-value

cout.precision ( 15 ) ;

// Entering values in niter and N

cout<<"Enter the number of data points you want for calculating the value of "<<ch<<" ";
cin>>niter ;
cout<<"\nEnter the number of "<<ch<<" values you want in your text file " ;
cin>>N ;

ofstream f ; // Declaring output stream file variable 'f'


cout<<"\n" ;
f.open ( "pi.txt" ) ; // Opening a text file 'pi.txt'

// Loops for generating and putting each pi-value in the text file 'pi.txt'

for ( int i=0 ; i<N ; i+=1 )


{
double c=0 ;
for ( int j=0 ; j<niter ; j+=1 )
{
double x= ( double ) rand ( ) /RAND_MAX ;
double y= ( double ) rand ( ) /RAND_MAX ;
if ( ( x*x+y*y ) <=1 )
c+=1 ;
}
pi= ( double ) ( ( 4*c ) /niter ) ;
f<<pi<<"\n" ;
}
f.close ( ) ;
}
/* Akash Maurya ( Physics I year ) */

#include<iostream>
#include<math.h>
using namespace std ;

// Defining function 'f' for returning function's value at any point 'x'

double f ( double x )
{
return ( x*x-2 ) ;
}

main ( )
{
cout.precision ( 15 ) ;

//Variable Declaration

double a,b ; // a and b hold any two values about the root ( f ( a ) < 0 and f ( b ) > 0 )
double e ; // e holds the accuracy upto which root is to be found
double mid ; // mid holds the mid-values of the two points about the root
int c=0 ; // c keeps the count of number of steps

//Entering values in a,b and e

cout<<"Enter the values of a and b such that f ( a ) < 0 and f ( b ) > 0 " ;
cin>>a>>b ;
cout<<"\nDefine the accuracy upto which you want to calculate the root " ;
cin>>e ;

// Code for calculating the root using Bisection method

while ( fabs( a-b ) >e )


{
c+=1 ;
mid= ( a+b ) /2 ;
if ( ( f ( a ) <0 ) && ( f ( mid ) >0 ) )
b=mid ;
else if ( ( f ( b ) >0 ) && ( f ( mid ) <0 ) )
a=mid ;
else if ( f ( mid ) ==0 )
{
cout<<"Root is found to be one of the mid value !" ;
break ;
}
cout<<"\nRoot as calculated in "<<c<<" step\t"<<mid ;
}

// Displaying the root

cout<<"\n\nCalculated root using bisection method is "<<mid<<"\n" ;


}
/* Akash Maurya ( Physics I year ) */

#include<iostream>
#include<math.h>
using namespace std ;

// Defining function 'f' for returning function's value at any point 'x'

double f ( double x )
{
return x*x-2 ;
}

// Defining function 'derivative_f' for returning function's derivative value at any point 'x'

double derivative_f ( double x )


{
return 2*x ;
}

main ( )
{
cout.precision ( 15 ) ;

// Variable Declaration

double a ; // a holds any value close to the root


double e ; // e holds the accuracy up to which root is to be found
double y ; // y holds the value of root calculated in each step
int c=1 ; // c keeps the count of number of steps

// Entering values in a and e

cout<<"Enter any value that you think is close to the root " ;
cin>>a ;
cout<<"\nEnter the accuracy you want in the determination of roots " ;
cin>>e ;
// Code for calculating the root using Newton-Raphson method

y= (double)(a-(f (a) /derivative_f (a))) ;


cout<<"\nRoot as calculated in "<<c<<" step\t"<<y ;
while ( fabs ( a-y ) >e )
{
c+=1 ;
a= ( double ) y ;
y= ( double ) ( a- ( f ( a ) /derivative_f ( a ) ) ) ;
cout<<"\nRoot as calculated in "<<c<<" step\t"<<y ;
}

//Displaying the Root

cout<<"\n\nRoot within the given accuracy is "<<y<<"\n" ;


}
/* Akash Maurya ( Physics I year ) */

#include<iostream>
#include<math.h>
using namespace std ;

// Defining function 'f' for returning function's value at any point 'x'

double f ( double x )
{
return ( ( x*x ) -2 ) ;
}

main ( )
{
cout.precision ( 15 ) ;

// Variable Declaration

double a,b ; // a and b hold any two values close to the root
double e ; // e holds the accuracy up to which root is to be found
double z ; // z holds the value of root calculated in each step
int c=1 ; // c keeps the count of number of steps

// Entering values in a,b and e

cout<<"Enter any two values that you think are close to the root " ;
cin>>a>>b ;
cout<<"\nEnter the accuracy you want in the determination of roots " ;
cin>>e ;

// Code for calculating the root using Secant method

z= (( a*f (b))-b*f (a)) / (f (b)-f (a)) ;


cout<<"\nRoot as calculated in "<<c<< " step\t"<<z ;
while ( fabs ( z-b ) >e )
{
c+=1 ;
a=b ;
b=z ;
z= ((a*f (b))-b*f (a)) / (f (b)-f (a)) ;
cout<<"\nRoot as calculated in "<<c<< " step\t"<<z ;
}

// Displaying the Root

cout<<"\n\nRoot within the given accuracy is "<<z<<"\n" ;


}

Anda mungkin juga menyukai