Complete each of the following exercises in a separate M-file. Write a MATLAB fu
ID: 3795266 • Letter: C
Question
Complete each of the following exercises in a separate M-file. Write a MATLAB function which approximates the first derivative of a polynomial of any order at a specified point using the forward difference approximation. The function will have three inputs: a vector whose elements arc the coefficients of the polynomial. the point at which the derivative of the function is to be approximated, and the step sire. The output is the approximated derivative. Repeat problem 1 using the central difference approximation of the first derivative. 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: a vector whose elements are the coefficients of the polynomial. the point at which the derivative of the function is to be approximated, and the initial step size. The output should be a plot of the error as a function of the step size. This function should call the functions from problems and to approximate the derivative at the specified point using the initial step size. This should be repeated 10 times using progressively smaller step sizes (by factors of 2). The error for each step size should then be calculated based on the known value of the derivative at the specified point and then plotted. The plot should be publishable quality. Write an explanation of the results which discusses the following: A comparison (qualitative and quantitative) of the error of the two finite difference methods (include a comparison of the expected and actual order of error for both methods). An explanation of how and why the error changed with changing step size.Explanation / Answer
clear all;
close all
clc
%the function
f = @(x) 10*x^3-7*x^2+5*x-3;
%actual derivative of function
fprime = @(x) 30*x^2-14*x+5;
%step size:
h=0.5;
for i = 1:10
%forward difference
dfdx_forward = (f(3+h)-f(3))/h
Error_forward = fprime(3)-dfdx_forward %error
%central difference
dfdx_central = (f(3+h)-f(3-h))/(2*h)
Error_central = fprime(3)-dfdx_central %error
h = h*2;
end
plot(dfdx_forward,dfdx_central);
axis([60 inf -1 1])
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.