Design and construct a computer program in Java The followin
Design and construct a computer program in Java. The following is a plot of the function f(x) = sin(x3) + x2 : In order to illustrate the effects of the two major error sources, rounding and truncation, attempt to determine an approximation to the derivative of f(x) at x = 2.0 radians using the difference approximation given below. (The true answer is 4 + 12 cos(8) or about 2.2539995942966376896). Use the formula: f\'(x) (f(x+h) - f(x)) / h with h=1, 0.5, 0.25, ... 1.8189894035459e-12 (i.e., keep halving h until it is less than 2.0e-12.) Print out the values of h, your approximation to f\'(x), and the error in the approximation for each value of h used. Thanks
Solution
Firstly, the derivative of sin(x3)+x2 is 3*x*x*cos(x)+2x or, to put it more formally:
you could solve sin(x) using the Taylor series for cos(x):
with recursion. In Java:
class exp
{
public static void main(String args[])
{
double x=2.0;
System.out.println(\"approximation to the derivative of f(x) at x = 2.0 “);
}
}
Error handling
For user-defined functions, when the method encounters an error during evaluation, users must use their own unchecked exceptions. The following example shows
private static class LocalException extends RuntimeException {
// the x value that caused the problem
private final double x;
public LocalException(double x) {
this.x = x;
}
public double getX() {
return x;
}
}
private static class MyFunction implements UnivariateFunction {
public double value(double x) {
double y = hugeFormula(x);
if (somethingBadHappens) {
throw new LocalException(x);
}
return y;
}
}
public void compute() {
try {
solver.solve(maxEval, new MyFunction(a, b, c), min, max);
} catch (LocalException le) {
// retrieve the x value
}
}

