|
Understanding numerical integration
You have to write a Java program, comparing the accuracy of numerical
integration techniques:
- start by creating a class IntegralTst
- import ptolemy.plot*.
- in your class:
- create a double method fun(double x), returning Exp(x)
- create a double method trapWt(int i, int N) returning a weight of
i-th integration point out of N in Trapezoid method
- create a double method simpWt(int i, int N) returning a weight of i-th integration point out N in Simpson's method
- create a double method intTrap(double a, double b, int N),
calculating the integral of fun(x), a<=x<=b using N points with
trapezoid method.
- create a double method intSimp(double a, double b, int N),
calculating the integral of fun(x), a<=x<=b using N points with
Simpson's method.
- in your main method
- create a double variables trap, simp, exact
- initialize exact with exp(5.)-1.;
- create an integer variable n
- define Plot variable myPlot
- start for loop over the variable n with the following parameters:
- initial value =1,
- final value=1001
- loop step=50
- in each iteration of your loop:
- calculate trap=intTrap(0,5,n);
- calculate simp=intSimp(0,5,n);
- add a point (i, log(abs(trap-exact))) to the first (0) set of myPlot;
connect the graph points; log scale will help you distinguish between
near-zero values
- add a point (i, log(abs(simp-exact))) to the second (1) set of myPlot;
connect the graph points
- display myPlot, print it (on paper)
- add a comment to your code about the relative accuracy of Trapezoidal
and Simpson's techniques.
- submit: printed code and the printed graph(s)
top
|