Question

In: Computer Science

Using Matlab, consider the function f(x) = x^3 – 2x + 4 on the interval [-2,...

Using Matlab, consider the function f(x) = x^3 – 2x + 4 on the interval [-2, 2] with h = 0.25. Write the MATLAB function file to find the first derivatives in the entire interval by all three methods i.e., forward, backward, and centered finite difference approximations.

Could you please add the copiable Matlab code and the associated screenshots? Thank you!

Solutions

Expert Solution

Answer :-

%% Function f and its derivatives

f = @(x) x.^3-2*x+4;

f_1 = @(x) 3*x.^2 -2; % first derivative

f_2 = @(x) 6*x; % second derivative

h = 0.25;

%% First derivative finite difference approximations

f_1b = @(x) (f(x)-f(x-h))/h; % backward

f_1f = @(x) (f(x+h)-f(x))/h; % forward

f_1c = @(x) (f(x+h)-f(x-h))/(2*h); % centered

%% Second derivative finite difference approximations

f_2b = @(x) (f(x)-2*f(x-h)+f(x-2*h))/(h^2); % backward

f_2f = @(x) (f(x+2*h)-2*f(x+h)+f(x))/(h^2); % forward

f_2c = @(x) (f(x+h)-2*f(x)+f(x-h))/(h^2); % centered

%% Plotting

x = -2:0.1:2;

subplot(1,3,1)

plot(x, f(x))

legend('f(x)')

title('Function f')

subplot(1,3,2)

plot(x, f_1(x), 'r-*', x, f_1b(x), 'b', x, f_1f(x), 'm', x, f_1c(x) , 'g')

legend('df/dx', 'backward finite difference approximation of df/dx', 'forward finite difference approximation of df/dx', 'centered finite difference approximation of df/dx' )

title('First derivative of f and its approximations')

subplot(1,3,3)

plot(x, f_2(x), 'r-*', x, f_2b(x), 'b', x, f_2f(x), 'm', x, f_2c(x) , 'g')

legend('d^2f/dx^2', 'backward finite difference approximation of d^2f/dx^2', 'forward finite difference approximation of d^2f/dx^2', 'centered finite difference approximation of d^2f/dx^2' )

title('Second derivative of f and its approximations')

If you have any question feel free to ask...

Hope this may help you.

Thank you ??


Related Solutions

Q.3 Consider the function f(x) = x^2– 2x + 4 on the interval [-2, 2] with...
Q.3 Consider the function f(x) = x^2– 2x + 4 on the interval [-2, 2] with h = 0.25. Write the MATLAB function file to find the first derivatives in the entire interval by all three methods i.e., forward, backward, and centered finite difference approximations.
Consider the function f(x)=2x^3-9x^2+4, over the interval [-1,5] a.Find the local maximum and minimum. b.Find the...
Consider the function f(x)=2x^3-9x^2+4, over the interval [-1,5] a.Find the local maximum and minimum. b.Find the absolute maximum and minimum.
5. Consider the function f(x) = -x^3 + 2x^2 + 2. (a) Find the domain of...
5. Consider the function f(x) = -x^3 + 2x^2 + 2. (a) Find the domain of the function and all its x and y intercepts. (b) Is the function even or odd or neither? (c) Find the critical points, all local extreme values of f, and the intervals on which f is increasing or decreasing. (d) Find the intervals where f is concave up or concave down and all inflection points. (e) Use the information you have found to sketch...
a.  For the following probability density function:                 f(X)= 3/4 (2X-X^2 ) 0 ≤ X ≤ 2           &nbsp
a.  For the following probability density function:                 f(X)= 3/4 (2X-X^2 ) 0 ≤ X ≤ 2                        = 0 otherwise            find its expectation and variance. b. The two regression lines are 2X - 3Y + 6 = 0 and 4Y – 5X- 8 =0 , compute mean of X and mean of Y. Find correlation coefficient r , estimate y for x =3 and x for y = 3.
Consider the function f(x, y) = 4xy − 2x 4 − y 2 . (a) Find...
Consider the function f(x, y) = 4xy − 2x 4 − y 2 . (a) Find the critical points of f. (b) Use the second partials test to classify the critical points. (c) Show that f does not have a global minimum.
Consider the root of function f(x) = x 3 − 2x − 5. The function can...
Consider the root of function f(x) = x 3 − 2x − 5. The function can be rearranged in the form x = g(x) in the following three ways: (a) x = g(x) = x3 − x − 5 (b) x = g(x) = (x 3 − 5)/2 (c) x = g(x) = thirdroot(2x + 5) For each form, apply fixed-point method with an initial guess x0 = 0.5 to approximate the root. Use the error tolerance = 10-5 to...
Integrate the following function by using symbolic operation in MATLAB. y = x^3 − 2x^2 +...
Integrate the following function by using symbolic operation in MATLAB. y = x^3 − 2x^2 + 7
F(x) = 0 + 2x + (4* x^2)/2! + (3*x^3)/3! + ..... This is a taylors...
F(x) = 0 + 2x + (4* x^2)/2! + (3*x^3)/3! + ..... This is a taylors series for a function and I'm assuming there is an inverse function with an inverse taylors series, I am trying to find as much of the taylors series of the inverse function (f^-1) as I can
Find the absolute minimum of the function f(x) = 2x^6 - 3x^4 on the interval. (negative...
Find the absolute minimum of the function f(x) = 2x^6 - 3x^4 on the interval. (negative Infiniti, positive Infiniti)thank you
For the function f(x) = x^2 +3x / 2x^2 + 6x +3 find the following, and...
For the function f(x) = x^2 +3x / 2x^2 + 6x +3 find the following, and use it to graph the function. Find: a)(2pts) Domain b)(2pts) Intercepts c)(2pts) Symmetry d) (2pts) Asymptotes e)(4pts) Intervals of Increase or decrease f) (2pts) Local maximum and local minimum values g)(4pts) Concavity and Points of inflection and h)(2pts) Sketch the curve
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT