Take the function y = x^2 The derivative of this function is y\' = 2x Create a M
ID: 3861843 • Letter: T
Question
Take the function y = x^2 The derivative of this function is y' = 2x Create a MATLAB code to solve for the derivative numerically using forward differencing where you can input different step size for your grid (different h) Plot the following lines on the same figure from x = 0 to 10 A black line showing the analytical derivative (use a step size of 0.1) A red lines showing the numerical derivative using a step size of b = 4. A blue line showing the numerical derivative using a step size of h = 1 A green dashed line showing the numerical derivative using a step size of h = 0.1 Include the title "Numerical Derivative using different step sizes' and be sure to include a legend indicating which line is which.Explanation / Answer
clc;
close all;
clear all;
% f = @(x) x.^2;
% fp=@(x) 2*x;
%
% dfdx_forward = (f(2+h)-f(2))/h ;
% Error_forward = fprime(2)-dfdx_forward %error
h = 0.1; % step size
X = 0:h:10; % domain
f = X.^2; % range
Y = diff(f)/h; % first derivative
plot(X,f,'black')
hold on;
h = 4; % step size
X = 0:h:10; % domain
f = X.^2; % range
Y = diff(f)/h; % first derivative
plot(X,f,'r')
h = 1; % step size
X = 0:h:10; % domain
f = X.^2; % range
Y = diff(f)/h; % first derivative
plot(X,f,'blue')
h = 0.1; % step size
X = 0:h:10; % domain
f = X.^2; % range
Y = diff(f)/h; % first derivative
plot(X,f,'g-')
legend('Analytical h=0.1','Numerical h=4','Numerical h=1','Numerical h=0.1')
xlabel('')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.