Saturday, 15 September 2012

To implements the Lagrange interpolation and Newton Gregory forward interpolation



       Algorithm:

Step 1. Read x,n

Step2. for i=1 to (n+1) is steps of 1 do Read xi,fi end for {the above 
           statements reads x,s and the corresponding values of  f is }

Step 3. Sum=0

Step 4. for i=1 to (n+1) in steps of 1 do

Step 5. Profvnc=1

Step 6. for J=1 to (n+1) in steps of 1 do

Step 7. If (j≠i) then prodfunc=prodfunc X(x-xj) / (xi-xj) endfor

Step 8. Sum=Sum+fi x Prodfunc {sum is the value of f at x} end for

Step 9. Write x, sum

Step 10. STOP
Program:

#include<stdio.h>
#include<math.h>
Main()
{
  Float y, x[20],f[20],sum,pf;
  Int I,j,n;
  Printf(“enter the value of n”);
  Scanf(“%d”,&n);
  Printf(“enter the value to be found”);
  Scanf(“%f”,&y);
  Printf(“enter the values of xi’s & fi’s”);
  For(i=0;i<n;i++)
   {
        Pf=1;
         For(j=0;j<n;j++)
         {
                     If(j!=i)
                                 Pf  *= (y-x[j])/(x[i] – x[j]);
         }
         Sum += f[i] *pf;
 }
Printf(“\nx = %f ”,y);
Printf(“\n sum =%f ”,sum);
}

Input/Output:

Enter the value of n 4
Enter the value to be found 2.5
Enter the values for xi’s & fi’s
1                    1
2                    8
3                    27
4                    64
         X = 2.500000
         Sum = 15.625000
Conclusion: The program is error free

VIVA QUESATIONS
1) Define storage class ?
Ans: Storage class specifiers inform the complier how to store the variable; the storage clas specifiers in the c language are : auto, register, static,extern, typedef
           Newton  gregory  forward interpolation.

          Algorithm:

Step1: START

Step2: Read n

Step3: for i=0 to (n-1) do read xi,yi

Step4: read x

Step5: h←xi-x0

Step6: p←(x-xo)/n

Step7: for j=0 to n-2 do
   ∆1yj←yj+1-∆i-1

Step8: k←n-2

Step9: for i=2 to (n-1)do
    Step9.1: k←k-1
    Step9.2:for j=0 to  k do
      ∆iyj←∆i-1 yj+1-∆i-1yj

Step10:   Sumy←y0

Step11: Pvalue←1

Step12: Fact value←1

Step13: for l=1 to (n-1) do
   Step13.1: Pvalue←pvalue x (p-(l-1))
Step13.2: factvalue←factvaluex1
Step13.3: term←(pvalue x ∆ly) / factvalue
Step13.4: Sumy←Sumy+term

Step14: Print x,SUMY

Step15: STOP
 Program:

#include<stdio.h>
#include<math.h>
Main()
{
    Int i, j, n, k, l;
    Float sumy, h, term, p, z, pvalue;
    Float x[25], y[25], d[25][25], factvalue;
    Printf(“enter the value of n”);
    Scanf(“%d”,&n);
    Printf(“enter %d values for x, y \n”,n);
    For(i=0;i<n;i++)
     Scanf(“%f %f”, &x[i], &y[i]);
     Printf(“\n enter z”);
     Scanf(“%f”,&z);
     h = x[1] – x[0];
     p = (z - x[0] )/ h;
     for(j=0; j<n-2; j++)
         d[i][j] =y[j+1] – y[j];
         k=n-2;
    for(i=2; i<n; i++)
    {
         k++;
         for(j=0; j<=k; j++)
           d[i][j] =d[i-1][j+1] – d[i-1][j];
   }
   For(l=1; l<n; l++)
   {
         Pvalue *= (p-(l  - 1));
         Factvalue *= 1;
         Term = pvalue* d[l][0] / factvalue;
         Sumy += term;
 }
 Printf(“\n y value at z = %f is %f”, z, sumy);
}










Input/Output:

Enter n 7
Enter 7 data values for x, y

                  1921                35
1931                42
1941                58
1951                84
1961               120

1971              165
1981              220

Enter z 1925
Y value at z = 1925.000000 is 36.756710
Conclusion: The program is error free




VIVA QUESATIONS
1) What is the use of goto statement ?
Ans: The goto statement is used to alter the normal sequence of the program execution by unconditionally transferring control to some other part of the program.
 2) What is the use of continue statement ?
Ans: The continue statement is used to bypass the remainder of the current pass through a loop















No comments:

Post a Comment