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

Tuesday, 14 February 2017

Triangle Of Asterisk '*' in C Language

#include<stdio.h>
int main ()
{
int n,t,i,j,k;
printf("Enter a number");
scanf("%d",&n);
n=n-1;
k=n;
for (i=0;i<=n;i++)
{ j=k;
k=k-1;
while(j!=0)
{printf(" ");
j=j-1;}
for(t=0;t<=i;t++)
{printf("* ");
}
printf("\n");
}
}





Output

Enter a number 
5
    *
   * * 
  * * *
 * * * * 
* * * * *

Monday, 13 February 2017

Electromagnetism--One Of The Groundbreaking Phenomenon

electromagnetism
ɪˌlɛktrəʊˈmaɡnɪtɪz(ə)m/
noun
noun: electromagnetism; noun: electro-magnetism
  1. the phenomenon of the interaction of electric currents or fields and magnetic fields.
    • the branch of physics concerned with electromagnetism.






  2. Electromagnetism is a branch of physics involving the study of the electromagnetic force, a type of physical interaction that occurs between electrically charged particles. The electromagnetic force usually exhibits electromagnetic fields such as electric fieldsmagnetic fields, and light and is one of the four fundamental interactions (commonly called forces) in nature. The other three fundamental interactions are the strong interaction, the weak interaction, and gravitation.



Friday, 27 January 2017

Complexity: What is the most complicated thing ever?

Do you think a Rubik’s cube is the most complicated mechanical puzzle ever invented? You have no idea how complicated mechanical puzzles can be.
A Standard 3x3x3 Rubik’s cube can have 43,252,003,274,489,856,000 combinations, but only 1 solution.
To put this into perspective, if we have one Rubik’s cube for each possible combination, then all the cubes will cover the whole surface of 275 earth-sized planets. and among all of them, only 1 cube will be in perfectly solved state.
But for some people, this was not complicated enough. they created 4x4x4 cubes, and also 5x5x5 cubes.
A 4x4x4 cube has 7401196841564901869874093974498574336000000000 combinations. If you have that many teaspoons of sugar, it will fill a sphere the size of the solar system 3.5 times over.
a 5x5x5 Rubik’s cube has 282870942277741856536180333107150328293127731985672134721536000000000000000 combinations, this is close to number of atoms in the known universe. and yet, people can solve it within few minutes.
So, some people though it was not complicated enough, they created this monster:
Say hello to 11x11x11 cube.



Is it world’s most complicated cube? Nope. The largest cube ever made is 17x17x17. Having a total combination of 66.9 * 10^1053. This number is so huge it’s digits can not even be written in here.
Yet, someone solved this thing in 7.5 hours.
Are Rubik’s cubes are the most complicated puzzle ever? Nope! Lets meet Minxes.
Minxes are like Rubik’s cube, but with more than 6 sides. Having more sides also makes them more complicated.
This is a 3x3x3 MEGAMINX. It has 12 faces and total 50 moving parts, whereas the Rubik’s cube has only 6 faces and 20 moving parts.



A 3x3x3 MEGAMINX
has 100669616553523347122516032313645505168688116411019768627200000000000 combinations, i.e. 1000 billion billion (yes there are two billions) more times than a 4x4x4 rubics cube.
The world record for fastest MEGAMINX solving is 37.58 seconds.
Still not complex enough? meet the GIGAMINX, with a 5x5x5 structure, the Teraminx with 7x7x7 structure and the Examinx, with 9x9x9 structure.



And this is how it looks when scrambled.

The combinations possible with a EXAMINX is not worth calculating…
But wait.
Someone has made a YOTAMINX, a minx with 15x15x15 structure.
Not sure if anyone was able to solve it or not.



And for people with nerve of steel, here is a TUTTAMINX with 32 faces and 150 movable parts.




If you scramble this, it looks like this:





After seeing all these, if you think a puzzle can not be more complicated than a Tuttaminx, you are wrong!
Multiple Minxes can be fused together to form a combined minx. Here is a triple fused Petaminx.



These puzzles have no practical use for mortals. Although in Hell, they may ask you to solve one.


 There are Shape shifting puzzles with non-linear movements as well, like the "GHOST CUBE".


This is how it looks when scrambled, Goodbye brain.




Due to each piece being of a different shape, this puzzle is notoriously difficult to solve. Unlike a Rubik’s cube the movements of the pieces are restricted due to their shape, so it’s very difficult to mathematically calculate the number of possible moves with this one.
R.I.P brain.!!