Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Numerical MethodsRSS Feeds

FIRST ORDER Difference

Posted By: Bingham Fischer     Category: C Programming     Views: 3616

Program of FIRST ORDER Difference.

Code for FIRST ORDER Difference in C Programming

#include<stdio.h>
#include<conio.h>
#define N    10

float d[N-1][N-1];

void main()
{
    int i,j,choice,n;
    FILE *fp;
    float x[N],y[N];
    void first_order(float x[],int n,FILE *fp);
    fp = fopen("diffnewf.dat","w");
    clrscr();
    printf("\nENTER NO. OF DATA = ");
    fprintf(fp,"\nENTER NO. OF DATA = ");
    scanf("%d",&n);
    fprintf(fp,"%d",n);
    printf("\nENTER DATA OF VARIABLE X\n");
    fprintf(fp,"\nENTER DATA OF VARIABLE X\n");
    for(i=0;i<n;i++)
    {
        printf("\nENTER VALUE OF ELEMENT %c[%d] = ",'x',i);
        fprintf(fp,"\nENTER VALUE OF ELEMENT %c[%d] = ",'x',i);
        scanf("%f",&x[i]);
        fprintf(fp,"%f",x[i]);
    }
    printf("\nENTER DATA OF VARIABLE Y\n");
    fprintf(fp,"\nENTER DATA OF VARIABLE Y\n");

    for(i=0;i<n;i++)
    {
        printf("\nENTER VALUE OF ELEMENT %c[%d] = ",'y',i);
        fprintf(fp,"\nENTER VALUE OF ELEMENT %c[%d] = ",'y',i);
        scanf("%f",&y[i]);
        fprintf(fp,"%f",y[i]);
    }
    for(i=0;i<n-1;i++)
    {
        for(j=0;j<n-(i+1);j++)
        {
            if(i==0)
            {
                d[j][i] = y[j+1]-y[j];
            }
            else
            {
                d[j][i] = d[j+1][i-1] - d[j][i-1];
            }
        }
    }
    printf("\n\n");
    fprintf(fp,"\n\n");
    printf(" x       y   ");
    fprintf(fp," x       y   ");
    for(i=1;i<n;i++)
    {
        printf("   d^%dy(i) ",i);
        fprintf(fp,"   d^%dy(i) ",i);
    }
    printf("\n\n");
    fprintf(fp,"\n\n");
    /*printf("\n   xi\t     yi\t\t");
fprintf(fp,"\n xi\t yi\t\t");
for(i=1;i<n;i++)
{
printf("d^%dyi\t ",i);
fprintf(fp,"d^%dyi\t ",i);
}
printf("\n\n");
fprintf(fp,"\n\n");*/
/*for(i=0;i<n;i++)
{
printf("\nx%d=%.3f y%d=%.3f",i,x[i],i,y[i]);
fprintf(fp,"\nx%d=%.3f y%d=%.3f",i,x[i],i,y[i]);
for(j=0;j<n-(i+1);j++)
{
printf(" d^%dy%d=%.3f",j+1,i,d[i][j]);
fprintf(fp," d^%dy%d=%.3f",j+1,i,d[i][j]);
}
printf("\n\n");
fprintf(fp,"\n\n");
} */
for(i=0;i<n;i++) { printf(" %.2f %.2f ",x[i],y[i]); fprintf(fp," %.2f %.2f ",x[i],y[i]); for(j=0;j<n-(i+1);j++) { printf(" %.4f ",d[i][j]); fprintf(fp," %.4f ",d[i][j]); } printf("\n"); fprintf(fp,"\n"); } while(1) { printf("\n1. FIRST ORDER DEFFERENCE "); printf("\n2. QUIT"); printf("\n\n ENTER YOUR CHOICE = "); scanf("%d",&choice); switch(choice) { case 1: first_order(x,n,fp); break; case 2: exit(1); default: printf("\nENTER PROPER VALUE\n"); fprintf(fp,"\nENTER PROPER VALUE\n"); } } } void first_order(float x[],int n,FILE *fp) { int ans,i,j,k,l; float p,h,u,sum,sum1,product,dy; int fact(int a); do { ans = 0; printf("\nENTER VALUE AT WHICH POINT YOU WANT TO INTERPOLATE = "); fprintf(fp,"\nENTER VALUE AT WHICH POINT YOU WANT TO INTERPOLATE = "); scanf("%f",&p); fprintf(fp,"%f",p); if((p<x[0]) || (p>x[n-1])) { printf("\nVALUE LIES OUTSIDE TABULATED RANGE\n"); fprintf(fp,"\nVALUE LIES OUTSIDE TABULATED RANGE\n"); printf("\nDO YOU WANT TO INTERPOLATE FOR ANOTHER VALUE,"); fprintf(fp,"\nDO YOU WANT TO INTERPOLATE FOR ANOTHER VALUE,"); printf("\nENTER 1 FOR 'YES' AND 0 FOR 'NO' = "); fprintf(fp,"\nENTER 1 FOR 'YES' AND 0 FOR 'NO' = "); scanf("%d",&ans); fprintf(fp,"%d",ans); } }while(ans == 1); i=0; while(p >= x[i]) { i = i + 1; } k = i - 1; h = (x[k+1] - x[k]); u = ((p - x[k]) / h); sum = d[k][0]; for(i=1;i<n-(k+1);i++) { sum1 = 0; for(j=0;j<=i;j++) { product = 1; for(l=0;l<=i;l++) { if(l!=j) { product = product * (u - l); } } sum1 = sum1 + product; } sum = sum + ((d[k][i] * sum1) /fact(i+1)); } dy = (1 / h) * sum; printf("\nVALUE OF THE DERIVATIVE AT POINT %.3f IS dy/dx = %.4f\n",x[k],dy); fprintf(fp,"\nVALUE OF THE DERIVATIVE AT POINT %.3f IS dy/dx = %.4f\n",x[k],dy); } int fact(int a) { int i,fact=1; if(a==0) return(1); else { for(i=a;i>=1;i--) { fact = fact * i; } return(fact); } }
  
Share: 


Didn't find what you were looking for? Find more on FIRST ORDER Difference Or get search suggestion and latest updates.

Bingham Fischer
Bingham Fischer author of FIRST ORDER Difference is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!