Tuesday 22 May 2012

c program of gauss ELIMINITION method


/*Gauss Elimination */
# include <stdio.h>
# include <conio.h>
# include <math.h>
# define MAX 10
void main()
{
int i,j,n,k;
float mat[MAX][MAX],x[MAX],temp,pivot,sum=0;
clrscr();
printf("\t\t\t GAUSS ELIMINITION METHOD\n");
printf("-------------------------------------------------------------------\n");
printf("Enter No of Equtions : ");
scanf("%d",&n);
printf("Enter Coefficients of Eqution \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%f",&mat[i][j]);
printf("Enter Constant value\n");
for(i=1;i<=n;i++)
{
scanf("%f",&mat[i][n+1]);
x[i]=mat[i][n+1];
}
for(i=2;i<=n;i++)
{
for(j=i;j<=n;j++)
{
pivot=mat[j][i-1]/mat[i-1][i-1];
for(k=i-1;k<=n+1;k++)
mat[j][k]=mat[j][k]-pivot*mat[i-1][k];
}
}
printf("Eliminated matrix as :- \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
printf("\t%.2f",mat[i][j]);
printf("\n");
}
for(i=1;i<=n;i++)
{
if(mat[i][i]==0)
{
printf("Since diagonal element become zero\n Hence solution is not possible\n");
exit(1);
}
}
printf("Solution : \n");
for(i=0;i {
sum=0;
for(j=n;j>n-i;j--)
sum=sum+mat[n-i][j];

x[n-i]=(mat[n-i][n+1]-sum*x[n])/mat[n-i][n-i];
printf("X%d = %4.2f\n",n-i,x[n-i]);
}

getch();
}

OUTPUT :-


GAUSS ELIMINITION METHOD
------------------------------------------------------------------------------
Enter No of Equtions : 3
Enter Coefficients of Eqution
4 3 -2
1 1 1
3 -2 1
Enter Constant value
5 3 2
Eliminated matrix as :-
4.00 3.00 -2.00 5.00
0.00 0.25 1.50 1.75
0.00 0.00 28.00 28.00
Solution :
X3 = 1.00
X2 = 1.00
X1 = 1.00

facebook page of the blog

click here to see the facebook page of this blog.
click on LIKE................. :)

C Program Of NA Using Trapezoidal Interpolation


/*Trapezoidal rule */
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],y[MAX],sum=0,h,res;
clrscr();
printf("Solution using Trapezoidal rule \n");
printf("Enter Number of data points : ");
scanf("%d",&n);
printf("Enter value of table value set by set\n");
for(i=1;i<=n;i++)
{
printf("Enter value of x[%d] and y[%d] ",i,i);
scanf("%f%f",&x[i],&y[i]);
}
h=x[2]-x[1];
printf("\nInterval :=%f\n",h);
sum=y[1]+y[n];
for(i=2;i<n;i++)
sum=sum+y[i]*2;

res=sum*(h/2);
printf ("Integral from %3.2f to %3.2f is : = %f",x[1],x[n],res);
getch();
}
OUTPUT :-
Solution using Trapezoidal rule
Enter Number of data points : 6
Enter value of table value set by set
Enter value of x[1] and y[1] 7.47 1.93
Enter value of x[2] and y[2] 7.48 1.95
Enter value of x[3] and y[3] 7.49 1.98
Enter value of x[4] and y[4] 7.50 2.01
Enter value of x[5] and y[5] 7.51 2.03
Enter value of x[6] and y[6] 7.52 2.06

Interval :=0.010000
Integral from 7.47 to 7.52 is : = 0.099652

C Program Of NA Using Simpson 1/3 Rule


/*Simpson 1/3 Rule */
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],y[MAX],sum=0,h,res;
clrscr();
printf("Solution of 1/(1+x)dx using Simpson's 1/3 rule \n");
printf("Enter Number of data points : ");
scanf("%d",&n);
printf("Enter value of table value set by set\n");
for(i=1;i<=n;i++)
{
scanf("%f",&x[i]);
y[i]=1/(x[i]+1);
}
printf("X \t\t Y\n");
for(i=1;i<=n;i++)
printf("%f\t%f\n",x[i],y[i]);
h=x[2]-x[1];
printf("\nSegment Interval `h':=0.25\n");
sum=sum+y[1]+y[n];
for(i=2;i<n;i++)
{
if(i%2==0)
sum=sum+y[i]*4;
else
sum=sum+y[i]*2;
}
res=sum*(0.25/3);
printf ("Integral from %3.2f to %3.2f is : = %f",x[1],x[n],res);
getch();
}
OUTPUT
Solution of 1/(1+x)dx using Simpson's 1/3 rule
Enter Number of data points : 5
Enter value of table value for x column
0 .25 .50 .75 1
X Y
0.000000 1.000000
0.250000 0.800000
0.500000 0.666667
0.750000 0.571429
1.000000 0.500000

