Lab Exam Code

This code may be practiced at the end, for better understanding.........

Download different year   question paper

1.0

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

using namespace std;

FILE*cplab;

double sinc(double x, int m){              // 3rd Year 2007, April 11, 2009
  double val=0.0;
  int n=0;
  for(n=1 ;n<=m;n++){
    val+=powf(-1,n+1)*sin(n*x)/n;
  }
  return 2*val;
}

int main(){

  cplab=fopen("last21","w");

  double n=0;
  double x=0.0, val=0.0,sgn=1.0;

  for(x=-M_PI; x<=M_PI; x+=M_PI/2000){
 
    fprintf(cplab,"%lf  %lf  %lf  %lf  %lf  %lf  %lf  %lf \n",x,sinc(x,2),
                   sinc(x,3),sinc(x,4),sinc(x,5),sinc(x,6),sinc(x,7),sinc(x,8));
     }
  fclose(cplab);
  return 0;
}


2.0

#include <iostream>
#include<cmath>                 
#include<stdlib.h>               
                             
using namespace std ;             //  3rd  Year B.Sc.,  April 11, 2009

FILE*cplab;

int N=10; 

double mygaussian(double a){
       
    double la, ub, frac; 
    int n;                                 

    double arry[N+1];           
    double x0, val, h;
    frac=1/sqrt(2*M_PI);        

    la=-a;
    ub=a;
    
    h=(ub-la)/N; 
    
    for(n=0; n<=N ; n++){
    x0=la;   
    arry[n]=exp(-x0*x0/2.0);
    la=x0+h;
    }
return frac*(h/3.0)*( (arry[0]+arry[10])+ 4*(arry[1]+arry[3]+arry[5]+arry[7]+
                               arry[9])+2*(arry[2]+arry[4]+arry[6]+arry[8]));
}       
                       
int main(){
    cplab=fopen("gaussion","w");
    
    double a;
    
    for(a=-2.0 ; a<=2.0; a+=0.01){
                         
    fprintf(cplab,"%lf %lf \n", a , mygaussian(a));       
    }
 fclose(cplab);
 return 0;
}

3.0


#include<iostream>                     //Exam Ques 2008
#include<stdio.h>
#include<cmath>
#include<stdlib.h>

using namespace std;

FILE*cplab;                             // Exam Date October 21,2009

double fac(int n){
       if (n==0) return 1;
       else return n*fac(n-1);
       }

double mylegen(int n, double x){
     
       int s;
       double val=0.0;
     
       for(s=0; s<=(n/2); s++){
       val+=powf(-1,s)*fac(2*n-2*s)*powf(x,n-2*s)/( powf(2,n)*fac(s)*fac(n-s)*fac(n-2*s));
       }
       return val;
       }
     
     
int main(){
 
cplab=fopen("lege","w");  
 
    double x;
    for(x=-1.0; x<=1.0; x+=0.0001){
    fprintf(cplab,"%lf %lf %lf %lf %lf \n",x,mylegen(1,x),mylegen(2,x),mylegen(3,x),mylegen(4,x));
}

fclose(cplab);
return 0;
}

The figure is like this


4.0
    
Write a C++ code to evaluate the error function


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

using namespace std;

int N=10;

double erf(double x){           // http://en.wikipedia.org/wiki/Error_function
     
    double a, b,frac ;
    int n;                              

    double arry[N+1];        
    double x0, val, h;
 
    frac=2.0/sqrt(M_PI);
 
    a=0;
    b=x;
 
    h=(b-a)/N;
 
    for(n=0; n<=N ; n++){
    x0=a;
    arry[n]=exp(-x0*x0);
    a=x0+h;
    }
return frac*(h/3.0)*( (arry[0]+arry[10])+ 4*(arry[1]+arry[3]+arry[5]+arry[7]+arry[9])
                                                            +2*(arry[2]+arry[4]+arry[6]+arry[8]));

}    

int main(){
 
    cout<<erf(2.0)<<endl;
return 0;
}

5.0

#include<iostream>
#include<cmath>

using namespace std;

FILE*cplab;

double confrac(int p, int n){
        if (n==0)  return p;
        else return p + p/confrac( p, n-1 );
        }

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

for( n=2; n<=20; n+=2 ){
fprintf(cplab,"%d %lf %lf \n",n,confrac(2,n),confrac(3,n));
}  
 fclose(cplab);
return 0;
}




6.0


#include<iostream>
#include<cmath>

using namespace std;

FILE*cplab;

double full(double t){                    // For more info look MMP book, Arfken, sec -14.3.2
       int n;
       double val=0.0;
     
    for(n=2; n<=12; n+=2){
    val+=cos(n*t)/(n*n-1);              //  we have used w=1, or wt along the x axis
    }    
return (2.0/M_PI)-(4.0/M_PI)*val;
}


int main(){
cplab=fopen("fou4","w");

    double t;
    for( t=-2*M_PI; t<=2*M_PI; t+=M_PI/2000 ){         // We have taken the increment Pi/2000
    fprintf(cplab," %lf  %lf \n", t , full(t));
}            

fclose(cplab);
return 0;



No comments:

Post a Comment