0% found this document useful (0 votes)
40 views

CM Lab File

The document is a practical file submitted for a Bachelor of Technology degree in Information Technology. It contains an index listing 11 programs related to computational lab exercises involving numerical methods like root finding, integration, and solving systems of linear equations. The programs are written in C++ and calculate things like decimal to binary conversion, solving non-linear equations using bisection, secant and Newton's methods, numerical integration using trapezoidal, Simpson's 1/3 and 3/8 rules, and Gauss elimination for systems of linear equations.

Uploaded by

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

CM Lab File

The document is a practical file submitted for a Bachelor of Technology degree in Information Technology. It contains an index listing 11 programs related to computational lab exercises involving numerical methods like root finding, integration, and solving systems of linear equations. The programs are written in C++ and calculate things like decimal to binary conversion, solving non-linear equations using bisection, secant and Newton's methods, numerical integration using trapezoidal, Simpson's 1/3 and 3/8 rules, and Gauss elimination for systems of linear equations.

Uploaded by

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

UNIVERSITY SCHOOL OF INFORMATION AND

COMMUNICATION TECHNOLOGY
GURU GOBIND SINGH INDRAPRASTHA
UNIVERSITY, NEW DELHI-110078
(2020-2024)

PRACTICAL FILE
OF
COMPUTATION LAB
IT -253
Submitted in partial fulfilment of the requirements
for the award of the degree of

BACHELOR OF TECHNOLOGY
IN
INFORMATION TECHNOLOGY

Submitted to: - NAME: AKSHAT KUMAR


Ms. ANSHUL BHATIA
IT-253 ENROLLMENT NO: 00716401520

B.Tech IT 3rd Sem


INDEX

S. No Name of the Programs

1. To Convert Decimal to Binary and Binary to Decimal numbers

2. To find the roots of the non-linear equation using Bisection


Method
3. To find the roots of the non-linear equation using Secant
Method
4. To find the roots of the non-linear equation using Newton’s
Method
5. To implement numerical integration using Trapezoidal rule

6. To implement numerical integration using Simpson 1/3 rule

7. To implement numerical integration using Simpson 3/8 rule

8. To solve the system of linear equation using Gauss-Elimination


method
9. To Implement Langrange’s Interpolation formula

10. To find the numerical solution of ordinary differential equation


by Euler’s Method
11. To find the numerical solution of ordinary differential equation
by Runge-Kutta Method
PROGRAM 1

Write a C++ Program for Decimal to Binary and Binary to Decimal


Conversion.

#include<iostream>
using namespace std;
int main()
{
    int choice;
    while(1)
  {
        cout<<endl<<endl<<"1. Decimal to Binary conversion";
        cout<<endl<<"2. Binary to Decimal conversion";
        cout<<endl<<"3. Exit";
        cout<<endl<<"ENTER YOUR CHOICE:";
        cin>>choice;
        switch(choice)
    {
            case 1:
      {
                int a[10],n,i;    
                cout<<"Enter the Decimal number to convert:";    
                cin>>n;    
                for(i=0;n>0;i++)    
        {  
                    a[i]=n%2;    
                    n= n/2;  
        }  
                cout<<"Binary of the given number:";    
                for(i=i-1;i>=0;i--)    
        {  
                    cout<<a[i];    
        }  
                break;
      }
            case 2:
      {
                int B;
                cout<<"Enter Binary Number to convert:";
                cin>>B;
                int dec=0;
                int base =1;
                while(B)
        {
                    int rem = B%10;
                    B = B/10;
                    dec += rem*base;
                    base=base*2;
        }
                cout<<"Decimal value is:"<<dec;
                break;
      }
            case 3: exit(0);
            default: cout<<"You have entered invalid choice";
    }
  }
    return 0;
}

OUTPUT:
PROGRAM 2

Write a C++ program to find the roots of non-linear equation by


using bisection method.

