Beispiel #1
0
int main()
{
int i,n;
printf("Enter the number of points \n");
scanf("%d",&n);
printf("Start entering the points in the format x_i : y _i \n");
float **p =allocate(n,2);
for(i=0;i<n;i++)
scanf("%f %f",&p[i][0],&p[i][1]);
float **mat = allocate(n-2,4);
float *h = (float *)malloc(n-1*sizeof(float));
for(i=0;i<n-1;i++)
h[i]=p[i+1][0]-p[i][0];
float *yk = (float *)malloc(n-1*sizeof(float));
for(i=0;i<n-1;i++)
yk[i]=p[i+1][1]-p[i][1];
mat[0][1]=2*(h[0]+h[1]);
mat[0][2]=h[1];
mat[0][3]=6*( yk[1]/h[1] - yk[0]/h[0]);
mat[n-3][0]=h[n-3];
mat[n-3][1]=2*(h[n-2]+h[n-3]);
mat[n-3][3]=6*( yk[n-2]/h[n-2] - yk[n-3]/h[n-3]);
for(i=1;i<n-3;i++)
{
mat[i][0]=h[i];
mat[i][1]=2*(h[i]+h[i+1]);
mat[i][2]=h[i+1];
mat[i][3]=6*( yk[i+1]/h[i+1] - yk[i]/h[i]);
}
float *X = thomas(mat,n-2);
//computing intermediate values
for(i=0;i<n-1;i++)
{
int j;
for(j=0;j<10;j++)
{
float x = p[i][0]+(j+1)*(p[i+1][0]-p[i][0])/10;
float a= p[i+1][0]-x;
float b = x-p[i][0];
float val = X[i]/6*(((a*a*a)/h[i]) - h[i]*a)
+yk[i]*a/h[i]
+X[i+1]/6*((b/h[i])-h[i]*b)
+yk[i+1]/h[i+1]*b;
printf("%f %f\n",x,val);
}
printf("\n");
}
return 0;
}
Beispiel #2
0
Array<real,2> MathInterpolation::I(){
	Array<real,1> A, A_I, B;

	A.resize(Field.ubound(firstDim));
	A_I.resize(Field.ubound(firstDim));
	B.resize(Field.ubound(firstDim)+1);

	Resizes(Field.ubound(firstDim));
	for(int j = 0; j <= Field.ubound(secondDim); j++){
		B(Range::all()) = Field(Range::all(),j);
		computeInt(A,B);
		thomas(bandA,mainBand,bandC,A,A_I);
		Field_I(Range::all(),j) = A_I(Range::all());
	}
	return Field_I;
}
Beispiel #3
0
int main() {
	Cat thomas("Tommy"); //создаем объект thomas класса Cat, вызывается конструктор и в конструктор передается параметр имени
	Dog bobby("Bob");

	//thomas.voice();
	//bobby.voice();

	Animal *array[2];
	array[0] = &thomas;
	array[1] = &bobby;

	for (int i = 0; i < 2; ++i)
	{
		array[i]->voice();
	}

	getc(stdin);
	return 0;
}
Beispiel #4
0
// input:  y is array to interpolate, n is array length
// output: D will be array of spline data
void spline(double *D, const double *y, int n) {
	int i;
	double *a, *b, *c;

	a = malloc(3*n*sizeof(double));
	b = a + n;
	c = b + n;

	if (n < 4) {
		a[0] = 0;
		b[0] = 2;
		c[0] = 1;
		D[0] = 3*(y[1]-y[0]);

		a[n-1] = 1;
		b[n-1] = 2;
		c[n-1] = 0;
		D[n-1] = 3*(y[n-1]-y[n-2]);
	}
	else {
		a[0] = 0;
		b[0] = 2;
		c[0] = 4;
		D[0] = -5*y[0] + 4*y[1] + y[2];

		a[n-1] = 4;
		b[n-1] = 2;
		c[n-1] = 0;
		D[n-1] = 5*y[n-1] - 4*y[n-2] - y[n-3];
	}

	for (i = 1; i < n-1; ++i) {
		a[i] = 1;
		b[i] = 4;
		c[i] = 1;
		D[i] = 3*(y[i+1]-y[i-1]);
	}

	thomas(D, a, b, c, n);

	free(a);
}
Beispiel #5
0
//Пусть эта штука будет рекурсивно себя вызывать и возвращать число итераций
int layer(double eps,int n,double tau,double h,double *v,double *hat_v){
  double err,gamma,*a,*c,*d,*b,*w,*pts[5]; int i;
  new_double(5,n,pts);
  a = pts[0];  c = pts[1];  d = pts[2];  b = pts[3]; w = pts[4];
  _G(n,b,tau,h,v,hat_v);
  err = norm_max(n,b);
  if (err<eps) printf("Ошибка %e\neps %e\n",norm_max(n,b),eps);
  if (norm_max(n,b) < eps) {
    clean_all(5,pts); return 0;
  }
  for(i=n-6,a[n-5] = 1/tau;i>=0;i--) {
    a[i] = 1/tau;
    c[i] = hat_v[i+3]/(2*h); d[i] = -hat_v[i+2]/(2*h);
  }
  thomas(n-4,w+2,a,c,d,b); for (gamma=1.;gamma>AESH_MIN;gamma/=2.){
     if (gamma<10*AESH_MIN) exit(-1);
    for (i=2;i<n-2;i++) hat_v[i] += gamma * w[i];
    _G(n,b,tau,h,v,hat_v); printf("Ошибка %e\n",norm_max(n,b)); if (norm_max(n,b)<err) break;
    for (i=2;i<n-2;i++) hat_v[i] -= gamma * w[i];
  }
  clean_all(5,pts);
  return 1 + layer(eps,n,tau,h,v,hat_v);
}