Friday 26 February 2016




















GAUSS SEIDEL METHOD

 /* Note: The system of equations must be diagonally dominant */
void main()
{


/* Variable declaration */

int n,p,i,j,m;
float a[10][10], x[10], b[10], c;
clrscr();
printf("Enter the number of variables");
scanf("%d", &n);
printf("enter the number of required iterations\n");
scanf("%d",&p);
printf("enter the right hand side matrix\n");


/* loop to read right-hand side constants */

for(i=0; i<n; i++)
{
scanf("%f", &b[i]);
}


/* Loops to read coefficient matrix */


printf("enter the left hand side matrix");
for(i=0; i<n; i++)
{
x[i]=0;
for(j=0; j<=n-1; j++)
{
scanf("%f", &a[i][j]);
}
}


/* Execution of Seidel iterations */

m=1;
line:
for(i=0; i<n; i++)
{
c=b[i];
for(j=0; j<n; j++)
{
if(i!=j)
c=c-a[i][j]*x[j];
}
x[i]=c/a[i][i];
printf("\n Solution: x[%d]=%f", i, x[i]);
}
m++;
if(m<=p)
goto line;
else
{
printf("\n required iterations completed");
}
getch();


}


Sunday 21 February 2016



LAGRANGE INTERPOLATION C-SOURCE CODE

\* define library functions before going to function main */
void main()
{
int n,i,j,c;
float f[10],x[10],y[10],t=1,sum=0,p,s=1;
clrscr();
printf("enter the number of data pairs\t");
scanf("%d",&n);

/* Read Data pairs from table */
for(i=0;i<n;i++)
{
printf("\n enter the value of x[%d]:\t",i);
scanf("%f",&x[i]);
printf("\n enter the value of f[x[%d]:\t",i);
scanf("%f", &y[i]);
}
printf("\n enter x for which you have to find f(x):\t");
scanf("%f",&p);
/* execution of lagrange polynomial at the entered value p */
for(i=0;i<n;i++)
{
t=1;
s=1;
for(j=0;j<n;j++)
{
if(j!=i)
{
t=t*(p-x[j]);
s=s*(x[i]-x[j]);
}
}
sum=sum+(t/s)*y[i];
}

/*Print the value of f(x) at p*/
printf("\n value of f at p=%f", sum);
getch();
}