Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "iostream"
- #include "cmath"
- #include <iomanip>
- using namespace std;
- #define PI 3.141592653589793238463
- double Lagrange (double t, int n, double *x, double *y)
- {
- double L=0;
- double *l = new double [n+1];
- for( int i=0;i<=n;i++)
- {
- l[i]=1;
- for(int j=0;j<=n;j++)
- {
- if(i==j)
- continue;
- else
- l[i]*=(t-x[j])/(x[i]-x[j]);
- }
- }
- for (int k=0;k<=n;k++)
- L+=(l[k]*y[k]);
- return L;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- int n;
- double a,b;
- cout<<"ENTER a: ";
- cin>>a;
- cout<<"ENTER b: ";
- cin>>b;
- cout<<"ENTER n: ";
- cin>>n;
- double *t = new double [n+1];
- double *x = new double [n+1];
- double *y = new double [n+1];
- for(int i=0;i<=n;i++)
- t[i]=cos(PI*(2*i+1)/(2*n+2));
- for(int i=0;i<=n;i++)
- {
- x[i]=(a+b)/2+t[i]*(b-a)/2;
- y[i]=fabs(x[i]);
- cout<<x[i]<<"\t"<<y[i]<<endl;
- }
- cout<<endl;
- for(int i=0;i<n;i++)
- {
- cout<<Lagrange(x[i], n, x, y)<<endl;
- cout<<"\t"<<Lagrange((x[i+1]+x[i])/2, n, x, y)<<endl;
- }
- system ("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment