۱۵-۱۱-۱۳۸۶, ۰۹:۴۴ عصر
سلام
این برنامه یک تابع درونیاب به روش نیوتن برای یک تابع جدولی درست میکنه ( این پروژه ی درس محاسباتم بود ، گفتم بزارم بد نیست )
این برنامه یک تابع درونیاب به روش نیوتن برای یک تابع جدولی درست میکنه ( این پروژه ی درس محاسباتم بود ، گفتم بزارم بد نیست )
کد:
//Written by soheil setayeshi
//Newton method interpolation
//forum.codecorona.com
#include<iostream>
#include<vector>
using namespace std;
//******************************************
struct node
{
float fi[2];
float xi[2];
float value;
};
//******************************************
struct mt
{
float x;
float y;
};
//******************************************
void create_table(mt*,int);
//******************************************
int main()
{
mt *table;
int n,i;
cout<<"Enter number of your points:";
cin>>n;
table=new mt [n];
cout<<"Enter your points:\n\n";
for (i=0 ; i<n ; i++)
{
cout<<"i="<<i<<":"<<endl;
cout<<"X["<<i<<"]: ";
cin>>table[i].x;
cout<<"Fx["<<i<<"]: ";
cin>>table[i].y;
cout<<"\n";
}
system("cls");
cout<<"Your table is :\n\n";
cout<<"X:\n";
for (i=0 ; i<n ; i++)
cout<<table[i].x<<' ';
cout<<"\n\n";
cout<<"F(x):\n";
for (i=0 ; i<n ; i++)
cout<<table[i].y<<' ';
cout<<endl;
create_table(table,n);
return 0;
}
//******************************************
void create_table(mt *a,int n)
{
int i,j,k;
vector<node> *v;
node temp;
v=new vector<node> [n-1];
for (i=0 ; i<n-1 ; i++)
{
temp.fi[0]=a[i].y;
temp.fi[1]=a[i+1].y;
temp.xi[0]=a[i].x;
temp.xi[1]=a[i+1].x;
temp.value = (temp.fi[1]-temp.fi[0]) / (temp.xi[1]-temp.xi[0]);
v[0].push_back(temp);
}
for (i=1 ; i<n-1 ; i++)
for (j=0 ; j<n-i-1 ; j++)
{
temp.fi[0]=v[i-1][j].value;
temp.fi[1]=v[i-1][j+1].value;
temp.xi[0]=v[i-1][j].xi[0];
temp.xi[1]=v[i-1][j+1].xi[1];
temp.value = (temp.fi[1]-temp.fi[0]) / (temp.xi[1]-temp.xi[0]);
v[i].push_back(temp);
}
for (j=0 ; j<n-1 ; j++)//display
{
cout<<"******************************************************************************\n\n";
cout<<"\tF[ ";
for (i=0 ; i<j+2 ; i++)
{
if (i!=0)
cout<<"Xi+"<<i<<' ';
else
cout<<"Xi ";
}
cout<<"] \n\n";
cout<<"******************************************************************************\n";
for (i=0 ; i<v[j].size() ; i++)
{
cout<<v[j][i].fi[1]<<" - "<<v[j][i].fi[0]<<endl;
cout<<"-------- = "<<v[j][i].value<<endl;
cout<<v[j][i].xi[1]<<" - "<<v[j][i].xi[0]<<"\n\n";
}
cout<<endl;
}
cout<<endl;
cout<<"FINAL EQUATION :\n\n";
if (n!=1)
cout<<a[0].y<<" + ";
else
cout<<a[0].y;
for (i=0 ; i<n-1 ; i++)
{
cout<<v[i][0].value<<" x ";
for (j=0 ; j<i+1 ; j++)
cout<<"(X-"<<a[j].x<<")";
if (i!=n-2)
cout<<" + ";
}
cout<<"\n\n";
system("pause");
}