Showing posts with label numerical methods. Show all posts
Showing posts with label numerical methods. Show all posts

Monday, June 2, 2014

Sourcecode of Newton's forward and backward interpolation using C

newton's forward and backward interpolation

#include<stdio.h>
#include<math.h>
int main()

{

    float x[10],y[15][15];
    int n,i,j;
//no. of items
    printf("Enter n : ");
    scanf("%d",&n);
    printf("X\tY\n");
    printf("enter the value of x then y\n");
    for(i = 0;i<n;i++){
            scanf("%f %f",&x[i],&y[i][0]);

    }


    printf("the entered value are\n");

     printf("x\t\ty\n");
     for(i = 0;i<n;i++){
            printf("\n");
            printf("%f\t%f",x[i],y[i][0]);

    }


    //forward difference table

    for(j=1;j<n;j++)
        for(i=0;i<(n-j);i++)
            y[i][j] = y[i+1][j-1] - y[i][j-1];
    printf("\n***********Forward Difference Table ***********\n");
//here is the  Forward Difference Table
    for(i=0;i<n;i++)
    {
        printf("\t%.2f",x[i]);
        for(j=0;j<(n-i);j++)
            printf("\t%.2f",y[i][j]);
        printf("\n");
    }
    // here is thebackward difference table
    for(j=1;j<n;j++)
//for j = 0 initially input is taken so we start from j=1
        for(i=n-1;i>(j-1);i--)
            y[i][j] = y[i][j-1] - y[i-1][j-1];
    printf("\n***********Backward Difference Table ***********\n");
//here is the Backward Difference Table
    for(i=0;i<n;i++)
    {
        printf("\t%.2f",x[i]);
        for(j=0;j<=i;j++)
            printf("\t%.2f",y[i][j]);
        printf("\n");
    }
return 0;

}

Sunday, June 3, 2012

SOURCE CODE FOR GAUSS ELIMINATION METHOD FOR FINDING THE SOLUTION OF LINEAR EQUATION


Gauss elimination method is one of the method of finding the solution of linear equationsin terms of matrix.
At first the equations are written in the form of matrix.Then the equations are solved.

HERE IS THE SOURCE CODE:



#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main()
{
        float **a,*temp,app,sum,mult;
        int i,j,k,n,p;
//take no of terms
        printf("Enter n : ");scanf("%d",&n);
//memory allocation
        a = (float**)malloc(n*sizeof(float*));
        for(i = 0; i < n; i++)
            a[i] = (float*)malloc(n*sizeof(float));
        temp = (float*)malloc(n*sizeof(float));
//take input from user
        printf("Enter the elements of augmended matrix rowwise\n");
        for(i=0;i<n;i++)
            for(j=0;j<=n;j++)
                scanf("%f",&a[i][j]);
//generation of scalar matrix
       for(i=0;i<(n);i++){
            app = a[i][i];
//initialization of p
            p = i;
//find largest no of the columns and row no. of largest no.
            for(k = i+1; k < n; k++)
            if(fabs(app) < fabs(a[k][i])){
               app = a[k][i] ;
               p = k;
            }
//swaping the elements of diagonal row and row containing largest no
            for(j = 0; j <= n; j++)
            {
                temp[j] = a[p][j];
                a[p][j] = a[i][j];
                a[i][j] = temp[j];
            }
//calculating triangular matrix
            for(j=i+1;j<n;j++){
                mult = a[j][i]/a[i][i];
                for(k=0;k<=n;k++)
                    a[j][k] -= mult*a[i][k];
            }
       }
//for calculating value of z,y,x via backward substitution method
        for(i=n-1;i>=0;i--)
        {
            sum = 0;
            for(j=i+1;j<n;j++)
                sum += a[i][j]*temp[j];
            temp[i] = (a[i][n]-sum)/a[i][i];
        }
        printf("****The matrix is : ***\n");
        for(i=0;i<n;i++){
            for(j=0;j<=n;j++)
                printf("%.2f\t",a[i][j]);
            printf("\n");
        }
//display solution
        printf("-------------The solution is ----------\n");
        for(i=0;i<n;i++)
        printf("X[%d] = %.2f\n",i+1,temp[i]);
//free allocated memory
        for(i = 0; i < n; i++)
            free(a[i]);
        free(a);
        free(temp);
    return 0;
}

NEWTON'S BACKWARD INTERPOLATION TABLE GENERATION SOURCE CODE IN NUMERICAL METHOD


Here is the source code for the generation of backward interpolation sourcecode.
note:   The IDE used is codeblocks andcode  is in C language format.so the file must be saved in .c format

+++++++++++++++++++
#include<stdio.h>
#include
<math.h>
int main()

{
    float x[10],y[15][15];
    int n,i,j;
//no. of items
    printf("Enter n : ");
    scanf("%d",&n);
    printf("X\tY\n");
    for(i = 0;i<n;i++){
            scanf("%f %f",&x[i],&y[i][0]);
    }
    //forward difference table
    for(j=1;j<n;j++)
        for(i=0;i<(n-j);i++)
            y[i][j] = y[i+1][j-1] - y[i][j-1];
    printf("\n***********Forward Difference Table ***********\n");
//display Forward Difference Table
    for(i=0;i<n;i++)
    {
        printf("\t%.2f",x[i]);
        for(j=0;j<(n-i);j++)
            printf("\t%.2f",y[i][j]);
        printf("\n");
    }
    //backward difference table
    for(j=1;j<n;j++)
//for j = 0 initially input is taken so we start from j=1
        for(i=n-1;i>(j-1);i--)
            y[i][j] = y[i][j-1] - y[i-1][j-1];
    printf("\n***********Backward Difference Table ***********\n");
//display Backward Difference Table
    for(i=0;i<n;i++)
    {
        printf("\t%.2f",x[i]);
        for(j=0;j<=i;j++)
            printf("\t%.2f",y[i][j]);
        printf("\n");
    }
return 0;
}