Hi Elias,
I understand that you need to create two functions and a script: one function to calculate derivatives using finite difference methods, another to compute RMS error between vectors, and a script to compare these derivatives with “gradient” function and explain the error due to floating-point precision.
In order to acheive the same you can follow the below steps:
Create the Derivative Function:
Implement a function that calculates the derivative using forward, backward, and central difference methods.
function dy = customDerivative(y, h)
n = length(y);
dy = zeros(size(y));
dy(1) = (y(2) - y(1)) / h; % Forward difference
for i = 2:n-1
dy(i) = (y(i+1) - y(i-1)) / (2*h); % Central difference
end
dy(n) = (y(n) - y(n-1)) / h; % Backward difference
end
Create the RMS Error Function:
Implement a function to calculate the RMS error between two vectors.
function error = rmsError(v1, v2)
error = sqrt(mean((v1 - v2).^2));
end
Write the Comparison Script:
Create a script to generate a quadratic function, compute derivatives using your function and MATLAB’s “gradient”, and compare them.
x = linspace(0, 10, 100);y = x.^2 + 2*x + 1; % Quadratic functionh = x(2) - x(1);
dy_custom = customDerivative(y, h);
dy_builtin = gradient(y, h);
error = rmsError(dy_custom, dy_builtin);
disp([‘RMS Error: ‘, num2str(error)]);
Explain the RMS Error:
The RMS error is not zero due to floating-point precision limits. Machine epsilon is the smallest difference distinguishable by the computer, affecting calculations by causing small discrepancies between theoretically identical operations.
Refer to the documentation of “gradient” function to know more about its usage: https://www.mathworks.com/help/matlab/ref/gradient.html
Hope this helps!