#include<iostream>
#include<math.h>
using namespace std;
#define ESP 0.001
double func1(double x)
{
    return x*x*x - 6*x*x + 11*x - 6;
}
double func2(double x)
{
    return 3*x*x - 7*x +1;
}
int main()
{
    double a,b;
    int choice;
    cout<<endl<<"1. Cubic Equation";
    cout<<endl<<"2. Qudratic Equation";
    cout<<endl<<"Enter your choice:";
    cin>>choice;
    cout<<endl<<"Enter the value of a and b:";
    cin>>a>>b;
    switch(choice)
  {
        case 1:
    {
            if(func1(a)*func1(b) >=0)
      {
                cout<<"Invalid Input"<<endl;
                goto end;
      }
            double c;
            while(fabs(b-a)>=ESP)
      {
                c=(a+b)/2;
                if(func1(c)*func1(a)<0)
                    b=c;
                else
                    a=c;
      }
            cout<<"The value of root is:"<<c;
            goto end;
    }
        case 2:
    {
            if(func2(a)*func2(b) >=0)
      {
                cout<<"Invalid Input"<<endl;
                goto end;
      }
            double c= a;
            while(fabs(b-a)>=ESP)
      {
                c=(a+b)/2;
                if(func2(c)*func2(a)<0)
                    b=c;
                else
                    a=c;
      }
            cout<<"The value of root is:"<<c;
            goto end;
    }
        default : cout<<"Invalid choice";
  }
    end:
    return 0;
}
OUTPUT:
PROGRAM 3

Write a C++ Program to find the roots of non-linear equation


using Secant method.

#include<iostream>
#include<stdlib.h>
#include<math.h>
#define f(x) x*x*x - 4*x*x - 6*x + 5
using namespace std;
int main()
{
    float x0,x1,x2,f0,f1,f2,e;
    int step=1,N;
    cout<<"Enter the first guess:";
    cin>>x0;
    cout<<"Enter the second guess:";
    cin>>x1;
    cout<<"Enter the mathematical error:";
    cin>>e;
    cout<<"Enter the maximum iteration:";
    cin>>N;
    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<<": x2= "<<x2<<" and f(x2)=
"<<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:
PROGRAM 4

Write a C++ Program to find the roots of non-linear equation


using Newton’s method.

#include<iostream>
#include<stdlib.h>
#include<math.h>
#define f(x) 3*x*x - cos(x) - 1
#define g(x) 6*x + sin(x)
using namespace std;
int main()
{
    float x0,x1,f0,f1,g0,e;
    int step=1,N;
    cout<<"Enter the inital guess:";
    cin>>x0;
    cout<<"Enter the mathematical error:";
    cin>>e;
    cout<<"Enter the maximum iteration:";
    cin>>N;
    do
  {
        g0=g(x0);
        f0=f(x0);
        if(g0==0.0)
    {
            cout<<"Mathematical Error.";
            exit(0);
    }
        x1=x0-f0/g0;
        cout<<"Iteration-"<<step<<": x= "<<x1<<" and f(x1)=
"<<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:
PROGRAM 5

Write a C++ program to implement numerical integration


using Trapezoidal rule.

#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 the lower limit of integration:";
    cin>>lower;
    cout<<"Enter the upper limit of integration:";
    cin>>upper;
    cout<<"Enter the number of subintervals:";
    cin>>subinterval;
    stepsize= (upper-lower)/subinterval;
    integration= f(lower) + f(upper);
    for(i=1;i<=subinterval-1;i++)
  {
        k=lower+i*stepsize;
        integration=integration + 2 * (f(k));
  }
    integration=integration * stepsize/2;
    cout<<endl<<"Required value of integration is:"<<integration;
    return 0;
}
OUTPUT:
PROGRAM 6

Write a C++ Program to implement numerical integration


using Simpson 1/3 Rule.

#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 subinterval;
    cout<<"Enter the lower limit of integration:";
    cin>>lower;
    cout<<"Enter the upper limit of integration:";
    cin>>upper;
    cout<<"Enter the number of subintervals:";
    cin>>subinterval;
    stepsize=(upper-lower)/subinterval;
    integration= f(lower) + f(upper);
    for(int 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:
PROGRAM 7

Write a C++ Program to implement numerical integration


using Simpson 3/8 Rule.

#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 subinterval;
    cout<<"Enter the lower limit of integration:";
    cin>>lower;
    cout<<"Enter the upper limit of integration:";
    cin>>upper;
    cout<<"Enter the number of subintervals:";
    cin>>subinterval;
    stepsize=(upper-lower)/subinterval;
    integration= f(lower) + f(upper);
    for(int 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:
PROGRAM 8

Write a C/C++ Program to solve the system of linear


equations using Gauss - elimination method.

#include<iostream>
using namespace std;
#define size 10
int main()
{
    float a[size][size],x[size],ratio;
    int i,j,k,n;
    cout<<"Enter the number of unknowns:";
    cin>>n;
    cout<<"Enter the coefficients of Augmented Matrix:"<<endl;
    for(i=1;i<=n;i++)
  {
        for(j=1;j<=n+1;j++)
    {
            cout<<"a["<<i<<"]"<<"["<<j<<"]:";
            cin>>a[i][j];
    }
  }
    for(i=1;i<=n-1;i++)
  {
        if(a[i][i]==0.0)
    {
            cout<<"Mathematical Error!";
            exit(0);
    }
        for(j=i+1;j<=n;j++)
    {
            ratio = a[j][i]/a[i][i];
            for(k=1;k<=n+1;k++)
      {
                a[j][k]=a[j][k] - ratio*a[i][k];
      }
    }
  }
    x[n] = a[n][n+1]/a[n][n];
    for(i=n-1;i>=1;i--)
  {
        x[i]=a[i][n+1];
        for(j=i+1;j<=n;j++)
    {
            x[i]=x[i] - a[i][j]*x[j];
    }
        x[i]=x[i]/a[i][i];
  }
    cout<<endl<<"Solution: "<<endl;
    for(i=1;i<=n;i++)
  {
        cout<<"x["<<i<<"]:"<<x[i]<<endl;
  }
    return 0;
}
OUTPUT:
PROGRAM 9

Write a C++ Program to implement Langrange’s


interpolation formula.

#include<iostream>
using namespace std;
int main()
{
    float x[100],y[100],xp,yp,p;
    int i,j,n;
    cout<<"Enter the number of Coordinates:";
    cin>>n;
    cout<<"Enter the Data:"<<endl;
    for(int i=1;i<=n;i++)
  {
        cout<<"x["<<i<<"]:";
        cin>>x[i];
        cout<<"y["<<i<<"]:";
        cin>>y[i];
  }
    cout<<endl<<"Enter Interpolation Point:";
    cin>>xp;
    for(i=1;i<=n;i++)
  {
        p=1;
        for(j=1;j<=n;j++)
    {
            if(i!=j)
      {
                p=p*(xp-x[j])/(x[i]-x[j]);
      }
    }
        yp=yp+p*y[i];
  }
    cout<<endl<<"Interpolated value at "<<xp<<" is "<<yp;
    return 0;
}

OUTPUT:
PROGRAM 10

Write a C++ Program to find numerical solution of ordinary


differential equations by Euler’s method.

#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 Conditions"<<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"<<endl;
    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<<endl<<"Value of y at x = "<<xn<<" is "<<yn;
    return 0;
}

OUTPUT:
PROGRAM 11

Write a C++ Program to find numerical solution of ordinary


differential equations by Runge-Kutta method.

RK-2

#include<iostream>
using namespace std;
float f(float x,float y)
{
    return (x*x*x)/(2*y);
}
int main()
{
    float x0,xn,y0,yn,h,k,k1,k2,n;
    int i;
    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;
    for(i=0;i<n;i++)
  {
        k1=h*f(x0,y0);
        k2=h*f(x0+h,y0+k1);
        k=(k1+k2)/2;
        yn=y0+k;
        x0=x0+h;
        y0=yn;
        cout<<"\nValue of y at x="<<x0<<" is "<<y0;
  }
    return 0;
}

OUTPUT:
RK-3

#include<iostream>
using namespace std;
float f(float x,float y)
{
    return (x*x*x)/(2*y);
}
int main()
{
    float x0,xn,y0,yn,h,k,k1,k2,k3,n;
    int i;
    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;
    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,y0+2*k2-k1);
        k=(k1+4*k2+k3)/6;
        yn=y0+k;
        x0=x0+h;
        y0=yn;
        cout<<"\nValue of y at x="<<x0<<" is "<<y0;
  }
    return 0;
}

OUTPUT:
RK-4

#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"<<endl;
    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:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy