%%Matlab code for forward backward and central difference
method
clear all
close all
%function for which derivative have to find
f=@(x) exp(x);
%exact solution for f'(x)=exp(x) at x=0
ext_val=exp(0);
%x value at which derivative have to find
x0=0;
%loop for forward backward and central difference formulae
fprintf('\th,\terr_frd,\tbkd_err,\tcnt_err\n')
for i=1:16
%all step size
h=10^-(i-1);
%forward diff
fd_frwd=(f(x0+h)-f(x0))/h;
%backward diff
bk_frwd=(f(x0)-f(x0-h))/h;
%central diff
cn_frwd=(f(x0+h/2)-f(x0-h/2))/h;
%true percent error forward diff
er_fd(i)=(abs((ext_val-fd_frwd)/ext_val))*100;
%true percent error backward diff
er_bk(i)=(abs((ext_val-bk_frwd)/ext_val))*100;
%true percent error central diff
er_cn(i)=(abs((ext_val-cn_frwd)/ext_val))*100;
%step size
hh(i)=h;
fprintf('\t%.2e, %.2e, \t %.2e, \t
%.2e.\n',h,er_fd(i),er_bk(i),er_cn(i))
end
%Loglog plot of true error vs h
loglog(hh,er_fd,'linewidth',2)
hold on
loglog(hh,er_bk,'linewidth',2)
loglog(hh,er_cn,'linewidth',2)
xlabel('step size (h)')
ylabel('True percent error')
title('True percent error vs. step size plot')
legend('Forward difference','Backward difference','Central
difference')
fprintf('\nUpto h=10^-5 for central diff and h=10^-8 for forward
and backward diff error decreases with h.\n')
fprintf('Beyond that by further decreasing h error tends to
increases which contradict our assumpsion .\n ')
%%%%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%%%%%%
#7. [Extra Credit] is calculus wrong?! Consider f(x) = ex (a) Calculate the derivative of fx) atx...
3. Find the first derivative of a functionf(x)-ex (a) Use calculus to determine the correct value of the derivative at x = 2. If h = 0.25, (b) Evaluate the second-order centered finite-difference approximation (e) Evaluate the second-order forward difference approximation. (d) Evaluate the second-order backward difference approximation. (e) Create a MATLAB function program, which gives output up to second order centered finite difference approximation of second derivative "(xo). The input arguments aref n (order of approximation, 1 or 2),...
MatlabMECE 2350 Numerical Methods Lab 8.1. Differentiate the following function: f(x) = ex -2x +1 and solve its first derivative atx = 8 2. Numerically evaluate the approximated first derivative from the above function at x = 8 and h = 0.15 by the following: (a) Forward finite difference method (b) Backward finite difference method (c) Centered finite difference method 3. Calculate the error of each method by comparing the numerical derivative with the result from problem 1.
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
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...
#use MATLAB script1) Calculate the following for the function f(x) = e-4x- 2x3 a. Calculate the derivative of the function by hand. Write a MATLAB function that calculates the derivative 05. of this function and calculate the derivative at x = 0.5. b. Develop an M- to evaluate the cetered finite-difference approximation (use equation below), at x = 0.5. Assume that h = 0.1. c. Repeat part (b) for the second-order forward and backward differences. Again Assume that h = 0.1. d. Using the results...
3. Consider the function f(x) = -0.1.24 – 0.15x3 – 0.522 – 0.25x + 1.2. (a) Obtain the analytical expression (i.e. True or Exact Solution) for the first derivative, Eval- uate its value at 1 =0.5. Box your answer and label it as fexact- (b) Now assume the function is discretized on a grid with uniform spacing of h. Evaluate your finite difference approximation at x = 0.5 using central differencing with step sizes starting at 1 and re- duced...