Integration

1.0 
Given that y=ln(x), Integrate this from  4.0 to 5.2 using Trapezoidal rule. For theory part look any numerical book.

#include <iostream>
#include <stdio.h>
#include<cmath>                        ////////////////////////////////////////////////////
#include<stdlib.h>                      //  Trapezoidal rule, For                //
                                                 //  more information look , titas    //
using namespace std ;                // numerical book, page 290        // 
                                                /////////////////////////////////////////////////////
int N=6;                   // global declaration          

int main(){
    int n;                        //for loop variable
    double a , b;             // limit
    double  h;               // interval
   
    double arry[N+1];             // value of y
    double  x0, val, tempa;        // temp a is a temporary a variable
   
    cout<<" Put the Lower limit:";
    cin>>a;
    cout<<" Put the Upper limit:";
    cin>>b;                                                 ///////////////////////////////
                                                                //                            //  
    h=(b-a)/N;                                             //         y=ln(x)        //
    tempa=a;                                             //                            //  
                                                               /////////////////////////////// 
    for(n=0; n<=6 ; n++){
    x0 = tempa ;  
    arry[n] = log(x0);
    tempa=x0+h;
}
val=(h/2.0)*( (arry[0]+arry[6])+ 2*(arry[1]+arry[2]+arry[3]+arry[4]+arry[5]) );

printf(" the value is: %12.10lf \n\n", val);      
return 0;

}


2.0 
Now  Integrate y=ln( x ) from  4.0 to 5.2 using Simpsons 1/3  rule. For theory part look any numerical book.

#include <iostream>
#include <stdio.h>
#include<cmath>                 ///////////////////////////////////////////////////
#include<stdlib.h>               // This is Simpsons 1/3 rule        //
                                          // For more information look       //
using namespace std ;         // numerical book, page 290      // 
                                          ///////////////////////////////////////////////////
int N=6;                 // global declaration          

int main(){
    int n;                     //for loop variable
    double a , b;           // limit
    double  h;             // interval
   
    double arry[N+1];           // value of y
    double  x0, val, tempa;       // temp a is a temporary a variable
   
    cout<<" Put the Lower limit:";
    cin>>a;
    cout<<" Put the Upper limit:";
    cin>>b;                                                 ///////////////////////////////
                                                                //                            //  
    h=(b-a)/N;                                             //         y=ln(x)        //
    tempa=a;                                             //                            //  
                                                               /////////////////////////////// 
    for(n=0; n<=6 ; n++){
    x0 = tempa ;  
    arry[n] = log(x0);
    tempa=x0+h;
}
val = ( h/3.0 )*(  (arry[0] + arry[6] ) + 4*( arry[1] + arry[3] + arry[5] ) + 2*( arry[2]+arry[4]) );

printf(" the value is: %12.10lf \n\n", val );      
return 0;
}


3.0   
Compute the following integrals correct to 10 decimal places

Using Trapezoidal rule and Romberg’s trick.

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


using namespace std ;                            // code for practice quwstion 1(b)

int N=8;

double val1(double a, double b){            // Numerical, titas math series //
  double h=(b-a) / 2.0;                            // page no 312                          //
   int n;                                                   // Romberg integration             //
    double arry[N+1], x0;                       // using Trapezoidal rule         //
     for(n=0;  n<=2 ;  n++){          
      x0=a;  
    arry[n] = (cos(x0)*log(sin(x0)))/(1+sin(x0));

   a = x0+h;
}
return (h / 2.0)*( (arry[0] + arry[2] ) +  2*arry[1] );
}

double val2(double a,  double b) {
  double h = (b-a) / 4.0;
   int n;
    double arry[N+1],  x0;
     for(n=0;  n<=4 ;  n++){
      x0=a;  
    arry[n] = ( cos(x0)*log(sin(x0) ) ) / (1+sin(x0));
    
    a=x0+h;
}

return ( h / 2.0)*( (arry[0] + arry[4] ) + 2*(arry[1]+arry[2]+arry[3]) );

}

double val3(double a, double b){
   double h=(b-a)/8.0;
    int n;
     double arry[N+1],x0;
      for(n=0;  n<=8 ;  n++){
       x0=a;  
  
    arry[n] = ( cos(x0)*log(sin(x0))) / (1+sin(x0));
  
      a=x0+h;
}
return ( h / 2.0 )*( ( arry[0]+arry[8] )+ 2*(arry[1]+arry[2]+arry[3]+arry[4]+arry[5]+arry[6]+arry[7]) );
}

int main()
{
double a,b;                     // limit
double pal,val,cal;
double I1,I2,I3;

a=M_PI / 4.0;                  // a is the lower limit
b=M_PI / 2.0;                 // b is the upper limit   

I1=val1(a,b);
I2=val3(a,b);
I3=val3(a,b);

 val=I2+(1.0/3.0)*(I2-I1);
 pal=I3+(1.0/3.0)*(I3-I2);

 if( fabs(val - pal) <=0.001)
 {
            printf("  Value= %14.12lf \n", pal);
                   }
             else   printf("Final Value= %14.12lf \n", pal - (1.0 / 3.0 )*( pal - val ) );  
                 
  return 0;
}


4.0 
#include <iostream>
#include <stdio.h>
#include<cmath>                 
#include<stdlib.h>              

using namespace std ;           

int N=6;                        

double L=0.248744;                                  // Length of pendulam =24.8744 cm
double g=9.82;                                    // value of g=9.82   

FILE * cplab;

double pendulumT( double thetaM ){
    int n;                                             //for loop variable
    double a, b;                                  // limit
    double h;                                      // interval
   
    double frac, thm;
   
    double arry[N+1];                          // value of y, it keeps 7 element
    double x0, val, tempa;                    // temp a is a temporary a variable      

    a = 0;                                             //  lower limit is 0
    b = M_PI/2.0;                               //  Upper limit is pi/2
 
    frac = 4.0*sqrt(L/g);

   thm = sin(thetaM/2.0)*sin(thetaM/2.0); 
  
    h=(b-a)/N;
    tempa=a;
   
    for(n=0; n<=N ; n++){
    x0=tempa;  
    arry[n] = frac / sqrt( 1.0 - ( thm*sin(x0)*sin(x0) )  );
   
    tempa=x0+h;
}
val=( h / 3.0 )*( (arry[0]+arry[6] )+ 4*(arry[1]+arry[3]+arry[5])+2*( arry[2]+arry[4] ) );

return val;
}

int main(){
cplab = fopen("pendulum", "w");
double n;   

  for(n=0; n<M_PI/2.0 ; n+=M_PI/40)
//printf(" the value is: %12.10lf \n\n", pendulumT(M_PI/18.0));      

 fprintf( cplab," %lf %lf \n ", n ,  pendulumT(n)  );

 fclose(cplab);

 return 0;

}









2 comments:

  1. http://phet.colorado.edu/sims/pendulum-lab/pendulum-lab_en.html

    To know more about pendulum, Login the above link...

    ReplyDelete
  2. Please give me the code of derivation of sin x to nth term, like 67th derivative of sin x.

    ReplyDelete