So the assignment states as following:
Type an m-file numerical_derivative.m that performs numerical derivation. Use it to calculate $f'(-3)$ when $f(x) = 3x^2 /(\ln(1-x))$.
In the m-file, you have to use $h = 10^{-6}$. The file should have the following main function:
function y = numericalderivative (f, x)
% Calculates the numerical value in the case of f in punk x.
% --- Input ---
% f: function handle f(x)
% x: the point where the derivative is calculated
% --- output ---
% y: the numerical derivative of f on the point x
If I want to save it as a file and run the program in Matlab, doesn't it make it redundant to use a function?
function y= numerical_derivative(dxdy,x)
h=10.^-6;
func = @(x) (3*x^2)/log(1-x);
x = -3;
numerical_derivative = @(x)((x+h)-(x))./h;
dxdy = numerical_derivative(func,x);
end
This is what I've got, but it won't run. Any help would be greatly appreciated!
You have hindered yourself by re-labeling
fto the non-sensicaldxdyin the call format. What you should have done is to strictly implement divided differencesThus the implementation of
numericalderivativehas one line.Bytheway, the
h=1e-6is in the optimal range for the one-sided difference quotient, for the symmetric the optimal range ish=1e-4toh=1e-3, as one can confirm with error plots using the graphical tool of your choice, here gnuplot