Machine Epsilon/Error Question
6 views (last 30 days)
Show older comments
I'm doing a homework problem in which I need to make three files (two functions, one script).
My first function calculates the derivative of an input vector using forward difference method and backward difference method for the first and last points respectively, and then uses a "for" loop to calculate the central difference from the second data point to the penultimate point.
My second function calculates the RMS error between two input vectors using a statistical formula.
Finally, my script asks me to compare the RMS error between my custom derivative function and MATLAB's built-in gradient function using a random quadratic function (math). It then asks me to explain why the RMS error is not exactly zero (using concepts such as machine epsilon, floating point, etc.) which is where I am a bit confused. I vaguely understand what machine epsilon is, but I don't understand how it affects the error I'm getting between the two calculations.
Can someone please explain this? I will provide code if requested.
1 Comment
Hari
on 24 Feb 2025
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!
Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!