第五题/*计算物理作业 3.5题*学号:20092200129*/程序:public class sun {public static void main(String argv[]){double del=1e-6;int n=10,m=10;double h=Math.PI/(2*n);//Evaluate the derivative and output the resultint k=0;for(int i=0;i<=n;++i){double x=h*i;double d=(f(x+h)-2*f(x)+f(x-h))/(h*h);double f2=firstOrderDerivative3(x,h,d,del,k,m);double df2=f2-O(x,f2);System.out.println("x="+x);System.out.println(" f"(x)="+f2);System.out.println("Error in f"(x):"+df2);System.out.println();System.out.println();}}//Method to carry out 2st-order derivative throuth the adaptive scheme.public static double firstOrderDerivative3(double x,double h,doubled,double del,int step,int maxstep){step++;h=h/2;double d2=(f(x+h)-2*f(x)+f(x-h))/(h*h);if(step>=maxstep){System.out.println("Not converged after"+step+"recursions");return d2;}else{if((h*h*Math.abs(d-d2))<del)return(4*d2-d)/3;else return firstOrderDerivative3(x,h,d2,del,step,maxstep);}}public static double f(double x){return Math.exp(-x)*Math.log(x);}public static double O(double x,double f2){returnMath.exp(-x)*Math.log(x)-(2*Math.exp(-x))/x-Math.exp(-x)/(x*x);}}运行结果:Not converged after10recursionsx=0.0f"(x)=NaNError in f"(x):NaNx=0.15707963267948966f"(x)=-47.10058585750553Error in f"(x):3.329329885559673E-6x=0.3141592653589793f"(x)=-12.896114166464196Error in f"(x):8.610592701074893E-7x=0.47123889803846897f"(x)=-5.929972627646059Error in f"(x):1.254731920141694E-6x=0.6283185307179586f"(x)=-3.2974024228908525Error in f"(x):2.3189374909193816E-7x=0.7853981633974483f"(x)=-2.010313400857776Error in f"(x):1.0145469402367269E-6x=0.9424777960769379f"(x)=-1.2886478977422928Error in f"(x):3.5448832647055895E-7x=1.0995574287564276f"(x)=-0.8495690372991292Error in f"(x):1.4696104200240256E-7x=1.2566370614359172f"(x)=-0.568185223209793Error in f"(x):6.901975246886849E-8x=1.413716694115407f"(x)=-0.38160076561659223Error in f"(x):3.562357669961713E-8x=1.5707963267948966f"(x)=-0.25505577758216Error in f"(x):3.170312274369813E-7第六题解题思路:首先定义主函数,计算出x[i],f[i],然后调用两个子函数,两个子函数分别计算泰勒展开式求最后一项的结果和辛普森方法求得last slice的结果。
对于两个子函数来书直接用last=h*(5*y[n]+8*y[n-1]-y[n-2])/12即可求得辛普森算法的最后一个的结果而泰勒方法中最后一项等于=h*h*f(x0)的一阶导数/2+h*f[n-1]+h*h*h*f(x0)的二阶导数/二的阶乘。
对于f(x0)的一阶和二阶导数则由另外两个子程序分别计算一阶导数=c[i]=(y[i+1]-y[i-1])/(2*h);二阶导数c[i]=(y[i+1]-2*y[i]+y[i-1])/(h*h);将所得值比较即可。
源程序:import ng.*;public class n2 {static final int n=9;public static void main (String argv[]){double f[]= new double[n+1];double h=Math.PI/(2.0*n);//定义均匀间隔for(int i=0;i<=n;i++){double x=h*i;f[i]=Math.sin(x);//为f[i]赋值}double l=lastlice(f,h);double t=taylor(f,h);System.out.println("lastlice is "+l);System.out.println("taylor is "+t);if(l == t)System.out.println("they are equal");elseSystem.out.println("they are not equal");}//辛普森方法的last slicepublic static double lastlice(double f[],double h){d ouble y[]= new double [n+1];f or(int i=0;i<=n;i++){y[i]=f[i];}d ouble l=h*(5*y[n]+8*y[n-1]-y[n-2])/12;r eturn l;}public static double taylor(double f[],double h){//泰勒展开式求最后一项d ouble y[]= new double [n+1];f or(int i=0;i<=n;++i){y[i]=f[i];}d ouble fl=first(n,y,h);d ouble s=second(n,y,h);d ouble t=h*h*fl/2+h*f[n-1]+h*h*h*s/6;return t;}public static double first(int n,double y[],double h){ //定义first orderd ouble c[]=new double [n+1];f or(int i=1;i<n;i++){c[i]=(y[i+1]-y[i-1])/(2*h);}r eturn c[n-1];}p ublic static double second(int n,double y[],double h){ //定义second orderdouble c[]=new double [n+1];for(int i=1;i<n;i++){c[i]=(y[i+1]-2*y[i]+y[i-1])/(h*h);}return c[n-1];}}运行结果:lastlice is 0.1736423622701241taylor is 0.1736423622701241they are equal事实证明结果一样。
第十一题// An example of searching for a root via the secant method // for f(x)=exp(x)*ln(x)-x*x=0.import ng.*;public class Root {public static void main(String argv[]) {double del = 1e-6, a = 0, b =2;double dx = (b-a)/10, x = (a+b)/2;int n = 6;x = secant(n, del, x, dx);System.out.println("Root obtained: " + x);}// Method to carry out the secant search.public static double secant(int n, double del,double x, double dx) {int k = 0;double x1 = x+dx;while ((Math.abs(dx)>del) && (k<n)) {double d = f(x1)-f(x);double x2 = x1-f(x1)*(x1-x)/d;x = x1;x1 = x2;dx = x1-x;k++;}if (k==n) System.out.println("Convergence not" + " found after " + n + " iterations");return x1;}public static double f(double x) {return Math.exp(x*x)*Math.log(x*x)-x;}}解题思路:f(x)=ⅇx2∗Log[ⅇ,x2]−x的大致图像如图由于正割法对初始值有依赖,只能逼近较近的根,所以我们可以通过改变初始点来求解函数的多个根;容易观察到函数在[-1,2]区间上有两个根,所以调整初值求解如下:运行结果:a=-1,b=2时:Root obtained: 1.1624063469159338a=-1,b=0时:Root obtained: -0.8105015386160186第十三题// An example of searching for a minimum of a multivariable// function through the steepest-descent method.import ng.*;public class minimum13 {static double c=-1;//区间static double d=2;//区间public static void main(String argv[]) {double del = 1e-6, a = 0.1;double x =0.2;x=steepestDescent(x, a, del);//compare the functions at the points of x=c,x=ddouble min=0;double mx=0;if(g(c)>g(d)){min=g(d);mx=d;}else{min=g(c);mx=c;}//ensure the x isamong [c,d]if(x>c&&x<d){if(g(x)<min) {min=g(x);mx=x;}}System.out.println("The minimum is at"+ " x= " + mx+" "+"minimum=" +min);}// Method to carry out the steepest-descent search.public static double steepestDescent(double x,double a, double del) {double h = 1e-6;double g0 = g(x);double fi = f(x, h);double dg = 0;dg += fi*fi;dg = Math.sqrt(dg);double b = a/dg;while (dg > del) {x-= b*fi;h /= 2;fi = f(x, h);dg = 0;dg += fi*fi;dg = Math.sqrt(dg);b = a/dg;double g1 = g(x);if (g1 > g0) a/= 2;else g0 = g1;}return x;}// Method to provide the gradient of g(x).public static double f(double x, double h) {double z;double y = x;y+=h;double g0 = g(x);z = (g(y)-g0)/h;return z;}// Method to provide function g(x).public static double g(double x) {return x*x;}}运行结果:在区间[-1,2]:The minimum is at x= 0.0 minimum=0.0在区间[3,5]: The minimum is at x= 3.0 minimum=9.0第十五题程序://第十五题任时坤//let ∑=sigma=E=m=1;//so V=4(pow((1/r),12)-pow((1/r),6))//using the exp(theta) and log(ruth) to plot the relations between theta and cross section;public class Lennard15 {static final int n = 1000, m = 100;static double b;public static void main(String argv[]) {int nc = 100, ne = 2;double del = 1e-6, db = 0.5, b0 = 0.01, h = 0.01;double g1, g2;double theta[] = new double[n+1];double fi[] = new double[n+1];double sig[] = new double[m+1];for (int i=0; i<=m; ++i) {b = b0+i*db;// Calculate the first term of thetafor (int j=0; j<=n; ++j) {double r = b+h*(j+1);fi[j] = 1/(r*r*Math.sqrt(fb(r)));}g1 = simpson(fi, h);// Find r_m from 1-b*b/(r*r)-U/E = 0double rm = secant(nc, del, b, h);// Calculate the second term of thetafor (int j=0; j<=n; ++j) {double r = rm+h*(j+1);fi[j] = 1/(r*r*Math.sqrt(f(r)));}g2 = simpson(fi, h);theta[i] = 2*b*(g1-g2);}// Calculate d theta/d bsig = firstOrderDerivative(db, theta, ne);// Put the cross section in log form with the exact// result of the Coulomb scattering (ruth)//theta = abs(theta)for (int i=m; i>=0; --i) {b = b0+i*db;sig[i] = b/(Math.abs(sig[i])*Math.sin(Math.abs(theta[i])));theta[i] = Math.abs(theta[i]);double ruth = 1/Math.pow(Math.sin(theta[i]/2),4);ruth /= 16;// because of sig and ruth are always very big numbersdouble si = Math.log(sig[i]);double ru = Math.log(ruth);//useing the exp(theta)---log(ruth)://System.out.println(Math.exp(theta[i])+" "+ru);System.out.println("theta = " + theta[i]);System.out.println("ln sigma(theta) = " + si);System.out.println("ln sigma_r(theta) = " + ru);System.out.println();}}// Method to achieve the evenly spaced Simpson rule.public static double simpson(double y[], double h) { int n = y.length-1;double s0 = 0, s1 = 0, s2 = 0;for (int i=1; i<n; i+=2) {s0 += y[i];s1 += y[i-1];s2 += y[i+1];}double s = (s1+4*s0+s2)/3;// Add the last slice separately for an even n+1if ((n+1)%2 == 0)return h*(s+(5*y[n]+8*y[n-1]-y[n-2])/12);elsereturn h*s;}// Method to carry out the secant search.public static double secant(int n, double del,double x, double dx) {int k = 0;double x1 = x+dx;while ((Math.abs(dx)>del) && (k<n)) {double d = f(x1)-f(x);double x2 = x1-f(x1)*(x1-x)/d;x = x1;x1 = x2;dx = x1-x;k++;}if (k==n) System.out.println("Convergence not" +" found after " + n + " iterations");return x1;}// Method for the 1st-order derivative with the 3-point // formula. Extrapolations are made at the boundaries.public static double[] firstOrderDerivative(double h,double f[], int m) {int n = f.length-1;double[] y = new double[n+1];double[] xl = new double[m+1];double[] fl = new double[m+1];double[] fr = new double[m+1];for (int i=1; i<n; ++i)y[i] = (f[i+1]-f[i-1])/(2*h);// Lagrange-extrapolate the boundary pointsfor (int i=1; i<=(m+1); ++i) {xl[i-1] = h*i;fl[i-1] = y[i];fr[i-1] = y[n-i];}y[0] = aitken(0, xl, fl);y[n] = aitken(0, xl, fr);return y;}// The Aitken method for the Lagrange interpolation.public static double aitken(double x, double xi[],double fi[]) {int n = xi.length-1;double ft[] = (double[]) fi.clone();for (int i=0; i<n; ++i) {for (int j=0; j<n-i; ++j) {ft[j] = (x-xi[j])/(xi[i+j+1]-xi[j])*ft[j+1]+(x-xi[i+j+1])/(xi[j]-xi[i+j+1])*ft[j];}}return ft[0];}// Method to provide function f(x) for the root search.public static double f(double x) {return 1-b*b/(x*x)-4*0.01*(Math.pow(4/x, 12)-Math.pow(4/x, 6));}// Method to provide function 1-b*b/(x*x).public static double fb(double x) {return 1-b*b/(x*x);}}结果:theta = 1.846559973157913E-8ln sigma(theta) = 39.46332190354649ln sigma_r(theta) = 71.2294252391947theta = 1.9678432294709114E-8ln sigma(theta) = 41.44547859635677ln sigma_r(theta) = 70.97497043326803theta = 2.0984203072983707E-8ln sigma(theta) = 41.29685975650948ln sigma_r(theta) = 70.71798366765296theta = 2.23910832153687E-8ln sigma(theta) = 41.146741657981565ln sigma_r(theta) = 70.45841211231631theta = 2.390804116802342E-8ln sigma(theta) = 40.99510503114457ln sigma_r(theta) = 70.1962039362033theta = 2.554492433300831E-8ln sigma(theta) = 40.84190815616484ln sigma_r(theta) = 69.9313087850952theta = 2.731263311225862E-8ln sigma(theta) = 40.68710838170971ln sigma_r(theta) = 69.66366596170842theta = 2.9223164015913793E-8ln sigma(theta) = 40.530684636823345ln sigma_r(theta) = 69.39321661577095ln sigma(theta) = 40.37260354657837 ln sigma_r(theta) = 69.11990334355792theta = 3.3526957719659523E-8ln sigma(theta) = 40.21283100549856 ln sigma_r(theta) = 68.84366405467912theta = 3.595097888327986E-8ln sigma(theta) = 40.0513********ln sigma_r(theta) = 68.56443809651903theta = 3.857973084525623E-8ln sigma(theta) = 39.888040827526325 ln sigma_r(theta) = 68.28215522411509theta = 4.143302109810981E-8ln sigma(theta) = 39.72294822235678 ln sigma_r(theta) = 67.99675065188941theta = 4.453289077006872E-8ln sigma(theta) = 39.55599779288899 ln sigma_r(theta) = 67.70815120943685theta = 4.790380100420932E-8ln sigma(theta) = 39.387160641391176 ln sigma_r(theta) = 67.41628393108627theta = 5.157289538093579E-8ln sigma(theta) = 39.216384308703816 ln sigma_r(theta) = 67.12107834313481theta = 5.5570533061197186E-8ln sigma(theta) = 39.043619581398794 ln sigma_r(theta) = 66.82245102837247theta = 5.99304568799086E-8ln sigma(theta) = 38.86882832471159 ln sigma_r(theta) = 66.52032399578432theta = 6.46903786532399E-8ln sigma(theta) = 38.691957273192436 ln sigma_r(theta) = 66.21461341422884ln sigma(theta) = 38.51295815805794ln sigma_r(theta) = 65.90523242502154theta = 7.558386080179501E-8ln sigma(theta) = 38.3317748114848ln sigma_r(theta) = 65.5920922320363theta = 8.181753405028802E-8ln sigma(theta) = 38.14835589090251ln sigma_r(theta) = 65.27509705445983theta = 8.865279546306503E-8ln sigma(theta) = 37.96264830487491ln sigma_r(theta) = 64.95415308486821theta = 9.615640111509949E-8ln sigma(theta) = 37.77458657724528ln sigma_r(theta) = 64.62915917147035theta = 1.0440352998339342E-7ln sigma(theta) = 37.58411412087224ln sigma_r(theta) = 64.30000939987208theta = 1.1347885113151267E-7ln sigma(theta) = 37.39116871455187ln sigma_r(theta) = 63.96659740412114theta = 1.2347806189527687E-7ln sigma(theta) = 37.195682818068484 ln sigma_r(theta) = 63.62880933253239theta = 1.345093455251689E-7ln sigma(theta) = 36.997588804528476 ln sigma_r(theta) = 63.28652862743951theta = 1.4669530836800004E-7ln sigma(theta) = 36.796815908764096 ln sigma_r(theta) = 62.939632533761774theta = 1.6017502676726746E-7ln sigma(theta) = 36.593289325848964 ln sigma_r(theta) = 62.58799480923479ln sigma(theta) = 36.38693222724815ln sigma_r(theta) = 62.23148173935577theta = 1.9167030948119556E-7ln sigma(theta) = 36.17766504484714ln sigma_r(theta) = 61.86995631603575theta = 2.1007138338434426E-7ln sigma(theta) = 35.96540241655411ln sigma_r(theta) = 61.50327377244451theta = 2.3054472993791484E-7ln sigma(theta) = 35.750057714899796 ln sigma_r(theta) = 61.13128374882553theta = 2.5335927519312685E-7ln sigma(theta) = 35.531539119699225 ln sigma_r(theta) = 60.753829181994995theta = 2.7882362735029416E-7ln sigma(theta) = 35.30975100457115ln sigma_r(theta) = 60.37074566022896theta = 3.07292622861513E-7ln sigma(theta) = 35.08459242279057ln sigma_r(theta) = 59.98186149762503theta = 3.3917520823927016E-7ln sigma(theta) = 34.855959129143315 ln sigma_r(theta) = 59.586996098468155theta = 3.7494352488370854E-7ln sigma(theta) = 34.623741482142805 ln sigma_r(theta) = 59.18596169050993theta = 4.151441435293335E-7ln sigma(theta) = 34.38782385725622ln sigma_r(theta) = 58.77856017286608theta = 4.604110535080114E-7ln sigma(theta) = 34.14808583292972ln sigma_r(theta) = 58.36458460759438ln sigma(theta) = 33.904400024954256 ln sigma_r(theta) = 57.94381718855084theta = 5.692151840329396E-7ln sigma(theta) = 33.65663315821511ln sigma_r(theta) = 57.51602917996846theta = 6.346164715751585E-7ln sigma(theta) = 33.40464498182495ln sigma_r(theta) = 57.080980009182426theta = 7.08862596056209E-7ln sigma(theta) = 33.148287972084795 ln sigma_r(theta) = 56.63841651533806theta = 7.933367794020485E-7ln sigma(theta) = 32.8874063658783ln sigma_r(theta) = 56.188072060697465theta = 8.896690350412157E-7ln sigma(theta) = 32.62183592432788ln sigma_r(theta) = 55.72966525648614theta = 9.997857765142727E-7ln sigma(theta) = 32.351403415585345 ln sigma_r(theta) = 55.26289921759669theta = 1.1259707037666184E-6ln sigma(theta) = 32.07592576636889ln sigma_r(theta) = 54.787460186218226theta = 1.2709395789602812E-6ln sigma(theta) = 31.795209114472588 ln sigma_r(theta) = 54.30301642030872theta = 1.4379326957132678E-6ln sigma(theta) = 31.50904830915759ln sigma_r(theta) = 53.809216415418184theta = 1.6308292001151331E-6ln sigma(theta) = 31.21722588807881ln sigma_r(theta) = 53.30568784307433ln sigma(theta) = 30.919510821416043 ln sigma_r(theta) = 52.79203535679371theta = 2.1139343178147585E-6ln sigma(theta) = 30.61565773149832ln sigma_r(theta) = 52.267838964384744theta = 2.416566206005638E-6ln sigma(theta) = 30.305405437773395 ln sigma_r(theta) = 51.732651794235196theta = 2.770453722952815E-6ln sigma(theta) = 29.988475657942747 ln sigma_r(theta) = 51.18599780911595theta = 3.1856879611830147E-6ln sigma(theta) = 29.664571469016604 ln sigma_r(theta) = 50.62736916757544theta = 3.6746383340544035E-6ln sigma(theta) = 29.333375610469282 ln sigma_r(theta) = 50.05622337058027theta = 4.252536203071811E-6ln sigma(theta) = 28.99454854777367ln sigma_r(theta) = 49.47197999740563theta = 4.93822662616647E-6ln sigma(theta) = 28.647726366980937 ln sigma_r(theta) = 48.87401709509007theta = 5.755142357569132E-6ln sigma(theta) = 28.292518410731745 ln sigma_r(theta) = 48.26166711907795theta = 6.732573461354642E-6ln sigma(theta) = 27.928504575092518 ln sigma_r(theta) = 47.634212403477896theta = 7.907333024784469E-6ln sigma(theta) = 27.555232254078774 ln sigma_r(theta) = 46.99087999219401ln sigma(theta) = 27.17221300370534ln sigma_r(theta) = 46.330835826113045theta = 1.1047630828540518E-5ln sigma(theta) = 26.778918618778874 ln sigma_r(theta) = 45.653178*********theta = 1.3148109286668792E-5ln sigma(theta) = 26.374776732486097 ln sigma_r(theta) = 44.95693036066628theta = 1.5725012876565414E-5ln sigma(theta) = 25.959165866061433 ln sigma_r(theta) = 44.24103174611569theta = 1.8905037492587125E-5ln sigma(theta) = 25.531409680376637 ln sigma_r(theta) = 43.504328549712504theta = 2.2853858185053202E-5ln sigma(theta) = 25.090770478433576 ln sigma_r(theta) = 42.74556242615165theta = 2.778985758680505E-5ln sigma(theta) = 24.636441723209035 ln sigma_r(theta) = 41.96335775550143theta = 3.4003342543032086E-5ln sigma(theta) = 24.167539454931855 ln sigma_r(theta) = 41.15620691255692theta = 4.18837209046552E-5ln sigma(theta) = 23.683092446964054 ln sigma_r(theta) = 40.322453316662596theta = 5.195835833631451E-5ln sigma(theta) = 23.18203088644655ln sigma_r(theta) = 39.46027184613055theta = 6.4948782643445E-5ln sigma(theta) = 22.66317341440529ln sigma_r(theta) = 38.56764623271658ln sigma(theta) = 22.12521233161425ln sigma_r(theta) = 37.642342997328385theta = 1.0406754740503121E-4ln sigma(theta) = 21.566696875806777 ln sigma_r(theta) = 36.68188150330053theta = 1.3357132418628624E-4ln sigma(theta) = 20.98601462403819ln sigma_r(theta) = 35.68349983987055theta = 1.7320596395968308E-4ln sigma(theta) = 20.381371414537902 ln sigma_r(theta) = 34.6441165189257theta = 2.2711088897784586E-4ln sigma(theta) = 19.75077092040879ln sigma_r(theta) = 33.560288656833634theta = 3.01409068290954E-4ln sigma(theta) = 19.09194311482954ln sigma_r(theta) = 32.42816875512143theta = 4.053162908582683E-4ln sigma(theta) = 18.40251914958873ln sigma_r(theta) = 31.243371349660784theta = 5.529029996976504E-4ln sigma(theta) = 17.679849592743274 ln sigma_r(theta) = 30.001311967934182theta = 7.661195683031765E-4ln sigma(theta) = 16.921021997658006 ln sigma_r(theta) = 28.696689321881497theta = 0.0010797952324843956ln sigma(theta) = 16.12320163486973ln sigma_r(theta) = 27.323935615821338theta = 0.0015502182762195764ln sigma(theta) = 15.283874246657785 ln sigma_r(theta) = 25.87743853892223theta = 0.002269769554237988ln sigma(theta) = 14.401745878318513ln sigma_r(theta) = 24.352308741038165theta = 0.0033912436700995877ln sigma(theta) = 13.478901248373013ln sigma_r(theta) = 22.74623615888332theta = 0.005164095381224361ln sigma(theta) = 12.526616258062047ln sigma_r(theta) = 21.0641057876601theta = 0.007965793323853805ln sigma(theta) = 11.583767293951484ln sigma_r(theta) = 19.330405533020915theta = 0.012192937936150904ln sigma(theta) = 10.797226474868692ln sigma_r(theta) = 17.6276181881397theta = 0.017210180336480897ln sigma(theta) = 11.564602732336702ln sigma_r(theta) = 16.249066126079722theta = 0.014957527039389101ln sigma(theta) = 8.58553644926581ln sigma_r(theta) = 16.81019978782069theta = 0.039112308192289655ln sigma(theta) = 5.9394958026453475ln sigma_r(theta) = 12.965527261356753theta = 0.2550969665562406ln sigma(theta) = 3.1561366006712133ln sigma_r(theta) = 5.475297811713763theta = 0.6315321811346928ln sigma(theta) = 1.8701683740161505ln sigma_r(theta) = 1.9051200004245903theta = 1.040747665790346ln sigma(theta) = 1.2952272070259088ln sigma_r(theta) = 0.02242657177744099theta = 1.428172678843424ln sigma(theta) = 1.0021476205032662ln sigma_r(theta) = -1.079664214060937theta = 1.7861676543974974ln sigma(theta) = 0.8169839653186198ln sigma_r(theta) = -1.773658246046629theta = 2.1110088158201457ln sigma(theta) = 0.7031431382362978ln sigma_r(theta) = -2.2162250149887663theta = 2.369163840140268ln sigma(theta) = -0.3748970500196441ln sigma_r(theta) = -2.466540159639649theta = 1.0478280744015163ln sigma(theta) = -5.90995091993309ln sigma_r(theta) = -0.002183401622834268第十六题//using the simpson methodpublic class pendulum_16 {static final int n = 100000;static final double pi=Math.PI;static final double l=1,g=9.8;public static void main(String argv[]) {double f[] = new double[n+2];double space[] ={pi/128,pi/64,pi/32,pi/16,pi/8,pi/4,pi/2};for(int k=space.length-1;k>=0;k--){double sp=space[k];double h=sp/n;for (int i=0; i<n; i++) {double theta = h*i;f[i] = 4*Math.sqrt(l/(2*g))/Math.sqrt(Math.cos(theta)-Math.cos(sp));}double s = simpson(f, h);System.out.println("While the theta0 = "+space[k]+" The integral is: " + s); }System.out.println("The T = 2pi*sqrt(l/g) = "+2*pi*Math.sqrt(l/g));}//Method to achieve the evenly spaced Simpson rule.public static double simpson(double y[], double h) {int n = y.length-1;double s0 = 0, s1 = 0, s2 = 0;for (int i=1; i<n; i+=2) {s0 += y[i];s1 += y[i-1];s2 += y[i+1];}double s = (s1+4*s0+s2)/3;// Add the last slice separately for an even n+1if ((n+1)%2 == 0)return h*(s+(5*y[n]+8*y[n-1]-y[n-2])/12);elsereturn h*s;}}运行结果:While the theta0 = 1.5707963267948966 The integral is: 2.364243960873343 While the theta0 = 0.7853981633974483 The integral is: 2.0832788690555177 While the theta0 = 0.39269908169872414 The integral is: 2.022723192575113 While the theta0 = 0.19634954084936207 The integral is: 2.00809006000594 While the theta0 = 0.09817477042468103 The integral is: 2.0044621218895236 While the theta0 = 0.04908738521234052 The integral is: 2.003557015039689 While the theta0 = 0.02454369260617026 The integral is: 2.003330855409693 The T = 2pi*sqrt(l/g) = 2.007089923154493第十七题://用simpson方法解决此积分问题;public class problem_17 {static final double l=1,g=9.8;static final double thit=0;static final int n = 1000;static final double pi=Math.PI;public static void main(String argv[]) { double f[] = new double[n+1];double space[] ={pi/8,pi/4,pi/2};for(int k=0;k<space.length;k++){double sp=space[k];double h=sp/n;for (int i=0; i<n; i++) {double theta = h*i;f[i] = 0.5*Math.sqrt(l/(3*g))*Math.sqrt((1+3*Math.cos(theta)*Math.cos(theta))/(Math.sin(sp)-Math.sin(theta)));}double s = simpson(f, h);System.out.println("theta0 = "+space[k]+" The integral of t is: " + s); }}//Method to achieve the evenly spaced Simpson rule.public static double simpson(double y[], double h) {int n = y.length-1;double s0 = 0, s1 = 0, s2 = 0;for (int i=1; i<n; i+=2) {s0 += y[i];s1 += y[i-1];s2 += y[i+1];}double s = (s1+4*s0+s2)/3;// Add the last slice separately for an even n+1if ((n+1)%2 == 0)return h*(s+(5*y[n]+8*y[n-1]-y[n-2])/12);elsereturn h*s;}}运行结果:theta0 = 0.39269908169872414 The integral of t is: 0.22615514392751754 theta0 = 0.7853981633974483 The integral of t is: 0.3206139187126751 theta0 = 1.5707963267948966 The integral of t is: 1.1404447118395957第十八题// set : T0= 2*pi,E0=1.6,// w = T0*Math.sqrt(E0)/(2*pi*Math.sqrt(2*m))=1;// u = 10//so x(u) = ∫ (1+alfa[k]*Math.exp(-e/E0))/Math.sqrt(u-e) dxpublic class problem_18 {static final int n = 10000;static final double pi=Math.PI,u=10;static final double T0=2*pi,E0=1.6,w=1;public static void main(String argv[]) {double f[] = new double[n+1];double alfa[] = {0,0.1,1,10};for(int k=0;k<alfa.length;k++){double h=u/n;for (int i=0; i<n; i++) {double e = h*i;f[i] = (1+alfa[k]*Math.exp(-e/E0))/Math.sqrt(u-e);}double s = simpson(f, h);System.out.println("alfa = "+alfa[k]+" The integral is: " + s); }}//Method to achieve the evenly spaced Simpson rule.public static double simpson(double y[], double h) {int n = y.length-1;double s0 = 0, s1 = 0, s2 = 0;for (int i=1; i<n; i+=2) {s0 += y[i];s1 += y[i-1];s2 += y[i+1];}double s = (s1+4*s0+s2)/3;// Add the last slice separately for an even n+1if ((n+1)%2 == 0)return h*(s+(5*y[n]+8*y[n-1]-y[n-2])/12);elsereturn h*s;}}运行结果:alfa = 0.0 The integral is: 6.284751047501293 alfa = 0.1 The integral is: 6.341179576835809 alfa = 1.0 The integral is: 6.849036340846619 alfa = 10.0 The integral is: 11.927603980954794。