Pages

Saturday 31 March 2018

Inverse of a Matrix using Gauss Jordan Method in C/C++

PROGRAM :

/*To write a C/C++ program to find the inverse of a matrix using Gauss Jordan Method.*/

#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
    int i, j, k, n;
    float a[10][10] = { 0 }, d;
    cout << "No of rows ";
    cin >> n;
    cout << "\nEnter the elements of the matrix row-wise:\n";
    for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
            cin >> a[i][j];

    for (i = 1; i <= n; i++)
        for (j = 1; j <= 2 * n; j++)
            if (j == (i + n))
                a[i][j] = 1;

    for (i = n; i > 1; i--)
    {
        if (a[i - 1][1] < a[i][1])
            for (j = 1; j <= n * 2; j++)
            {
                d = a[i][j];
                a[i][j] = a[i - 1][j];
                a[i - 1][j] = d;
            }
    }
    cout << "pivoted output: " << endl;
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n * 2; j++)
            cout << a[i][j] << "    ";
        cout << endl;
    }

    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n * 2; j++)
            if (j != i)
            {
                d = a[j][i] / a[i][i];
                for (k = 1; k <= n * 2; k++)
                    a[j][k] -= a[i][k] * d;
            }
    }
    for (i = 1; i <= n; i++)
    {
        d = a[i][i];
        for (j = 1; j <= n * 2; j++)
            a[i][j] = a[i][j] / d;
    }

    cout <<"\nInverse of the matrix is as follows :\n";
    for (i = 1; i <= n; i++)
    {
        for (j = n + 1; j <= n * 2; j++)
            cout << a[i][j] << "    ";
        cout << endl;
    }
    return 0;
}



OUTPUT :

No of rows 3

Enter the elements of the matrix row-wise:
 2    2    3
 2    1    1
 1    3    5

pivoted output:
 2    2    3    1    0    0
 2    1    1    0    1    0
 1    3    5    0    0    1

Inverse of the matrix is as follows :
 2    -1    -1
-9     7     4
 5    -4    -2

Gauss Jordan Method in C/C++

PROGRAM :

/*To write a C/C++ program to find the solution of
the system of linear equations using Gauss Jordan
Method.*/

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n,i,j,k;
    cout<<"\nEnter the no. of equations\n";        
    cin>>n;
    double a[n][n+1],x[n];
    cout<<"\nEnter the elements of the augmented-matrix row-wise:\n";
    for (i=0;i<n;i++)
        for (j=0;j<=n;j++)    
            cin>>a[i][j];
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n+1; j++)
            if (j != i)
            {
                double d = a[j][i] / a[i][i];
                for (k = 0; k < n+1; k++)
                    a[j][k] -= a[i][k] * d;
            }
    }
    cout<<"\n\nThe matrix after gauss-jordan elimination is as follows:\n";
    for (i=0;i<n;i++)
    {
        for (j=0;j<=n;j++)
            printf("%4f\t",a[i][j]);
printf("\n");
    }
    for(i=1;i<n;i++)
    {
    for (j=0;j<i;j++)
    {
    a[i][j]=0;
}
}
    for (i=n-1;i>=0;i--)
    {
x[i]=a[i][n];
for (j=i+1;j<n;j++)
            if (j!=i)
x[i]=x[i]-a[i][j]*x[j];
        x[i]=x[i]/a[i][i];
    }
    
    cout<<"\nThe values of the variables are as follows:\n";
    
    for (i=0;i<n;i++){
        cout<<(x[i])<<endl;
}
    return 0;
}



OUTPUT :

Enter the no. of equations
4

Enter the elements of the augmented-matrix row-wise:
10 -7 3 5 6
-6 8 -1 -4 5
3 1 4 11 2
5 -9 -2 4 7


The matrix after gauss-jordan elimination is as follows:
10.000000    0.000000    0.000000    0.000000    50.000000
0.000000     3.800000    0.000000    0.000000    15.200000
0.000000     0.000000    2.447368    0.000000   -17.131579
0.000000     0.000000    0.000000    9.924731     9.924731

The values of the variables are as follows:
5
4
-7
1

Gauss Elimination Method in C/C++

PROGRAM :

