The code doesn't work....

2 visualizaciones (últimos 30 días)
재훈
재훈 el 21 de Mayo de 2024
Respondida: Hassaan el 21 de Mayo de 2024
hello everyone
I am practicing numerical differentiation. However, the numerical analysis values ​​and actual values ​​do not appear on the graph at the same time and seem to conflict. Where did we go wrong? Thank you for reading this long sentence. have a good day:)
here is my code :
clc; clear all; close all;
fun = @(t) -0.01953125.*t.^2 + 18.75.*t
est_point = 100;
da = 1;
dfda_central = (fun(est_point + da) - fun(est_point - da))./2*da
x = 0:0.01:960;
fx = fun(x);
dfda_true = -0.0390625.*est_point + 18.75
dgraph = @(point, dfda, t) dfda*t + (fun(point) - dfda*point);
figure(1)
plot(x,fx,'black','linewidth',2); hold on;
plot(x, dgraph(est_point, dfda_central, x),'red'); hold on;
plot(x, dfda_true,'blue'); hold on;
grid on;
rel_error=norm(dfda_true-dfda_central)/norm(dfda_true);
disp(rel_error);
disp(dfda_true);

Respuesta aceptada

Hassaan
Hassaan el 21 de Mayo de 2024
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)]);
Relative Error: 0
disp(['True Derivative: ', num2str(dfda_true)]);
True Derivative: 14.8438
-----------------------------------------------------------------------------------------------------------------------------------------------------
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.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Productos


Versión

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by