Segment Interval `h':=0.25
Integral from 0.00 to 1.00 is : = 0.693254

C Program Of NA Using Simpson 3/8 Rule


/*Simpson 3/8 rule */
# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int j,n=0;
float x[MAX],y[MAX],sum=0,h,res,lb,ub,i;
clrscr();
printf("Solution of 1/(1+x)dx using Simpson's 3/8 rule \n");
printf ("Enter initial value : ");
scanf("%f",&lb);
printf("Enter final value : ");
scanf("%f",&ub);
printf("Enter no. of Subinterval : ");
scanf("%f",&h);
h=(ub-lb)/h;
for(i=lb;i<=ub;i=i+h)
{
x[n]=i;
y[n]=1/(1+x[n]);
n++;
}
printf("No.\t X\t\tY=1/(1+x)\n");
printf("---------------------------------------\n");
sum=y[0]+y[n-1];
for(j=0;j<n;j++)
printf("%d\t%f\t%f\n",j,x[j],y[j]);
printf("---------------------------------------\n");
for(j=1;j<n-1;j++)
{
if(j%3==0)
sum=sum+y[j]*2;
else
sum=sum+y[j]*3;
}
res=((3*h)/8)*sum;
printf("\nIntegral from %f to %f when h = %f is = %f",x[0],x[n-1],h,res);
getch();
}

OUTPUT

Solution of 1/(1+x)dx using Simpson's 3/8 rule
Enter initial value : 6
Enter final value : 15
Enter no. of Subinterval : 8
No. X Y=1/(1+x)
---------------------------------------
0 6.000000 0.142857
1 7.125000 0.123077
2 8.250000 0.108108
3 9.375000 0.096386
4 10.500000 0.086957
5 11.625000 0.079208
6 12.750000 0.072727
7 13.875000 0.067227
8 15.000000 0.062500
---------------------------------------

Integral from 6.000000 to 15.000000 when h = 1.125000 is = 0.817303

C Program Of NA Using Runga-Kutta Mthod Of 4th Order

/* Runge-kutta method of 4th order to solve 10*dy/dx=(x*x)+(y*y)*/
# include <stdio.h>
# include <conio.h>
void main()
{
int c=0;
float x,y0,xp,h,i,y1,m1,m2,m3,m4;
float f(float,float);
clrscr();
printf("Solution by Runge kutta 2nd order Method\n");
printf("Enter initial Boundry condition x,y : ");
scanf("%f%f",&x,&y0);
printf("Enter Value of X at which Y is required : ");
scanf("%f",&xp);
printf("Enter Interval ,h : ");
scanf("%f",&h);
printf(" No. X\t m1 \t m2\t m3 \t m4 \t Y\n");
printf("-------------------------------------------------------------------\n");
for(i=x;i<=xp;i=i+h)
{
c++;
m1=h*f(i,y0);
m2=h*f(i+(h/2),y0+(m1/2));
m3=h*f(i+(h/2),y0+(m2/2));
m4=h*f(i+h,y0+m3);
y1=y0+((m1+2*m2+2*m3+m4)/6);
printf("%2d %5.6f %5.6f %5.6f %5.6f %5.6f %5.6f\n",c,i,m1,m2,m3,m4,y1);
y0=y1;
}
getch();
}
float f(float x,float y)
{
return ((x*x)+(y*y))/10;
}



OUTPUT



Solution by Runge kutta 2nd order Method
Enter initial Boundry condition x,y : 0 1
Enter Value of X at which Y is required : .09


Enter Interval ,h : .03
No. X m1 m2 m3 m4 Y
-------------------------------------------------------------------------------
1 0.000000 0.003000 0.003010 0.003010 0.003021 1.003010
2 0.030000 0.003021 0.003033 0.003033 0.003047 1.006043
3 0.060000 0.003047 0.003062 0.003062 0.003079 1.009106
4 0.090000 0.003079 0.003097 0.003097 0.003117 1.012204