Page 1 of 2 12 Last
1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
34
Rep Power
5

#### Langrange interpolation

my code for above problem is:
#include<stdio.h>
float f[50];
float x[50];
int n;
float recur(int k,int i,float y)
{
if(i==n)
return 1.0;
if(i<n)
{
if(i!=k)
i++;
else
return (((y-x[i])/(x[k]-x[i]))*recur(k,i+1,y));
}
}
int main()
{
int i;
int j=0;
float z=0.0;
float p;
printf("enter elements");
scanf("%d",&n);
printf("enter values");
for(i=0; i<n; i++)
scanf("%f",&x[i]);
printf("enter f");
for(i=0; i<n; i++)
scanf("%f",&f[i]);
printf("enter the value at which u have to find");
scanf("%f",&p);
do
{
z+=recur(j,0,p)*f[j];
j++;
}while(j<n);
printf("%f",z);
return 0;
}

here when i input x and f as
x:1,2,4,8
f:4,5,5,4
i am getting value of z as nan
also
i am getting a warning that recur function is not returning any value
2. I know the answer to "i am getting a warning that recur function is not returning any value". However you took none of my advice for the Newton interpolation problem.
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
34
Rep Power
5
can u tell why i am getting the warning and the way to rectify it
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
34
Rep Power
5
i have modified the code a bit

#include<stdio.h>
float f[50];
float x[50];
int n;
float h;
float recur(int k,int i,float y)
{

if(i==n||i==n-1)
return 1.0;
if(i<n)
{
if((i==k))
i++;
if((i!=k))
h= (((y-x[i])/(x[k]-x[i]))*recur(k,i+1,y));
}
return h;
}
int main()
{
int i;
int j=0;
float z=0.0;
float p;
printf("enter elements");
scanf("%d",&n);
printf("enter values");
for(i=0; i<n; i++)
scanf("%f",&x[i]);
printf("enter f");
for(i=0; i<n; i++)
scanf("%f",&f[i]);
printf("enter the value at which u have to find");
scanf("%f",&p);
do
{

z+=recur(j,0,p)*f[j];
j++;
}while(f[j]!=0);
printf("%f",z);
return 0;
}

is this correct
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
34
Rep Power
5
i have modified the code a bit

#include<stdio.h>
float f[50];
float x[50];
int n;
float h;
float recur(int k,int i,float y)
{

if(i==n||i==n-1)
return 1.0;
if(i<n)
{
if((i==k))
i++;
if((i!=k))
h= (((y-x[i])/(x[k]-x[i]))*recur(k,i+1,y));
}
return h;
}
int main()
{
int i;
int j=0;
float z=0.0;
float p;
printf("enter elements");
scanf("%d",&n);
printf("enter values");
for(i=0; i<n; i++)
scanf("%f",&x[i]);
printf("enter f");
for(i=0; i<n; i++)
scanf("%f",&f[i]);
printf("enter the value at which u have to find");
scanf("%f",&p);
do
{

z+=recur(j,0,p)*f[j];
j++;
}while(f[j]!=0);
printf("%f",z);
return 0;
}

is this correct
6. Originally Posted by tendoeschate
is this correct
Who knows? Until you learn to post in code in code tags so that the code can be read with little effort, I doubt any one will bother.
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
34
Rep Power
5
Code:
```#include&lt;stdio.h&gt;
float f[50];
float x[50];
int n;
float h;
float recur(int k,int i,float y)
{
if(i==n||i==n-1)
return 1.0;
if(i&lt;n)
{
if((i==k))
i++;
if((i!=k))
h= (((y-x[i])/(x[k]-x[i]))*recur(k,i+1,y));
}
return h;
}
int main()
{
int i;
int j=0;
float z=0.0;
float p;
printf("enter elements");
scanf("%d",&n);
printf("enter values");
for(i=0; i&lt;n; i++)
scanf("%f",&x[i]);
printf("enter f");
for(i=0; i&lt;n; i++)
scanf("%f",&f[i]);
printf("enter the value at which u have to find");
scanf("%f",&p);
do
{
z+=recur(j,0,p)*f[j];
j++;
}while(f[j]!=0);
printf("%f",z);
return 0;
}```
is this correct
8. #### I'm sorry, Dave. I'm afraid I can't do that.

uijt ibqqz ibm dpef dfbtbs djqifs tbzt zpvs qsphsbn jt jodpssfdu . eje zpv uftu ju ? ep zpv lopx xibu uif mbhsbohf joufsqpmbou tipvme ep ? epft uijt qsphsbn hjwf uif tbnf botxfs uibu pg zpvs ofxupo joufsqpmbou ? usz zpvs qsphsbn xjui dvcjdt .
Last edited by b49P23TIvg; February 8th, 2014 at 05:11 AM. Reason: djuf tqfdjgjd gbjmvsf
9. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
34
Rep Power
5
what do u mean?
10. #### Hoping this assignment is now overdue

These interpolations you've been working with are expressions the lowest order polynomials that actually go through the given points. These polynomials are unique and have order one less than the number of points. Therefor test data for the Newton interpolation formula is valid test data for the LaGrange interpolating polynomial. They are the same polynomial. Does your program work?

No. Your code evaluates 1 + pow(x,3) as 7 when x is 2.

Code:
```\$ echo  4  -1 0 1 3  0 1 2 28  2 | ( gcc -Wall c.c && ./a.out )
enter elementsenter valuesenter fenter the value at which u have to find7.000000\$```
11. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
34
Rep Power
5
can u suggest changes in my logic so that the program gives correct output
12. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
34
Rep Power
5
got it i have used condition f[j]!=0 for while loop but in ur sample case u have used frequency as 0.
13. The data are just points, why restrict your program against x=0? And think what you like, your program is wrong. The answer to this 4th order polynomial evaluated at x=1.5 is 12.4. Your program computes the answer as 44.4.
Code:
```\$ gcc -Wall -o a.exe c.c
\$ echo 5  1 2 5 8 13  3 39 1671 11385 81615  1.5 | ./a.exe
enter elementsenter valuesenter fenter the value at which u have to find44.367363

\$ python -c 'from math import pow;x=1.5;print(1*pow(x,0) - 1*pow(x,1) + 2*pow(x,2) - 2*pow(x,3) + 3*pow(x,4))'
12.4375

\$ jconsole  # www.jsoftware.com
1 _1 2 _2 3 p. 5  1 2 5 8 13  3 39 1671 11385 81615  1.5
1671 3 39 1671 11385 81615 205 6.82469e6 2.33805e13 5.03997e16 1.33106e20 12.4375

1 2 5 8 13 ,. 3 39 1671 11385 81615   NB. display the knots
1     3
2    39
5  1671
8 11385
13 81615```
Last edited by b49P23TIvg; February 11th, 2014 at 12:41 PM.
14. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2013
Posts
34
Rep Power
5