12.07.2015 Views

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

✞✝systems of equations with matrices; data fitting 183process, which means that there is an element of chance involved in just when adecay will occur, and so no two experiments are expected to give exactly the sameresults. The basic law of nature for spontaneous decay is that the number of decays∆N in a time interval ∆t is proportional to the number of particles N(t) present atthat time and to the time interval∆N(t)=− 1 ∆N(t)N(t)∆t ⇒ = −λN(t). (8.47)τ ∆t/∗ SplineAppl . java : Application version of cubic spline fitting . Interpolatesarray x[n] , y[n] , x0 < x1 . . . < x(n−1) . yp1 , ypn : y ’ at ends evaluated internallyy2 []: y" array ; yp1, ypn > e30 for natural spline ∗/import java . io .∗ ;public class SplineAppl {} }public static void main ( String [] argv ) throws IOException , FileNotFoundException {PrintWriter w = new PrintWriter (new FileOutputStream ("Spline .dat"), true );PrintWriter q = new PrintWriter (new FileOutputStream ("Input .dat") , true );double x[] = {0. ,1.2 ,2.5 ,3.7 ,5. ,6.2 ,7.5 ,8.7 ,9.9}; // inputdouble y[] = {0. ,0.93 ,.6 , − 0.53 , − 0.96 , − 0.08 ,0.94 ,0.66 , − 0.46};int i ,n = x. length , np = 15, klo , khi , k;double y2 [ ] = new double [9] , u[] = new double [n];double h, b, a , Nfit , p, qn, sig , un, yp1 , ypn, xout , yout ;for ( i=0; i < n; i++ ) q. println (" " + x[i] + " " + y[i] + " ");Nfit = 30; / / N output ptsyp1 = (y[1]−y[0])/(x[1]−x[0]) − (y[2]−y[1])/(x[2]−x[1]) +(y[2]−y[0])/(x[2]−x[0]) ;ypn = ( y [n−1]−y[n−2])/(x[n−1]−x[n−2]) − (y[n−2]−y[n−3])/(x[n−2]−x[n−3]) + (y[n−1]−y[n−3])/(x[n−1]−x[n−3]) ;if (yp1 > 0.99e30) y2[0] = u[0] = 0. ; // Naturalelse {y2[0] = (−0.5) ; u[0] = (3/(x[1]−x[0]) )∗ ((y[1]−y[0])/(x[1]−x[0])−yp1 ) ; }for ( i =1; i 0.99e30) qn = un = 0. ; // Test for naturalelse {qn = 0.5; un = (3/(x[n−1]−x[n−2]) ) ∗(ypn−(y [ n−1]−y[n−2])/(x[n−1]−x[n−2]) ) ; }y2 [n−1] = (un−qn∗u[n−2])/(qn∗y2 [n−2] + 1.) ;for ( k = n−2; k>= 0; k−−) y2[k] = y2[k]∗ y2[k + 1] + u[k];for ( i =1; i 1) {k = (khi+klo ) >> 1; if (x[k] > xout ) khi =k; else klo = k ;}h = x[ khi]−x[ klo ];if (x[k] > xout ) khi = k; else klo = k;h = x[ khi]−x[klo ]; a = (x[khi]−xout )/h;b = ( xout−x[ klo ])/h;yout = (a∗y[ klo ] + b∗y[ khi ] +((a∗a∗a−a)∗y2[ klo ] + (b∗b∗b−b)∗y2[ khi ]) ∗(h∗h) /6.) ;w. println (" " + xout + " " + yout + " ");}System . out . println ("data stored inSpline .dat");☎Listing 8.3 SplineAppl.java is an application version of an applet given on the CD thatperforms a cubic spline fit to data. The arrays x[ ] and y[ ] are the data to fit, and the valuesof the fit at Nfit points are output into the file Spline.dat.−101<strong>COPYRIGHT</strong> <strong>2008</strong>, PRINCET O N UNIVE R S I T Y P R E S SEVALUATION COPY ONLY. NOT FOR USE IN COURSES.ALLpup_06.04 — <strong>2008</strong>/2/15 — Page 183

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!