c++ code help !

Discussion in 'Homework Help' started by salam, Nov 22, 2012.

  1. salam

    Thread Starter New Member

    Oct 29, 2012
    3
    0
    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 ??


    Code ( (Unknown Language)):
    1. #include <iostream.h>
    2. #include <stdlib.h>
    3. #include <math.h>
    4.  
    5. void main ()
    6. {
    7.     double  X[5],Y[5];
    8.     double n=5,sum=0,x;
    9.  
    10.     cout <<"Insert the value x: \n";
    11.     cin >>x;
    12.  
    13.  
    14.     cout <<"Enter vector X : \n" ;
    15.     for (int a=0;a<n;a++)
    16.     {
    17.         cin>>X[a];
    18.         cout <<X[a]<<endl;
    19.     }
    20.     cout<<endl;
    21.  
    22.  
    23.    
    24.     cout <<"Enter vector Y : \n" ;
    25.     for (int b=0;b<n;b++)
    26.     {
    27.         cin>>Y[b];
    28.         cout <<Y[b]<<endl;
    29.     }
    30.     cout<<endl;
    31.  
    32.  
    33.  
    34.     for (int i=0;i<n;i++)
    35.     {
    36.         double p=1;
    37.         for (int j=0;j<n;j++)
    38.         {
    39.             if (j!=i)
    40.             {
    41.                 p=p*((x-X[j])/(X[i]-X[j]));
    42.             }
    43.         }
    44.         sum=sum+p*Y[i];
    45.        
    46.     }
    47. cout <<"SUM = \n " << sum <<endl;
    48.    
    49. }
    50.  
    51. [/i][/i][/b][/b]


    where x=1.4 , and sum will equal to 11.1984
     
Loading...