Question

Design and construct a computer program in Java. The following is a plot of the function...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Firstly, the derivative of sin(x3)+x2 is 3*x*x*cos(x)+2x or, to put it more formally:

f(x) = sin(x3)+x2
f'(x) = 3*x*x*cos(x)+2*x

you could solve sin(x) using the Taylor series for cos(x):

cos(x) = 1 - x^2/2| + x^2/4! + ...

with recursion. In Java:

class exp
{

public double cos(double x) {
  return 1 + next(-x*x/2, x, 3);
}
 
public double next(double term, double x, int i) {
  double next = -term * x * x / (i * (i + 1));
  return term + next(term, x, i + 2);
}


public static void main(String args[])
{
double x=2.0;
System.out.println("approximation to the derivative of f(x) at x = 2.0 “);

 System.out.println(" for f(x) = sin(x3) + x2”+ 3*x*x*cos(x)+2*x);


}
}

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

   }

}

Add a comment
Know the answer?
Add Answer to:
Design and construct a computer program in Java. The following is a plot of the function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please help with the question 1.03 and 2.02 . Chapter 1.03: Problem #3 3. What is the truncation error in the calcu...

    please help with the question 1.03 and 2.02 . Chapter 1.03: Problem #3 3. What is the truncation error in the calculation of the f'(x) that uses the approximation for /(x) =x, Ar=0.4 , and x = 5 . Chapter 2.02: Problem #3 3. Using forward divided difference scheme, find the first derivative of the function fx) -sin(2x) at x x/3 correct within 3 significant digits. Start with a step size of h0.01 and keep halving it till you find...

  • the question is from my Numerical methods and analysis course et /()-sin(), where is measured in radians. (a...

    the question is from my Numerical methods and analysis course et /()-sin(), where is measured in radians. (a). Calculate approximations to ) using Theorem 6.1 with h-0.1, h-0.01 and h-0.001, Carry eight or nine decimal places. (b). Compare with the value /(0.8)-cos(08), i.e. calculating the error of approximation. s(0.8) Theorem 6.1 (Centered Formula of Order 0(h)). Assume that fe Cla, bl and that x -h. x, x + h e la, bl. Then The notation S) stands for the set...

  • Please help me answer this question using matlab Consider the function f(x) x3 2x4 on the...

    Please help me answer this question using matlab Consider the function f(x) x3 2x4 on the interval [-2, 2] with h 0.25. Use the forward, backward, and centered finite difference approximations for the first and second derivatives so as to graphically illustrate which approximation is most accurate. Graph all three first-derivative finite difference approximations along with the theoretical, and do the same for the second derivative as well

  • Question 4 (16 Points) Use Neville's method to approximate V11 with the following function and values....

    Question 4 (16 Points) Use Neville's method to approximate V11 with the following function and values. Use this polynomial to approximate f(11). Using 4-digit rounding arithmatic. (a). f(x) = (x and the values Xo = 6, xı = 8, x2 = 10, x3 = 12 and X4 = 13. (b). Compare the accuracy of the approximation in parts (a).

  • (1) Use the Bisection method to find solutions accurate to within 10-2 for x3 – 7x2...

    (1) Use the Bisection method to find solutions accurate to within 10-2 for x3 – 7x2 + 14x – 6 = 0 on the interval [3.2, 4]. Using 4-digit rounding arithmatic. (2) Consider the function f(x) = cos X – X. (a). Approximate a root of f(x) using Fixed- point method accurate to within 10-2 . (b). Approximate a root of f(x) using Newton's method accurate to within 10-2. Find the second Taylor polynomial P2(x) for the function f(x) =...

  • question starts at let. than one variable. Let f:R? → R3 be the function given by...

    question starts at let. than one variable. Let f:R? → R3 be the function given by f(x, y) = (cos(x3 - y2), sin(y2 – x), e3x2-x-2y). (a) Let P be a point in the domain of f. As we saw in class, for (x, y) near P, we have f(x, y) f(P) + (Dpf)(h), where h = (x, y) - P. The expression on the right hand side is called the linear approximation of f around P. Compute the linear...

  • Please explain the solution and write clearly for nu, ber 25. Thanks. 25. Approximate the following functions f(x) as a linear combination of the first four Legendre polynomials over the interval [-...

    Please explain the solution and write clearly for nu, ber 25. Thanks. 25. Approximate the following functions f(x) as a linear combination of the first four Legendre polynomials over the interval [-1,1]: Lo(x) = 1, Li(x) = x, L2(x) = x2-1. L3(x) = x3-3x/5. (a) f(x) = X4 (b) f(x) = k (c) f(x) =-1: x < 0, = 1: x 0 Example 8. Approximating e by Legendre Polynomials Let us use the first four Legendre polynomials Lo(x) 1, Li(x)...

  • Section A Q1 0 Using the following Taylor series expansion: f(x+h) = f(x)+hf'(x)+22 h 3! f"(x)+...

    Section A Q1 0 Using the following Taylor series expansion: f(x+h) = f(x)+hf'(x)+22 h 3! f"(x)+ (+0) (1.1) 4! show that the central finite difference formula for the first derivative can be written as: f'(x)= f(x+h)-f(x-1) + ch" +0(hº) (1.2) 2h Determine cp and of the derived equation. [4 marks] Consider the function: f(x) = sin +COS (1.3) 2 2 Let x =ih with n=0.25, give your answer in 3 decimals for (ii) to (vi): (ii) Evaluate f(x) for i...

  • Midterm Examination         CSCI 3321        Summer, 2015                     &n

    Midterm Examination         CSCI 3321        Summer, 2015                      ___________             Your Name …                 .. Please answer each question by entering the most nearly correct answer (a, b, c, d) in the blank on the left.                ____1. When approximating ex by a truncated Taylor series (expanded about x=0), how many terms would be required to keep the absolute value of the error below 10-3 over the interval [-1,1] ?                               a. 1                b. 2                c. 3                d. 4 ____2. The polynomial that...

  • I need help creating program for the Test_Bisection pseudocode. I need to find a root for...

    I need help creating program for the Test_Bisection pseudocode. I need to find a root for each function f and g and get the output below. Pseudocode Now let's construct pseudocode to carry out this procedure. We shall not try to create a piece of high-quality software with many "bells and whistles,” but we write the pseudocode in the form of a procedure for general use. This allows the reader an opportunity to review how a main program and one...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT