clc; clear all; close all;
% Define the function
fun = @(t) -0.01953125.*t.^2 + 18.75.*t;
% Estimation point and step size
est_point = 100;
da = 1;
% Central difference calculation
dfda_central = (fun(est_point + da) - fun(est_point - da)) / (2*da);
% Define the range and function values for plotting
x = 0:0.01:960;
fx = fun(x);
% True derivative value
dfda_true = -0.0390625.*est_point + 18.75;
% Define the line representing the central difference derivative
dgraph = @(point, dfda, t) dfda * t + (fun(point) - dfda * point);
% Plot the original function
figure(1);
plot(x, fx, 'black', 'linewidth', 2); hold on;
% Plot the central difference approximation
plot(x, dgraph(est_point, dfda_central, x), 'red');
% Plot the true derivative as a constant line
plot(x, dfda_true * ones(size(x)), 'blue');
% Add grid and legend
grid on;
legend('Function', 'Central Difference Approximation', 'True Derivative');
% Calculate and display the relative error
rel_error = norm(dfda_true - dfda_central) / norm(dfda_true);
disp(['Relative Error: ', num2str(rel_error)]);
disp(['True Derivative: ', num2str(dfda_true)]);
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.