Question

************matlab code please*******************

4. Errors in finite difference approximations. Consider the second-order accurate central difference formula for computing th

************matlab code please*******************

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


Matlab code for log log plot of 2nd derivative clear all close all %function for which derivative have to find f-e(x) exp (XError vs. step size for central difference 10 10-10 10 10 10 10 102 10° step size (h) Published with MATLAB R2018a

%%Matlab code for log log plot of 2nd derivative

clear all
close all

%function for which derivative have to find
f=@(x) exp(x).*sin(3*x);
%displaying the function
fprintf('The function for which derivative have to find f(x)\n')
disp(f)

%function for exact derivative
df_ext= @(x) 6*cos(3*x)*exp(x) - 8*sin(3*x)*exp(x);
%second derivative of the function
fprintf('Second derivative of the function \n')
disp(df_ext)

%All step sizes
hh=logspace(-1,-10,100);

%formula for central derivative
f2_cen=@(x,h) (f(x+h)-2*f(x)+f(x-h))./(h.^2);

%here x0=0.4
x0=0.4;
ext_val=df_ext(x0);

fprintf('Exact value for 2nd derivative at x=0.4 is %f.\n',ext_val);

%loop for error for different h
for i=1:length(hh)
  
    dff(i)=f2_cen(x0,hh(i));
    error(i)=abs(ext_val-dff(i));
  
end

%plotting error vs. step size
loglog(hh,error)
xlabel('step size (h)')
ylabel('Error')
title('Error vs. step size for central difference')

%%%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
************matlab code please******************* ************matlab code please******************* 4. Errors in finite...
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 show me a Matlab script that will compute the total errors of the approximation due...

    please show me a Matlab script that will compute the total errors of the approximation due to the given function, also include the panel plot as well, thank you. 1) This problem studies the errors due to the approximation of the first derivative of a given function f(x) using the forward and centered difference methods. For this problem, we consider f(x)=sin(x). a) First, we will investigate the effect of the step size h on the first derivative approximation. Set h=10',...

  • 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

  • Use the Taylor expansion to derive three finite difference schemes (forward-difference, backward-...

    Use the Taylor expansion to derive three finite difference schemes (forward-difference, backward- difference and central-difference) of the first derivatives as well as the central difference scheme of the second derivative and comment on the accuracy order of each scheme. [10%] Use the Taylor expansion to derive three finite difference schemes (forward-difference, backward- difference and central-difference) of the first derivatives as well as the central difference scheme of the second derivative and comment on the accuracy order of each scheme. [10%]

  • #7. [Extra Credit] is calculus wrong?! Consider f(x) = ex (a) Calculate the derivative of fx) atx...

    #7. [Extra Credit] is calculus wrong?! Consider f(x) = ex (a) Calculate the derivative of fx) atx 0 using O(h) finite difference (forward and backward) and O(h2) centered finite difference. Vary h in the following manner: 1, 101,102... 1015. (Write a MATLAB script for this purpose and call it pset5_prob7) (b) Modify your script to plot (log-log) the the true percent error in all three cases as a function of h in one plot. (c) In calculus we learned that...

  • Problem 4 : Finite difference formula and interpolation For this problem, you will approximate th...

    Please answer this problem using MATLAB. Problem 4 : Finite difference formula and interpolation For this problem, you will approximate the derivative of the function g(x)5x6x 23823x 15 on a set of points using the centered difference formula (x +h) -g(x- h) 2h g'(x) Then, you will find the interpolating polynomial through these points, Q, and verify that it is indeed close to the polynomial that is the true derivative of g, ie. Q(x) g'(x) 25x 24x36x+16x+3 In your prob40...

  • Compute the backward and center difference approximations for the 1st derivative of y = log x...

    Compute the backward and center difference approximations for the 1st derivative of y = log x evaluated @ x=20 and h=2. Using the second order error O(h2) formula.

  • Question 1 15 Points) It is always desirable to have/ use the finite difference approximation wit...

    Question 1 15 Points) It is always desirable to have/ use the finite difference approximation with error term. Please using the Taylor Series: higher order of truncation sw(x) h" +R 2! 3! (I) Derive the following forward difference approximation of the 2nd orde 2) What is the order of error for this case? derivative of f(x). f" derivative off(x) h2 Question 1 15 Points) It is always desirable to have/ use the finite difference approximation with error term. Please using...

  • Please explain 0 Required information Consider the function2x4 on the interval (-2, 2] with h 0.25....

    Please explain 0 Required information Consider the function2x4 on the interval (-2, 2] with h 0.25. Which among the fonward, backward, and centered finite difference approximations for the second derivative is more accurate when it is compared with theoretical value? (Refer to the graph plotted to arrive at the result) The most accurate result is obtained usin Cliek to select finite difference approximation. backward forward

  • please do this problem in matlab and show me the code. Thanks. please do this problem...

    please do this problem in matlab and show me the code. Thanks. please do this problem in matlab and show me the code. Thanks. please do this problem in matlab and show me the code. Thanks. please do this problem in matlab and show me the code. Thanks. please do this problem in matlab and show me the code. Thanks.please do this problem in matlab and show me the code. Thanks. please do this problem in matlab and show me...

  • please i need the requiered MATLAB script to solve this. All calculations need to be done in matlab please Design Layout References Mailings Review View Share 1) (15 points) Calculate the followi...

    please i need the requiered MATLAB script to solve this. All calculations need to be done in matlab please Design Layout References Mailings Review View Share 1) (15 points) Calculate the following for the function f(x) = e-a-x a. Use calculus to determine the correct value of the derivative. b. Develop an M-file function to evaluate the centered finite-difference approximations, starting with x = 0.5 Thus, for the first evaluation, the x values for the centered difference approximation will be...

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