You are on page 1of 31

S .

I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

To get more study materails clickhere > www.ktustudent.com

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


S . I N
T U N OTE
K

Downloaded from Ktunotes.in


Gauss Elimination Method
#include<iostream>
using namespace std;
int main()
{
int i,j,k,n;
float A[20][20],c,x[10],sum=0.0;
cout<<"\nEnter the order of matrix: ";
cin>>n;
cout<<"\nEnter the elements of augmented matrix row-wise:\n\n";
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
cout<<"A["<<i<<"]["<<j<<"] : ";
cin>>A[i][j];
}

E S . I N
NOT
}

KTU
for(j=1; j<=n; j++) /* loop for the generation of upper triangular matrix*/
{
for(i=1; i<=n; i++)
{
if(i>j)
{
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]-c*A[j][k];
}
}
}
}
x[n]=A[n][n+1]/A[n][n];

Downloaded from Ktunotes.in


/* this loop is for backward substitution*/
for(i=n-1; i>=1; i--)
{
sum=0;
for(j=i+1; j<=n; j++)
{
sum=sum+A[i][j]*x[j];
}
x[i]=(A[i][n+1]-sum)/A[i][i];
}
cout<<"\nThe solution is: \n";

for(i=1; i<=n; i++)


{
cout<<"\n x"<<i<<"= "<<x[i]; /* x1, x2, x3 are the required solutions*/
}
return(0);

E S . I N
NOT
}

KTU
OUTPUT
Enter the order of matrix: 4

Enter the elements of augmented matrix row-wise:

A[1][1] : 1
A[1][2] : 2
A[1][3] : 3
A[1][4] : 4
A[1][5] : 10
A[2][1] : 7
A[2][2] : 10
A[2][3] : 5
A[2][4] : 2
A[2][5] : 40
A[3][1] : 13
A[3][2] : 6

Downloaded from Ktunotes.in


A[3][3] : 2
A[3][4] : -3
A[3][5] : 34
A[4][1] : 11
A[4][2] : 14
A[4][3] : 8
A[4][4] : -1
A[4][5] : 64

The solution is:

x1= 1
x2= 2
x3= 3
x4= -1

Gauss Jordan Method

E S . I N
#include<iostream>

KTU
using namespace std;
NOT
int main()
{
int i,j,k,n;
float A[20][20],c,x[10];
cout<<"\nEnter the size of matrix: ";
cin>>n;
cout<<"\nEnter the elements of augmented matrix row-wise:\n";
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
cout<<"A["<<i<<"]["<<j<<"] : ";
cin>>A[i][j];
}
}

Downloaded from Ktunotes.in


/* Now finding the elements of diagonal matrix */
for(j=1; j<=n; j++)
{
for(i=1; i<=n; i++)
{
if(i!=j)
{
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]-c*A[j][k];
}
}
}
}
cout<<"\nThe solution is: \n";

E S . I N
NOT
for(i=1; i<=n; i++)

KTU
{
x[i]=A[i][n+1]/A[i][i];

cout<<"\n x"<<i<<"= "<<x[i];

}
return(0);
}
OUTPUT
Enter the size of matrix: 4

Enter the elements of augmented matrix row-wise:


A[1][1] : 1
A[1][2] : 1
A[1][3] : 1
A[1][4] : 1
A[1][5] : 2

Downloaded from Ktunotes.in


A[2][1] : 2
A[2][2] : 3
A[2][3] : -1
A[2][4] : 2
A[2][5] : 7
A[3][1] : 1
A[3][2] : 1
A[3][3] : 3
A[3][4] : -2
A[3][5] : -6
A[4][1] : 1
A[4][2] : 2
A[4][3] : 1
A[4][4] : -1
A[4][5] : -2

The solution is:

E S . I N
x1= 1
x2= 0
KTU NOT
x3= -1
x4= 2

Lagrange Interpolation Polynomial

#include<iostream>
using namespace std;
int main()
{
float x[100],y[100],a,s,t,sum=0;
int i,j,n;
cout<<"enter the number of terms available: ";
cin>>n;
cout<<"enter the values of x and corresponding y ";
for(i=0;i<n;i++)

Downloaded from Ktunotes.in


{
cout<<"\nx"<<i<<":";
cin>>x[i];
cout<<"y"<<i<<":";
cin>>y[i];

}
cout<<"enter the value of x whose y value is to be computed: ";
cin>>a;
for(i=0;i<n;i++)
{
s=1;
t=1;
for(j=0;j<n;j++)
{
if(i!=j)
{

E S . I N
NOT
s=s*(a-x[j]);

KTU
t=t*(x[i]-x[j]);
}

sum=sum+((s/t)*y[i]);
}
cout<<"value of y is"<<sum;
return 0;
}
Output
enter the number of terms available: 5
enter the values of x and corresponding y
x0:1
y0: 1

x1:2

Downloaded from Ktunotes.in


y1: 1.4142

x2:3
y2: 1.7321

x3:4
y3: 2

x4:5
y4: 2.2361
enter the value of x whose y value is to be computed: 2.5
value of y is1.58164

E S . I N
KTU NOT

Downloaded from Ktunotes.in

You might also like