السلام عليكم ورحمة الله وبركاته
اخواني اذا فيه واحد يقدر يساعدني في تصميم برامج (دوال ) لحل بعض طرق التحليل العددي command line applications
حاليا انا سويت طريقتين(برنامجين )
الرجاء الرد
و شكرا
السلام عليكم ورحمة الله وبركاته
اخواني اذا فيه واحد يقدر يساعدني في تصميم برامج (دوال ) لحل بعض طرق التحليل العددي command line applications
حاليا انا سويت طريقتين(برنامجين )
الرجاء الرد
و شكرا
الطريقة الاولى
Newton's Divided Difference Interpolation
double NewtonInterpolation(const double xa[], const double ya[], const double x, const int n)
{
double *f = new double[n];
double *t = new double[n];
for(int i=0; i<n; i++)
{
t = ya;
}
double y = ya[0];
for(int i=0; i<n; i++)
{
f = t[0];
cout << "t: ";
for(int j=0; j<n-i-1; j++)
{
DP deltaX = xa[i+j+1] - xa[j] ;
if (abs(deltaX) < DP_epsilon )
{
y = 0;
cout << "[Error], independent values must be distinct!" << endl;
return 0;
}
t[j] = ( t[j+1] - t[j] ) / deltaX;
cout << t[j] << "\t";
}
cout << endl;
}
for( int i=0; i<n; i++ )
cout << f << "\t";
cout << endl;
DP deltaXs = 1.0;
for( int i=1; i<n; i++)
{
for( int j=0; j<i; j++ )
deltaXs *= x-xa[j];
y += deltaXs*f;
deltaXs = 1.0;
}
delete []f; f = NULL;
delete []t; t = NULL;
return y;
}
void main(){
const double xa[] = {0,2,4,7,1,19,89,8};
const double ya[] = {2,1,1,9,3,4,5,3};
//const DP x = atof(argv[1]);
const double x = 10.0;
double y = Newton(xa, ya, x, 8);
cout << y << endl;
}
الطريقة الثانية
Lagrange method
# include<conio.h>
void main()
{int i,j,n;
float L[9],sum,xp,x[9],y[9],C,D;
sum=0;
printf("Enter n value\n ");
scanf("%d",&n);
printf("\n Enter x and y \n ");
for(i=0;i<n;i++)
scanf("%f %f",&x,&y);
printf("\n Enter point of x ");
scanf("%f",&xp);
clrscr();
printf("Lagrange method\n\n");
printf("\n x ");
for(i=0;i<n;i++) printf("%4.2f\t",x);
printf("\n y ");
for(i=0;i<n;i++) printf("%4.2f\t",y);
printf(" xp= %4.2f\n",xp);
printf\ـــــــــــــــــــــــــــــــــ")
n");
for(i=0;i<n;i++)
{ C=1; D=1;
for(j=0;j<n;j++)
{ if(i!=j) {
C=C*(xp-x[j]) ;
D=D*(x-x[j]);
} }
L=C/D;
sum=sum+L*y;
printf("\n L(%d)= %f ",i,L);
}
printf("\n\ــــــــــــــــــــــــــــــ
n");
printf("\nL=L1*y1+l2*y2+....\n");
printf\ــــــــــــــــــــــــــــــ")
n\n");
printf("L= %f ",sum);
}
إعادة تنسيقها
لطريقة الاولى
Newton's Divided Difference Interpolation
double NewtonInterpolation(const double xa[], const double ya[], const double x, const int n)
{
double *f = new double[n];
double *t = new double[n];
for(int i=0; i<n; i++)
{
t = ya;
}
double y = ya[0];
for(int i=0; i<n; i++)
{
f = t[0];
cout << "t: ";
for(int j=0; j<n-i-1; j++)
{
DP deltaX = xa[i+j+1] - xa[j];
if (abs(deltaX) < DP_epsilon )
{
y = 0;
cout << "[Error], independent values must be distinct!" << endl;
return 0;
}
t[j] = ( t[j+1] - t[j] ) / deltaX;
cout << t[j] << "\t";
}
cout << endl;
}
for( int i=0; i<n; i++ )
cout << f << "\t";
cout << endl;
DP deltaXs = 1.0;
for( int i=1; i<n; i++)
{
for( int j=0; j<i; j++ )
deltaXs *= x-xa[j];
y += deltaXs*f;
deltaXs = 1.0;
}
delete []f; f = NULL;
delete []t; t = NULL;
return y;
}
void main(){
const double xa[] = {0,2,4,7,1,19,89,8};
const double ya[] = {2,1,1,9,3,4,5,3};
//const DP x = atof(argv[1]);
const double x = 10.0;
double y = Newton(xa, ya, x, 8);
cout << y << endl;
}
الطريقة الثانية
Lagrange method
# include<conio.h>
void main()
{int i,j,n;
float L[9],sum,xp,x[9],y[9],C,D;
sum=0;
printf("Enter n value\n ");
scanf("%d",&n);
printf("\n Enter x and y \n ");
for(i=0;i<n;i++)
scanf("%f %f",&x,&y);
printf("\n Enter point of x ");
scanf("%f",&xp);
clrscr();
printf("Lagrange method\n\n");
printf("\n x ");
for(i=0;i<n;i++) printf("%4.2f\t",x);
printf("\n y ");
for(i=0;i<n;i++) printf("%4.2f\t",y);
printf(" xp= %4.2f\n",xp);
printf\ـــــــــــــــــــــــــــــــــ")
n");
for(i=0;i<n;i++)
{ C=1; D=1;
for(j=0;j<n;j++)
{ if(i!=j) {
C=C*(xp-x[j]);
D=D*(x-x[j]);
} }
L=C/D;
sum=sum+L*y;
printf("\n L(%d)= %f ",i,L);
}
printf("\n\ــــــــــــــــــــــــــــــ
n");
printf("\nL=L1*y1+l2*y2+....\n");
printf\ــــــــــــــــــــــــــــــ")
n\n");
printf("L= %f ",sum);
}تم تعديل هذه المشاركة بواسطة b.m.s في 7 مايو 2007 في 05:25
هذا الموضوع مغلق.