Write a MATLAB function which calculates and plots the error

Write a MATLAB function which calculates and plots the error of both the forward and central difference approximations of the first derivative of a polynomial as a function of the step size. The function will have three inputs: 1. a vector whose elements are the coefficients of the polynomial, 2. the point at which the derivative of the function is to be approximated, and 3. the initial step size. The output should be a plot of the error as a function of the step size.

Solution

I will give one example to differenitate this function y(x)=e^(-x)*sin(3x),using forward ,backward and central difference using 101 points from x=0 to x=4 and plot the estimates and the actual function derivatives.here is my code.

f = @(x) exp(-x)*sin(3*x); %actual derivative of function fprime = @(x) -exp(-x)*sin(3*x)+ 3*exp(-x)*cos(3*x);

%step size:

h=0.04;

%forward difference dfdx_forward = (f(2+h)-f(2))/h Error_forward = fprime(2)-dfdx_forward %error

 Write a MATLAB function which calculates and plots the error of both the forward and central difference approximations of the first derivative of a polynomial

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site