c++ code help !

Thread Starter

salam

Joined Oct 29, 2012
3
Hello guys !
I have this homework .. my instructor wants us to solve the following question using c++
Determine the value of F(x) using lagrange polynomials method for the following data
Xi=-1.5 , -0.75 , 0 , 0.75 ,1.5
Yi=-14.1014 , -0.931596 , 0 , 0.931596, 14.1014

Here is my code .. but it only finds the summation , I guess I have to define some character value to use it in printing the formula F(x),, but I couldn't apply this idea .. any help ??


Rich (BB code):
#include <iostream.h>
#include <stdlib.h>
#include <math.h>

void main ()
{
	double  X[5],Y[5];
	double n=5,sum=0,x;

	cout <<"Insert the value x: \n";
	cin >>x;


	cout <<"Enter vector X : \n" ;
	for (int a=0;a<n;a++)
	{
		cin>>X[a];
		cout <<X[a]<<endl;
	}
	cout<<endl;


	
	cout <<"Enter vector Y : \n" ;
	for (int b=0;b<n;b++)
	{
		cin>>Y;
		cout <<Y<<endl;
	}
	cout<<endl;



	for (int i=0;i<n;i++)
	{
		double p=1;
		for (int j=0;j<n;j++)
		{
			if (j!=i)
			{
				p=p*((x-X[j])/(X-X[j]));
			}
		}
		sum=sum+p*Y;
		
	}
cout <<"SUM = \n " << sum <<endl;
	
}



where x=1.4 , and sum will equal to 11.1984
 
Top