int main(void) { int i,j,k,kk; float xx,*x,*fvec,**fjac; fjac=matrix(1,N,1,N); fvec=vector(1,N); x=vector(1,N); for (kk=1;kk<=2;kk++) { for (k=1;k<=3;k++) { xx=0.2001*k*(2*kk-3); printf("Starting vector number %2d\n",k); for (i=1;i<=4;i++) { x[i]=xx+0.2*i; printf("%7s%1d%s %5.2f\n", "x[",i,"] = ",x[i]); } printf("\n"); for (j=1;j<=NTRIAL;j++) { mnewt(1,x,N,TOLX,TOLF); usrfun(x,N,fvec,fjac); printf("%5s %13s %13s\n","i","x[i]","f"); for (i=1;i<=N;i++) printf("%5d %14.6f %15.6f\n", i,x[i],fvec[i]); printf("\npress RETURN to continue...\n"); (void) getchar(); } } } free_vector(x,1,N); free_vector(fvec,1,N); free_matrix(fjac,1,N,1,N); return 0; }
/* =========================================================================== get_spline x , y - spline points xx, yy - output (allocated) spline interpolation =========================================================================== */ void get_spline(int i_x[],int i_y[],int nwhisker_points, int min_x, int max_x, int **yy) { int i, status, x_val; float *x, *y, *y2; float y_val; int start_ind, end_ind; x = allocate_vector( 1, nwhisker_points ); y = allocate_vector( 1, nwhisker_points ); y2 = allocate_vector( 1, nwhisker_points ); for (i = 0; i<nwhisker_points; i++) { x[i+1] = i_x[i] + 0.; y[i+1] = i_y[i] + 0.; } *yy = allocate_ivector( min_x, max_x ); spline( x,y, y2, nwhisker_points ); /* calculate the 2nd derivatives of y at spline points */ for (x_val = min_x;x_val <= max_x; x_val++) { status = splint( x, y, y2, nwhisker_points, (float) x_val, &y_val ); if (status != 1) mexErrMsgTxt("bad spline"); (*yy)[x_val] = round ( y_val ); } free_vector( x, 1,nwhisker_points); free_vector( y, 1,nwhisker_points); free_vector( y2, 1,nwhisker_points); }
int main(void) { int i,j; float h,x=1.0,*y,*dydx,*yout; y=vector(1,N); dydx=vector(1,N); yout=vector(1,N); y[1]=bessj0(x); y[2]=bessj1(x); y[3]=bessj(2,x); y[4]=bessj(3,x); derivs(x,y,dydx); printf("\n%16s %5s %12s %12s %12s\n", "Bessel function:","j0","j1","j3","j4"); for (i=1;i<=5;i++) { h=0.2*i; rk4(y,dydx,N,x,h,yout,derivs); printf("\nfor a step size of: %6.2f\n",h); printf("%12s","rk4:"); for (j=1;j<=4;j++) printf(" %12.6f",yout[j]); printf("\n%12s %12.6f %12.6f %12.6f %12.6f\n","actual:", bessj0(x+h),bessj1(x+h),bessj(2,x+h),bessj(3,x+h)); } free_vector(yout,1,N); free_vector(dydx,1,N); free_vector(y,1,N); return 0; }
int main(void) { /* Test with Gauss-Hermite */ int i,n; float amu0,check,*a,*b,*x,*w; a=vector(1,NP); b=vector(1,NP); x=vector(1,NP); w=vector(1,NP); for (;;) { printf("Enter N:\n"); if (scanf("%d",&n) == EOF) break; for (i=1;i<n;i++) { a[i]=0.0; b[i+1]=i*0.5; } a[n]=0.0; /* b[1] is arbitrary for call to tqli */ amu0=SQRTPI; gaucof(n,a,b,amu0,x,w); printf("%3s %10s %14s\n","#","x(i)","w(i)"); for (i=1;i<=n;i++) printf("%3d %14.6e %14.6e\n",i,x[i],w[i]); for (check=0.0,i=1;i<=n;i++) check += w[i]; printf("\nCheck value: %15.7e should be: %15.7e\n",check,SQRTPI); } free_vector(w,1,NP); free_vector(x,1,NP); free_vector(b,1,NP); free_vector(a,1,NP); return 0; }
void arma_linmin(double sdata[], int nobs, int ar_p, int ma_q, double *p,double *xi,int n,double *fret) //double *p,*xi,*fret; //long n; { long j; double xx,xmin,fx,fb,fa,bx,ax,tol=1.0e-4; double arma_brent(),f1dim(),*vector(); void arma_mnbrak(),free_vector(); int z; double alpha[ar_p]; double beta[ma_q]; ncom=n; pcom = vector(n); xicom = vector(n); //nrfunc = func; for (j=0;j<n;j++){ pcom[j] = p[j]; xicom[j] = xi[j]; } ax = 0.; xx = 1.0; bx = 2.0; arma_mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim,sdata,nobs,ar_p,ma_q); *fret=arma_brent(ax,xx,bx,f1dim,TOL,&xmin,sdata,nobs,ar_p,ma_q); for(j=0;j<n;j++){ xi[j] *= xmin; p[j] += xi[j]; } free_vector(xicom); free_vector(pcom); }
void rk4(float y[], float dydx[], int n, float x, float h, float yout[], void(*derivs)(float, float[], float[])) { int i; float xh, hh, h6, *dym, *dyt, *yt; dym = vector(1, n); dyt = vector(1, n); yt = vector(1, n); hh = h*0.5; h6 = h / 6.0; xh = x + hh; for (i = 1; i <= n; i++) yt[i] = y[i] + hh*dydx[i]; (*derivs)(xh, yt, dym); for (i = 1; i <= n; i++){ yt[i] = y[i] + h*dym[i]; dym[i] += dyt[i]; } (*derivs)(x + h, yt, dyt); for (i = 1; i <= n; i++) yout[i] = y[i] + h6*(dydx[i] + dyt[i] + 2.0*dym[i]); free_vector(yt, 1, n); free_vector(dyt, 1, n); free_vector(dym, 1, n); }
int main(void) { int i,j,nflag,nroot=0; static float p[N+1]={10.0,-18.0,25.0,-24.0,16.0,-6.0,1.0}; float *b,*c; b=vector(1,NTRY); c=vector(1,NTRY); printf("\nP(x)=x^6-6x^5+16x^4-24x^3+25x^2-18x+10\n"); printf("Quadratic factors x^2+bx+c\n\n"); printf("%6s %10s %12s \n\n","factor","b","c"); for (i=1;i<=NTRY;i++) { c[i]=0.5*i; b[i] = -0.5*i; qroot(p,N,&b[i],&c[i],EPS); if (nroot == 0) { printf("%4d %15.6f %12.6f\n",nroot,b[i],c[i]); nroot=1; } else { nflag=0; for (j=1;j<=nroot;j++) if ((fabs(b[i]-b[j]) < TINY) && (fabs(c[i]-c[j]) < TINY)) nflag=1; if (nflag == 0) { printf("%4d %15.6f %12.6f\n",nroot,b[i],c[i]); ++nroot; } } } free_vector(c,1,NTRY); free_vector(b,1,NTRY); return 0; }
int main(void) { int i; static float u[N+1]={-1.0,5.0,-10.0,10.0,-5.0,1.0}; static float v[NV+1]={1.0,3.0,3.0,1.0}; float *q,*r; q=vector(0,N); r=vector(0,N); poldiv(u,N,v,NV,q,r); printf("\n%10s %10s %10s %10s %10s %10s\n\n", "x^0","x^1","x^2","x^3","x^4","x^5"); printf("quotient polynomial coefficients:\n"); for (i=0;i<=5;i++) printf("%10.2f ",q[i]); printf("\nexpected quotient coefficients:\n"); printf("%10.2f %10.2f %10.2f %10.2f %10.2f %10.2f\n\n", 31.0,-8.0,1.0,0.0,0.0,0.0); printf("remainder polynomial coefficients:\n"); for (i=0;i<=3;i++) printf("%10.2f ",r[i]); printf("\nexpected remainder coefficients:\n"); printf("%10.2f %10.2f %10.2f %10.2f\n",-32.0,-80.0,-80.0,0.0); free_vector(r,0,N); free_vector(q,0,N); return 0; }
int main(void) { int i; float a1=0.5976,a2=1.4023,a3=0.0,*y,*yout,*dfdx,**dfdy,*dydx; y=vector(1,NVAR); yout=vector(1,NVAR); dfdx=vector(1,NVAR); dfdy=matrix(1,NVAR,1,NVAR); dydx=vector(1,NVAR); y[1]=y[2]=1.0; y[3]=0.0; derivs(X1,y,dydx); jacobn(X1,y,dfdx,dfdy,NVAR); printf("Test Problem:\n"); for (i=5;i<=50;i+=5) { simpr(y,dydx,dfdx,dfdy,NVAR,X1,HTOT,i,yout,derivs); printf("\n%s %5.2f %s %5.2f %s %2d %s \n", "x=",X1," to ",X1+HTOT," in ",i," steps"); printf("%14s %9s\n","integration","bessj"); printf("%12.6f %12.6f\n",yout[1],a1); printf("%12.6f %12.6f\n",yout[2],a2); printf("%12.6f %12.6f\n",yout[3],a3); } free_vector(dydx,1,NVAR); free_matrix(dfdy,1,NVAR,1,NVAR); free_vector(dfdx,1,NVAR); free_vector(yout,1,NVAR); free_vector(y,1,NVAR); return 0; }
int main(void) { unsigned long i,j; float cmp,*data1,*data2,*ans; data1=vector(1,N); data2=vector(1,N); ans=vector(1,N2); for (i=1;i<=N;i++) { if ((i > N/2-N/8) && (i < N/2+N/8)) data1[i]=1.0; else data1[i]=0.0; data2[i]=data1[i]; } correl(data1,data2,N,ans); /* Calculate directly */ printf("%3s %14s %18s\n","n","CORREL","direct calc."); for (i=0;i<=16;i++) { cmp=0.0; for (j=1;j<=N;j++) cmp += data1[((i+j-1) % N)+1]*data2[j]; printf("%3ld %15.6f %15.6f\n",i,ans[i+1],cmp); } free_vector(ans,1,N2); free_vector(data2,1,N); free_vector(data1,1,N); return 0; }
int main(void) { float d,**a,**al,*b,*x; unsigned long i,j,*indx; long idum=(-1); a=matrix(1,7,1,4); x=vector(1,7); b=vector(1,7); al=matrix(1,7,1,2); indx=lvector(1,7); for (i=1;i<=7;i++) { x[i]=ran1(&idum); for (j=1;j<=4;j++) { a[i][j]=ran1(&idum); } } banmul(a,7,2,1,x,b); for (i=1;i<=7;i++) printf("%ld %12.6f %12.6f\n",i,b[i],x[i]); bandec(a,7,2,1,al,indx,&d); banbks(a,7,2,1,al,indx,b); for (i=1;i<=7;i++) printf("%ld %12.6f %12.6f\n",i,b[i],x[i]); free_lvector(indx,1,7); free_matrix(al,1,7,1,2); free_vector(b,1,7); free_vector(x,1,7); free_matrix(a,1,7,1,4); return 0; }
void rkqs(float y[], float dydx[], int n, float *x, float htry, float eps, float yscal[], float *hdid, float *hnext, void (*derivs)(float, float [], float [])) { void rkck(float y[], float dydx[], int n, float x, float h, float yout[], float yerr[], void (*derivs)(float, float [], float [])); int i; float errmax,h,xnew,*yerr,*ytemp; yerr=vector(1,n); ytemp=vector(1,n); h=htry; for (;;) { rkck(y,dydx,n,*x,h,ytemp,yerr,derivs); errmax=0.0; for (i=1;i<=n;i++) errmax=FMAX(errmax,fabs(yerr[i]/yscal[i])); errmax /= eps; if (errmax > 1.0) { h=SAFETY*h*pow(errmax,PSHRNK); if (h < 0.1*h) h *= 0.1; xnew=(*x)+h; if (xnew == *x) nrerror("stepsize underflow in rkqs"); continue; } else { if (errmax > ERRCON) *hnext=SAFETY*h*pow(errmax,PGROW); else *hnext=5.0*h; *x += (*hdid=h); for (i=1;i<=n;i++) y[i]=ytemp[i]; break; } } free_vector(ytemp,1,n); free_vector(yerr,1,n); }
int main(void) { int i,nbad,nok; float eps=1.0e-4,h1=0.1,hmin=0.0,x1=1.0,x2=10.0,*ystart; ystart=vector(1,N); xp=vector(1,200); yp=matrix(1,10,1,200); ystart[1]=bessj0(x1); ystart[2]=bessj1(x1); ystart[3]=bessj(2,x1); ystart[4]=bessj(3,x1); nrhs=0; kmax=100; dxsav=(x2-x1)/20.0; odeint(ystart,N,x1,x2,eps,h1,hmin,&nok,&nbad,derivs,rkqs); printf("\n%s %13s %3d\n","successful steps:"," ",nok); printf("%s %20s %3d\n","bad steps:"," ",nbad); printf("%s %9s %3d\n","function evaluations:"," ",nrhs); printf("\n%s %3d\n","stored intermediate values: ",kount); printf("\n%8s %18s %15s\n","x","integral","bessj(3,x)"); for (i=1;i<=kount;i++) printf("%10.4f %16.6f %14.6f\n", xp[i],yp[4][i],bessj(3,xp[i])); free_matrix(yp,1,10,1,200); free_vector(xp,1,200); free_vector(ystart,1,N); return 0; }
int main(void) { int i,j; float x1x2,*x1,*x2,**y,**y2; x1=vector(1,N); x2=vector(1,N); y=matrix(1,M,1,N); y2=matrix(1,M,1,N); for (i=1;i<=M;i++) x1[i]=0.2*i; for (i=1;i<=N;i++) x2[i]=0.2*i; for (i=1;i<=M;i++) for (j=1;j<=N;j++) { x1x2=x1[i]*x2[j]; y[i][j]=x1x2*x1x2; } splie2(x1,x2,y,M,N,y2); printf("\nsecond derivatives from SPLIE2\n"); printf("natural spline assumed\n"); for (i=1;i<=5;i++) { for (j=1;j<=5;j++) printf("%12.6f",y2[i][j]); printf("\n"); } printf("\nactual second derivatives\n"); for (i=1;i<=5;i++) { for (j=1;j<=5;j++) printf("%12.6f",2.0*x1[i]*x1[i]); printf("\n"); } free_matrix(y2,1,M,1,N); free_matrix(y,1,M,1,N); free_vector(x2,1,N); free_vector(x1,1,N); return 0; }
int main(void) { long idum=(-1984); int i,mwt=1; float a,abdev,b,chi2,q,siga,sigb; float *x,*y,*sig; x=vector(1,NDATA); y=vector(1,NDATA); sig=vector(1,NDATA); for (i=1;i<=NPT;i++) { x[i]=0.1*i; y[i] = -2.0*x[i]+1.0+SPREAD*gasdev(&idum); sig[i]=SPREAD; } fit(x,y,NPT,sig,mwt,&a,&b,&siga,&sigb,&chi2,&q); printf("\nAccording to routine FIT the result is:\n"); printf(" a = %8.4f uncertainty: %8.4f\n",a,siga); printf(" b = %8.4f uncertainty: %8.4f\n",b,sigb); printf(" chi-squared: %8.4f for %4d points\n",chi2,NPT); printf(" goodness-of-fit: %8.4f\n",q); printf("\nAccording to routine MEDFIT the result is:\n"); medfit(x,y,NPT,&a,&b,&abdev); printf(" a = %8.4f\n",a); printf(" b = %8.4f\n",b); printf(" absolute deviation (per data point): %8.4f\n",abdev); printf(" (note: gaussian SPREAD is %8.4f)\n",SPREAD); free_vector(sig,1,NDATA); free_vector(y,1,NDATA); free_vector(x,1,NDATA); return 0; }
int main(void) { long idum=(-11); int i; float b,rf,*x,*y; x=vector(1,NDATA); y=vector(1,NDATA); ndatat=NDATA; xt=x; yt=y; for (i=1;i<=NDATA;i++) { x[i]=0.1*i; y[i] = -2.0*x[i]+1.0+SPREAD*gasdev(&idum); } printf("%9s %9s %12s %10s\n","b","a","ROFUNC","ABDEVT"); for (i = -5;i<=5;i++) { b = -2.0+0.02*i; rf=rofunc(b); printf("%10.2f %9.2f %11.2f %10.2f\n", b,aa,rf,abdevt); } free_vector(y,1,NDATA); free_vector(x,1,NDATA); return 0; }
void polcof(float xa[], float ya[], int n, float cof[]) { void polint(float xa[], float ya[], int n, float x, float *y, float *dy); int k,j,i; float xmin,dy,*x,*y; x=vector(0,n); y=vector(0,n); for (j=0;j<=n;j++) { x[j]=xa[j]; y[j]=ya[j]; } for (j=0;j<=n;j++) { polint(x-1,y-1,n+1-j,0.0,&cof[j],&dy); xmin=1.0e38; k = -1; for (i=0;i<=n-j;i++) { if (fabs(x[i]) < xmin) { xmin=fabs(x[i]); k=i; } if (x[i]) y[i]=(y[i]-cof[j])/x[i]; } for (i=k+1;i<=n-j;i++) { y[i-1]=y[i]; x[i-1]=x[i]; } } free_vector(y,0,n); free_vector(x,0,n); }
int main(void) { long idum=(-51773); int i,j; float fctr1,fctr2,prob,t,*data1,*data2; data1=vector(1,NPTS); data2=vector(1,MPTS); /* Generate two gaussian distributions of different variance */ fctr1=sqrt(VAR1); for (i=1;i<=NPTS;i++) data1[i]=fctr1*gasdev(&idum); fctr2=sqrt(VAR2); for (i=1;i<=MPTS;i++) data2[i]=NSHFT/2.0*EPS+fctr2*gasdev(&idum); printf("\nDistribution #1 : variance = %6.2f\n",VAR1); printf("Distribution #2 : variance = %6.2f\n\n",VAR2); printf("%7s %8s %16s\n","shift","t","probability"); for (i=1;i<=NSHFT+1;i++) { tutest(data1,NPTS,data2,MPTS,&t,&prob); printf("%6.2f %10.2f %11.2f\n",(i-1)*EPS,t,prob); for (j=1;j<=NPTS;j++) data1[j] += EPS; } free_vector(data2,1,MPTS); free_vector(data1,1,NPTS); return 0; }
/* Free()s the given graph_node but NOT its command. */ static void free_graph_node(struct graph_node *node) { free_vector(node->input_files); free_vector(node->output_files); free_vector(node->node_deps); free(node); }
void cyclic(float a[], float b[], float c[], float alpha, float beta, float r[], float x[], unsigned long n) { void tridag(float a[], float b[], float c[], float r[], float u[], unsigned long i; unsigned long i; float fact, gamma, *bb, *u, *z; if (n <= 2) nrerror("n too small in cyclic"); bb = vector(1, n); u = vector(1, n); z = vector(1, n); gamma = -b[1]; bb[1] = b[1] - gamma; bb[n] = b[n] - alpha*beta / gamma; for (i = 2; i < n; i++) bb[i] = b[i]; tridag(a, bb, c, r, x, n); u[1] = gamma; u[n] = alpha; for (i = 2; i <= n; i++) u[i] = 0.0; tridag(a, bb, c, u, z, n); fact = (x[1] + beta*x[n] / gamma); for (i = 1; i <= n; i++) x[i] -= fact*z[i]; free_vector(z, 1, n); free_vector(u, 1, n); free_vector(bb, 1, n); }
int main(void) { unsigned long i; int isign; float *data1,*data2,*fft1,*fft2; data1=vector(1,N); data2=vector(1,N); fft1=vector(1,N2); fft2=vector(1,N2); for (i=1;i<=N;i++) { data1[i]=floor(0.5+cos(i*2.0*PI/PER)); data2[i]=floor(0.5+sin(i*2.0*PI/PER)); } twofft(data1,data2,fft1,fft2,N); printf("Fourier transform of first function:\n"); prntft(fft1,N); printf("Fourier transform of second function:\n"); prntft(fft2,N); /* Invert transform */ isign = -1; four1(fft1,N,isign); printf("inverted transform = first function:\n"); prntft(fft1,N); four1(fft2,N,isign); printf("inverted transform = second function:\n"); prntft(fft2,N); free_vector(fft2,1,N2); free_vector(fft1,1,N2); free_vector(data2,1,N); free_vector(data1,1,N); return 0; }
void j_linmin( double p[],double xi[],int n,double *fret,double (*func)(double x[], void *localdata),void *localdata) { int j; double xx,xmin,fx,fb,fa,bx,ax; J_LINMIN_STRUCT jls; jls.ncom=n; jls.pcom=vector(1,n); jls.xicom=vector(1,n); jls.nrfunc=func; jls.localdata=localdata; for (j=1;j<=n;j++) { jls.pcom[j]=p[j]; jls.xicom[j]=xi[j]; } ax=0.0; xx=1.0; bx=2.0; j_mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,j_f1dim,(void *) &jls); *fret=j_brent(ax,xx,bx,j_f1dim,TOL,&xmin,(void *) &jls); for (j=1;j<=n;j++) { xi[j] *= xmin; p[j] += xi[j]; } free_vector(jls.xicom,1,n); free_vector(jls.pcom,1,n); }
void rkqs(float y[], float dydx[], int n, float *x, float htry, float eps, float yscal[], float *hdid, float *hnext, void (*derivs)(float, float [], float [])) { void rkck(float y[], float dydx[], int n, float x, float h, float yout[], float yerr[], void (*derivs)(float, float [], float [])); int i; float errmax,h,htemp,xnew,*yerr,*ytemp; yerr=vector(1,n); ytemp=vector(1,n); h=htry; for (;;) { rkck(y,dydx,n,*x,h,ytemp,yerr,derivs); errmax=0.0; for (i=1;i<=n;i++) errmax=FMAX(errmax,fabs(yerr[i]/yscal[i])); errmax /= eps; if (errmax <= 1.0) break; htemp=SAFETY*h*pow(float(errmax),float(PSHRNK)); h=(h >= 0.0 ? FMAX(htemp,0.1*h) : FMIN(htemp,0.1*h)); xnew=(*x)+h; if (xnew == *x) nrerror("stepsize underflow in rkqs"); } if (errmax > ERRCON) *hnext=SAFETY*h*pow(float(errmax),float(PGROW)); else *hnext=5.0*h; *x += (*hdid=h); for (i=1;i<=n;i++) y[i]=ytemp[i]; free_vector(ytemp,1,n); free_vector(yerr,1,n); }
int main(void) { long idum=(-17); int i,ibin,j; float chsq,df,prob,x,*bins1,*bins2; bins1=vector(1,NBINS); bins2=vector(1,NBINS); for (j=1;j<=NBINS;j++) { bins1[j]=0.0; bins2[j]=0.0; } for (i=1;i<=NPTS;i++) { x=expdev(&idum); ibin=(int) (x*NBINS/3.0+1); if (ibin <= NBINS) ++bins1[ibin]; x=expdev(&idum); ibin=(int) (x*NBINS/3.0+1); if (ibin <= NBINS) ++bins2[ibin]; } chstwo(bins1,bins2,NBINS,0,&df,&chsq,&prob); printf("\n%15s %15s\n","dataset 1","dataset 2"); for (i=1;i<=NBINS;i++) printf("%13.2f %15.2f\n",bins1[i],bins2[i]); printf("\n%18s %12.4f\n","chi-squared:",chsq); printf("%18s %12.4f\n","probability:",prob); free_vector(bins2,1,NBINS); free_vector(bins1,1,NBINS); return 0; }
/* following 3 routines do romberg integration on closed intervals */ void polint(float xa[], float ya[], int n, float x, float *y, float *dy) { int i,m,ns=1; float den,dif,dift,ho,hp,w; float *c,*d; dif=fabs(x-xa[1]); c=vector(1,n); d=vector(1,n); for (i=1;i<=n;i++) { if ( (dift=fabs(x-xa[i])) < dif) { ns=i; dif=dift; } c[i]=ya[i]; d[i]=ya[i]; } *y=ya[ns--]; for (m=1;m<n;m++) { for (i=1;i<=n-m;i++) { ho=xa[i]-x; hp=xa[i+m]-x; w=c[i+1]-d[i]; if ( (den=ho-hp) == 0.0) error("Error in routine polint"); den=w/den; d[i]=hp*den; c[i]=ho*den; } *y += (*dy=(2*ns < (n-m) ? c[ns+1] : d[ns--])); } free_vector(d,1,n); free_vector(c,1,n); }
int main(void) { int i,j; float eps,hdid,hnext,htry,x=1.0,*y,*dydx,*dysav,*ysav,*yscal; y=vector(1,N); dydx=vector(1,N); dysav=vector(1,N); ysav=vector(1,N); yscal=vector(1,N); ysav[1]=bessj0(x); ysav[2]=bessj1(x); ysav[3]=bessj(2,x); ysav[4]=bessj(3,x); derivs(x,ysav,dysav); for (i=1;i<=N;i++) yscal[i]=1.0; htry=0.6; printf("%10s %11s %12s %13s\n","eps","htry","hdid","hnext"); for (i=1;i<=15;i++) { eps=exp((double) -i); x=1.0; for (j=1;j<=N;j++) { y[j]=ysav[j]; dydx[j]=dysav[j]; } rkqs(y,dydx,N,&x,htry,eps,yscal,&hdid,&hnext,derivs); printf("%13f %8.2f %14.6f %12.6f \n",eps,htry,hdid,hnext); } free_vector(yscal,1,N); free_vector(ysav,1,N); free_vector(dysav,1,N); free_vector(dydx,1,N); free_vector(y,1,N); return 0; }
void dlinmin(float p[], float xi[], int n, float *fret, float (*func)(float []), void (*dfunc)(float [], float [])) { float dbrent(float ax, float bx, float cx, float (*f)(float), float (*df)(float), float tol, float *xmin); float f1dim(float x); float df1dim(float x); void mnbrak(float *ax, float *bx, float *cx, float *fa, float *fb, float *fc, float (*func)(float)); int j; float xx,xmin,fx,fb,fa,bx,ax; ncom=n; pcom=vector(1,n); xicom=vector(1,n); nrfunc=func; nrdfun=dfunc; for (j=1;j<=n;j++) { pcom[j]=p[j]; xicom[j]=xi[j]; } ax=0.0; xx=1.0; mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); *fret=dbrent(ax,xx,bx,f1dim,df1dim,TOL,&xmin); for (j=1;j<=n;j++) { xi[j] *= xmin; p[j] += xi[j]; } free_vector(xicom,1,n); free_vector(pcom,1,n); }
void rk4(double y[], double dydx[], int n, double x, double h, double yout[], void (*derivs)(double, double [], double [])) { int i; double xh, hh, h6, *dym, *dyt, *yt; dym = vector(1,n); /* define vector! */ dyt = vector(1,n); yt = vector(1,n); hh = h*0.5; h6 = h/6.0; xh = x+hh; for (i=1;i<=n;i++) yt[i]=y[i]+hh*dydx[i]; /* 1st step */ (*derivs)(xh,yt,dyt); /* 2nd step */ for (i=1;i<=n;i++) yt[i]=y[i]+hh*dyt[i]; (*derivs)(xh,yt,dym); /* 3rd step */ for (i=1;i<=n;i++) { yt[i]=y[i]+h*dym[i]; dym[i] += dyt[i]; } (*derivs)(x+h,yt,dyt); /* 4th step */ for (i=1;i<=n;i++) yout[i]=y[i]+h6*(dydx[i]+dyt[i]+2.0*dym[i]); free_vector(yt,1,n); free_vector(dyt,1,n); free_vector(dym,1,n); }
int main(void) { int i; float b1,b2,b3,b4,xf=X1+HTOT,*y,*yout,*dydx; y=vector(1,NVAR); yout=vector(1,NVAR); dydx=vector(1,NVAR); y[1]=bessj0(X1); y[2]=bessj1(X1); y[3]=bessj(2,X1); y[4]=bessj(3,X1); derivs(X1,y,dydx); b1=bessj0(xf); b2=bessj1(xf); b3=bessj(2,xf); b4=bessj(3,xf); printf("First four Bessel functions:\n"); for (i=5;i<=50;i+=5) { mmid(y,dydx,NVAR,X1,HTOT,i,yout,derivs); printf("\n%s %5.2f %s %5.2f %s %2d %s \n", "x=",X1," to ",X1+HTOT," in ",i," steps"); printf("%14s %9s\n","integration","bessj"); printf("%12.6f %12.6f\n",yout[1],b1); printf("%12.6f %12.6f\n",yout[2],b2); printf("%12.6f %12.6f\n",yout[3],b3); printf("%12.6f %12.6f\n",yout[4],b4); printf("\nPress RETURN to continue...\n"); (void) getchar(); } free_vector(dydx,1,NVAR); free_vector(yout,1,NVAR); free_vector(y,1,NVAR); return 0; }
int main(void) { int i,n; float ak,alf=(-0.5),bet=(-0.5),checkw,checkx,xx,*x,*w; x=vector(1,NP); w=vector(1,NP); for (;;) { printf("Enter N\n"); if (scanf("%d",&n) == EOF) break; gaujac(x,w,n,alf,bet); printf("%3s %10s %14s\n","#","x(i)","w(i)"); for (i=1;i<=n;i++) printf("%3d %14.6e %14.6e\n",i,x[i],w[i]); checkx=checkw=0.0; for (i=1;i<=n;i++) { checkx += x[i]; checkw += w[i]; } printf("\nCheck value: %15.7e should be: %15.7e\n", checkx,n*(bet-alf)/(alf+bet+2*n)); printf("\nCheck value: %15.7e should be: %15.7e\n", checkw,exp(gammln(1.0+alf)+gammln(1.0+bet)- gammln(2.0+alf+bet))*pow(2.0,alf+bet+1.0)); /* demonstrate the use of GAUJAC for an integral */ ak=0.5; for (xx=0.0,i=1;i<=n;i++) xx += w[i]*func(ak,x[i]); printf("\nIntegral from gaujac: %12.6f\n",xx); printf("Actual value: %12.6f\n",2.0*ellf(PIBY2,ak)); } free_vector(w,1,NP); free_vector(x,1,NP); return 0; }