/*To write a C/C++ program to find the solution of
the system of linear equations using Gauss Elimination
Method.*/

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n,i,j,k;
    cout<<"\nEnter the no. of equations\n";        
    cin>>n;
    float a[n][n+1],x[n];
    cout<<"\nEnter the elements of the augmented-matrix row-wise:\n";
    for (i=0;i<n;i++)
        for (j=0;j<=n;j++)    
            cin>>a[i][j];
    for (i=0;i<n-1;i++){
        for (k=i+1;k<n;k++)
            {
                double t=a[k][i]/a[i][i];
                for (j=0;j<=n;j++)
                    a[k][j]=a[k][j]-t*a[i][j];
            }
}
    cout<<"\n\nThe matrix after gauss-elimination is as follows:\n";
    for (i=0;i<n;i++)
    {
        for (j=0;j<=n;j++)
            printf("%4f\t",a[i][j]);
printf("\n");
    }
    for(i=1;i<n;i++)
    {
    for (j=0;j<i;j++)
    {
    a[i][j]=0;
}
}
    for (i=n-1;i>=0;i--)
    {
x[i]=a[i][n];
for (j=i+1;j<n;j++)
            if (j!=i)
x[i]=x[i]-a[i][j]*x[j];
        x[i]=x[i]/a[i][i];
    }
    
    cout<<"\nThe values of the variables are as follows:\n";
    
    for (i=0;i<n;i++){
    if (i==n-1)
    cout<<floor(x[i])<<endl;
    else 
        cout<<round(x[i])<<endl;
}
    return 0;

}


OUTPUT :

Enter the no. of equations
4

Enter the elements of the augmented-matrix row-wise:
10 -7 3 5 6
-6 8 -1 -4 5
3 1 4 11 2
5 -9 -2 4 7


The matrix after gauss-elimination is as follows:
10.000000   -7.000000    3.000000    5.000000    6.000000
0.000000     3.800000    0.800000   -1.000000    8.600000
-0.000000   -0.000000    2.447368   10.315789   -6.815791
0.000000    -0.000000   -0.000000    9.924731    9.924732

The values of the variables are as follows:
5
4
-7
1

Newton Raphson Method in C/C++

PROGRAM :

/*To write a C/C++ program to find roots of an equation using 
Newton Raphson method.*/

#include<iostream>
#include<cmath>
using namespace std;
float a,b,err,x1,x2,x0;
int maxitr,i=1,flag=0;
float f(float x){
return x*log10(x)-1.2;}
float df(float x){
return log10(x)+0.43429;}
int main(){
cout<<"Enter value of a and b :";
cin>>a>>b;
if(f(a)*f(b)>0)
cout<<"No Root or Even Root";
else{
cout<<"Enter maximum iteration and allowed error :";
cin>>maxitr>>err;
x0=(a+b)/2;
while(maxitr>0){
x1=x0;
x2=x1-(f(x1)/df(x1));
cout<<"After "<<i<<" iteration value of x ="<<x2<<"\n";
if(fabs(x1-x2)<err){
cout<<"After "<<i<<" iteration value of x is"<<x2<<"\n";
flag=1;
break;
}
x0=x2;
i++;
maxitr--;
}
if(flag==0)
cout<<"maximum iteration is insufficient";}
return 0;

}


OUTPUT :

Enter value of a and b :5 1
Enter maximum iteration and allowed error :25
0.0001
After 1 iteration value of x = 2.74615
After 2 iteration value of x = 2.74065
After 3 iteration value of x = 2.74065

After 3 iteration value of x is 2.74065

Regula-Falsi Method in C/C++

PROGRAM :

//To write a C/C++ program to find roots of an equation using
//Regula-Falsi method.

#include<stdio.h>
#include<math.h>
float bisect(float a,float b);
float f(float x);
float f(float x)
{
float f;
f=cos(x)-x*exp(x);
return f;
}
float bisect(float a,float b)
{
float c=((b*f(a))-(a*f(b)))/(f(a)-f(b));
return c;
}
void main()
{
float a,b,x0,ae,x1;
int itr,maxitr;
printf("Enter a & b :");
scanf("%f%f",&a,&b);
if (f(a)*f(b)<0)
{
printf("Enter the no. of iterations required and Allowed error");
scanf("%d%f",&maxitr,&ae);
x0=bisect(a,b);
printf("Iteration no. 0 value of x is %f\n",x0);
for(itr=1;itr<=maxitr;itr++)
{
if (f(x0)*f(a)<0)
b=x0;
else a=x0;
x1=bisect(a,b);
printf("Iteration no. %d value of x is %f\n",itr,x1);
if ((fabs(x1-x0))<=ae)
{
printf("root is %f",x1);
break;
}
x0=x1;
}
if (itr==maxitr && fabs(x1-x0)>ae)
printf("Number of iterations is not sufficient");
}


}


