use matlab to do: Calculate the 2nd derivative and higher derivatives of f (x) = 1 + x + x^2 +x^3 + x^4 + x^5 + x^6 + x^7 + x^8 using the finite difference formulae for higher derivatives. How do the errors vary with step size?
Question
use matlab to do: Calculate the 2nd derivative and higher derivatives of f (x) = 1 + x + x^2 +x^3 + x^4 + x^5 + x^6 + x^7 + x^8 using the finite difference formulae for higher derivatives. How do the errors vary with step size?
Solution
To calculate the 2nd derivative and higher derivatives of the function using MATLAB, you can use the symbolic toolbox. Here's how you can do it:
- Define the function:
syms x
f = 1 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8;
- Calculate the 2nd derivative:
f2 = diff(f, x, 2);
- Calculate higher derivatives:
f3 = diff(f, x, 3);
f4 = diff(f, x, 4);
and so on.
To use the finite difference formulae for higher derivatives, you would need to define a step size h and calculate the derivatives using the finite difference approximations. For example, the second derivative can be approximated as:
h = 0.01; % Define a step size
x = 0:h:1; % Define a range of x values
f = 1 + x + x.^2 + x.^3 + x.^4 + x.^5 + x.^6 + x.^7 + x.^8; % Define the function
f2_approx = (f(1:end-2) - 2*f(2:end-1) + f(3:end)) / h^2; % Approximate the second derivative
To see how the errors vary with step size, you would need to calculate the exact derivatives, compare them with the approximations, and calculate the errors. You can then plot the errors against the step size to see how they vary.
Please note that the finite difference formulae are only approximations and the errors will depend on the step size and the specific function. The errors will generally decrease as the step size decreases, but they may also increase due to numerical precision issues for very small step sizes.
Similar Questions
use matlab to do: Calculate the 2nd derivative and higher derivatives of f (x) = 1 + x + x^2 +x^3 + x^4 + x^5 + x^6 + x^7 + x^8 using the finite difference formulae for higher derivatives, and use loglog to plot the chart. and use words tell me that how do the errors vary with step size?
use matlab to explain Estimate the error in a numerical derivative and/or integral from the convergence of the solution as the step size is changed. (It’s best to use step sizes that are large enough to avoid round-off error.) Compare your estimate of the error with actual error determined from comparison with the analytical solution, if an analytical solution is available.
Choose different step sizes h and plot a log-log graph of error vs step size to compare the numerical derivatives found using the forward, backward, and central difference formulas with the actual derivative. How accurate are the numerical derivatives at x = 0, 1, 1.5, and 2? At what step sizes do you obtain the minimum errors, and what are the minimum errors (see Sauer figure 5.1)? when x=? can get the minimum errors
Add some additional error to your polynomial. For example, try y =f(x) + 0.001 * randn(size(x));. How does the error in the numer- ical derivative vary with step size h?
4 Third order derivative.a. Use Taylor series to derive the truncation error of the approximationf′′′(x) ≈( −f(x − 2h) + 2f(x − h) − 2f(x + h) + f(x + 2h))/2h^3assuming f ∈ C^5.b. Explain why dividing by h produces no roundoff error if h = 2−k, k ∈ N.c. Assuming that built-in functions return exact answers but that addition/subtraction produce roundoff errors and that h = 2^−k, k ∈ Z, show that the roundoff error RE in computing this expression satisfies the bound |RE| ≤ (K1 + K2h)u/h^3 where K1, K2 depend on f and x. You may use using the bound |θn| <nu/ (1 − nu)d. Hence estimate, in terms of u, the optimal choice for h.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.