예제 #1
0
파일: odes.c 프로젝트: zhemao/odesolver
double * rk4sd(derivative f, double y0, double end, int n, 
					double abs_tol, double rel_tol){
	double h = end / n;
	double y, x, d0, d;
	double step1, step2;
	double xvalues[n], yvalues[n];
	double * answers = (double*)malloc(2 * n * sizeof(double*));
	int i;

	yvalues[0] = y0;
	xvalues[0] = 0;

	for(i=1; i < n; i++){
		x = xvalues[i - 1];
		y = yvalues[i - 1];
		
		step1 = rk4step(f, x, y, h);
		step2 = rk4step(f, x, y, h/2);
		step2 = rk4step(f, x + h/2, step2, h/2);

		d0 = abs(step2 - step2);
		d = rel_tol * step1 + abs_tol;

		if(d0 < d){
			xvalues[i] = x + h;
			yvalues[i] = step1;
			continue;
		}

		h = h * pow(d / d0, 0.2);

		xvalues[i] = x + h;
		yvalues[i] = rk4step(f, x, y, h);
	}

	for(i=0; i<n; i++){
		answers[2*i] = xvalues[i];
		answers[2*i + 1] = yvalues[i];
	}
	
	return answers;
}
예제 #2
0
파일: odes.c 프로젝트: zhemao/odesolver
double * rk4(derivative f, double y0, double end, int n){
	double h = end / n;
	double * yvalues = (double *)malloc(n * sizeof(double));
	int i;

	yvalues[0] = y0;

	for(i=1; i < n; i++){
		yvalues[i] = rk4step(f, (i - 1) * h, yvalues[i-1], h);
	}

	return yvalues;
}
예제 #3
0
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
    double* dt;
    double* x;
    double* xout;
    int N, M, n;
    int i;

    if (nlhs != 1 || nrhs != 2)
	mexErrMsgTxt("L3_step_c: wrong number of input or output arguments");

    dt = mxGetPr(prhs[0]);
    x = mxGetPr(prhs[1]);
    N = mxGetN(prhs[1]);
    M = mxGetM(prhs[1]);
    n = N * M;

    plhs[0] = mxCreateDoubleMatrix(M, N, mxREAL);
    xout = mxGetPr(plhs[0]);
    for (i = 0; i < n; ++i)
	xout[i] = x[i];

    rk4step(0.0, dt[0], n, xout, xout);
}