OUTPUT :

Enter a & b :0 1
Enter the no. of iterations required and Allowed error25
0.0001

Iteration no. 0 value of x is 0.314665
Iteration no. 1 value of x is 0.446728
Iteration no. 2 value of x is 0.494015
Iteration no. 3 value of x is 0.509946
Iteration no. 4 value of x is 0.515201
Iteration no. 5 value of x is 0.516922
Iteration no. 6 value of x is 0.517485
Iteration no. 7 value of x is 0.517668
Iteration no. 8 value of x is 0.517728

root is 0.517728

Bisection Method in C/C++

PROGRAM :

//To write a C/C++ program to find roots of a polynomial using
//bisection method.


#include<stdio.h>
#include<math.h>
float bisect(float a,float b);
float f(float x);
float f(float x)
{
float f;
f=pow(x,3)-4*x-9;
return f;
}
float bisect(float a,float b)
{
float c=(a+b)/2;
return c;
}
void main()
{
float a,b,x0,ae,x1;
int itr,maxitr;
printf("Enter a & b :");
scanf("%f%f",&a,&b);
if (f(a)*f(b)<0)
{
printf("Enter the no. of iterations required and Allowed error");
scanf("%d%f",&maxitr,&ae);
x0=bisect(a,b);
printf("Iteration no. 0 value of x is %f\n",x0);
for(itr=1;itr<=maxitr;itr++)
{
if (f(x0)*f(a)<0)
b=x0;
else a=x0;
x1=bisect(a,b);
printf("Iteration no. %d value of x is %f\n",itr,x1);
if ((fabs(x1-x0))<=ae)
{
printf("root is %f",x1);
break;
}
x0=x1;
}
if (itr==maxitr && fabs(x1-x0)>ae)
printf("Number of iterations is not sufficient");
}

}




OUTPUT :

Enter a & b :2 3
Enter the no. of iterations required and Allowed error25
0.00001
Iteration no. 0 value of x is 2.500000
Iteration no. 1 value of x is 2.750000
Iteration no. 2 value of x is 2.625000
Iteration no. 3 value of x is 2.687500
Iteration no. 4 value of x is 2.718750
Iteration no. 5 value of x is 2.703125
Iteration no. 6 value of x is 2.710938
Iteration no. 7 value of x is 2.707031
Iteration no. 8 value of x is 2.705078
Iteration no. 9 value of x is 2.706055
Iteration no. 10 value of x is 2.706543
Iteration no. 11 value of x is 2.706299
Iteration no. 12 value of x is 2.706421
Iteration no. 13 value of x is 2.706482
Iteration no. 14 value of x is 2.706512
Iteration no. 15 value of x is 2.706528
Iteration no. 16 value of x is 2.706535

root is 2.706535



Friday 23 March 2018

Fuel Cell Electric Vehicle



Honda Clarity - 2017
(PC: caranddriver.com)

fuel cell vehicle (FCV) or fuel cell electric vehicle (FCEV) is a type of electric vehicle which uses a fuel cell, instead of a battery, or in combination with a battery or supercapacitor, to power its on-board electric motor. Fuel cells in vehicles generate electricity to power the motor, generally using oxygen from the air and compressed hydrogen. Most fuel cell vehicles are classified as zero-emissions vehicles that emit only water and heat. As compared with internal combustion vehicles, hydrogen vehicles centralize pollutants at the site of the hydrogen production, where hydrogen is typically derived from reformed natural gas. Transporting and storing hydrogen may also create pollutants.
Fuel cells have been used in various kinds of vehicles including forklifts, especially in indoor applications where their clean emissions are important to air quality, and in space applications. The first commercially produced hydrogen fuel cell automobiles began to be sold by Toyota and leased on a limited basis by Hyundai in 2015, with additional manufacturers planning to enter the market. As of June 2016, the Toyota Mirai is available for retail sale in Japan, California, the UK, Denmark, Germany, Belgium, and Norway. Furthermore, fuel cells are being developed and tested in buses, boats, motorcycles and bicycles, among other kinds of vehicles.
All fuel cells are made up of three parts: an electrolyte, an anode and a cathode. In principle, a hydrogen fuel cell functions like a battery, producing electricity, which can run an electric motor. Instead of requiring recharging, however, the fuel cell can be refilled with hydrogen.
And here is the link to Poster made by Zubair: Poster Link